Class: Lita::Handlers::Nagios

Inherits:
Handler
  • Object
show all
Defined in:
lib/lita/handlers/nagios.rb

Instance Method Summary collapse

Constructor Details

#initialize(robot) ⇒ Nagios

Returns a new instance of Nagios.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lita/handlers/nagios.rb', line 13

def initialize(robot)
  @site = NagiosHarder::Site.new(
    robot.config.handlers.nagios.cgi,
    robot.config.handlers.nagios.user,
    robot.config.handlers.nagios.pass,
    robot.config.handlers.nagios.version,
    robot.config.handlers.nagios.time_format,
    robot.config.handlers.nagios.verify_ssl
  )
  super(robot)
end

Instance Method Details

#acknowledge(response) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/lita/handlers/nagios.rb', line 90

def acknowledge(response)
  args = response.extensions[:kwargs]
  return response.reply("Missing 'host' argument") unless args[:host]

  user = response.message.source.user.name
  message =  args[:message] ? "#{args[:message]} (#{user})" : "acked by #{user}"

  if args[:service]
    method_w_params = [ :acknowledge_service, args[:host], args[:service], message ]
    reply = "#{args[:service]} on #{args[:host]}"
  else
    method_w_params = [ :acknowledge_host, args[:host], message ]
    reply = args[:host]
  end

  reply = @site.send(*method_w_params) ? "Acknowledgment set for #{reply}" : "Failed to acknowledge #{reply}"
  response.reply(reply)
end

#receive(request, response) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/lita/handlers/nagios.rb', line 157

def receive(request, response)
  params = request.params

  if params.has_key?("room")
    room = params["room"]
  elsif config.default_room
    room = config.default_room
  else
    raise "Room must be defined. Either fix your command or specify a default room ('config.handlers.nagios.default_room')"
  end

  message = nil
  case params["notificationtype"]
  when "ACKNOWLEDGEMENT"
    message = "[ACK] "
  when "PROBLEM", "RECOVERY"
    message = ""
  else
    # Don't process FLAPPING* and DOWNTIME* events for now
    return
  end

  case params["type"]
  when "service"
    message += "#{params["host"]}:#{params["description"]} is #{params["state"]}: #{params["output"]}"
  when "host"
    message += "#{params["host"]} is #{params["state"]}: #{params["output"]}"
  else
    raise "Notification type must be defined in Nagios command ('host' or 'service')"
  end

  target = Source.new(room: room)
  robot.send_message(target, "nagios: #{message}")
rescue Exception => e
  Lita.logger.error(e)
end

#recheck(response) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/lita/handlers/nagios.rb', line 62

def recheck(response)
  args = response.extensions[:kwargs]
  return response.reply("Missing 'host' argument") unless args[:host]

  if args[:service]
    method_w_params = [ :schedule_service_check, args[:host], args[:service] ]
    reply = "#{args[:service]} on #{args[:host]}"
  else
    method_w_params = [ :schedule_host_check, args[:host] ]
    reply = args[:host]
  end

  reply = @site.send(*method_w_params) ? "Check scheduled for #{reply}" : "Failed to schedule check for #{reply}"
  response.reply(reply)
end

#schedule_downtime(response) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/lita/handlers/nagios.rb', line 121

def schedule_downtime(response)
  args = response.extensions[:kwargs]
  return response.reply("Missing 'host' argument") unless args[:host]

  units = { "m" => :minutes, "h" => :hours, "d" => :days }
  match = /^(?<value>\d+)(?<unit>[#{units.keys.join}])?$/.match(args[:duration])
  return response.reply("Invalid downtime duration") unless (match and match[:value])

  duration = match[:unit] ? match[:value].to_i.send(units[match[:unit]]) : match[:value].to_i

  options = case response.match_data[:type]
  when "fixed"
    { type: :fixed, start_time: Time.now, end_time: Time.now + duration }
  when "flexible"
    { type: :flexible, hours: (duration / 3600), minutes: (duration % 3600 / 60) }
  end.merge({ author: "#{response.message.source.user.name} via Lita" })

  if args[:service]
    method_w_params = [ :schedule_service_downtime, args[:host], args[:service], options ]
    reply = "#{args[:service]} on #{args[:host]}"
  else
    method_w_params = [ :schedule_host_downtime, args[:host], options ]
    reply = args[:host]
  end

  reply = @site.send(*method_w_params) ? "#{options[:type].capitalize} downtime set for #{reply}" : "Failed to schedule downtime for #{reply}"
  response.reply(reply)
end

#toggle_notifications(response) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/lita/handlers/nagios.rb', line 41

def toggle_notifications(response)
  args = response.extensions[:kwargs]
  return response.reply("Missing 'host' argument") unless args[:host]

  action = response.match_data[:action]

  reply = @site.send("#{action}_service_notifications", args[:host], args[:service])
  response.reply(reply)
end