Class: NagiosHarder::Site

Inherits:
Object
  • Object
show all
Includes:
HTTParty::ClassMethods
Defined in:
lib/nagiosharder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nagios_url, user, password, version = 3, nagios_time_format = nil, ssl_verify = true) ⇒ Site

Returns a new instance of Site.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nagiosharder.rb', line 31

def initialize(nagios_url, user, password, version = 3, nagios_time_format = nil, ssl_verify = true)
  @nagios_url = nagios_url.gsub(/\/$/, '')
  @user = user
  @password = password
  @default_options = {:verify => ssl_verify}
  @default_cookies = {}
  @version = version
  debug_output if ENV['DEBUG']
  basic_auth(@user, @password) if @user && @password
  @nagios_time_format = case nagios_time_format
                        when 'us'
                          "%m-%d-%Y %H:%M:%S"
                        when 'euro'
                          "%d-%m-%Y %H:%M:%S"
                        when 'iso8601'
                          "%Y-%m-%d %H:%M:%S"
                        else
                          if @version.to_i == 3 # allows compatability with nagios 4
                            "%Y-%m-%dT%H:%M:%S"
                          else
                            "%m-%d-%Y %H:%M:%S"
                          end
                        end
  self
end

Instance Attribute Details

#default_cookiesObject

Returns the value of attribute default_cookies.



28
29
30
# File 'lib/nagiosharder.rb', line 28

def default_cookies
  @default_cookies
end

#default_optionsObject

Returns the value of attribute default_options.



28
29
30
# File 'lib/nagiosharder.rb', line 28

def default_options
  @default_options
end

#nagios_time_formatObject

Returns the value of attribute nagios_time_format.



28
29
30
# File 'lib/nagiosharder.rb', line 28

def nagios_time_format
  @nagios_time_format
end

#nagios_urlObject

Returns the value of attribute nagios_url.



28
29
30
# File 'lib/nagiosharder.rb', line 28

def nagios_url
  @nagios_url
end

#passwordObject

Returns the value of attribute password.



28
29
30
# File 'lib/nagiosharder.rb', line 28

def password
  @password
end

#userObject

Returns the value of attribute user.



28
29
30
# File 'lib/nagiosharder.rb', line 28

def user
  @user
end

#versionObject

Returns the value of attribute version.



28
29
30
# File 'lib/nagiosharder.rb', line 28

def version
  @version
end

Instance Method Details

#acknowledge_host(host, comment) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/nagiosharder.rb', line 64

def acknowledge_host(host, comment)
  request = {
    :cmd_typ => COMMANDS[:acknowledge_host_problem],
    :com_author => @user,
    :com_data => comment,
    :host => host,
    :send_notification => true,
    :persistent => false,
    :sticky_ack => true
  }

  post_command(request)
end

#acknowledge_service(host, service, comment) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nagiosharder.rb', line 87

def acknowledge_service(host, service, comment)
  request = {
    :cmd_typ => COMMANDS[:acknowledge_service_problem],
    :com_author => @user,
    :com_data => comment,
    :host => host,
    :service => service,
    :send_notification => true,
    :persistent => false,
    :sticky_ack => true
  }

  post_command(request)
end

#alert_history(options = {}) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/nagiosharder.rb', line 356

def alert_history(options = {})
  params = {}

  {
    :state_type => :history_state,
    :type => :history
  }.each do |key, val|
    if options[key] && (options[key].is_a?(Array) || options[key].is_a?(Symbol))
      params[key.to_s.gsub(/_/, '')] = Nagiosharder::Filters.value(val, *options[key])
    end
  end
  
  # if any of the standard filter params are already integers, those win
  %w(
    :statetype,
    :type
  ).each do |key|
    params[key.to_s] = options[:val] if !options[:val].nil? && options[:val].match(/^\d*$/)
  end
  
  params['host'] = options[:host] || 'all'
  params['archive'] = options[:archive] || '0'
  
  query = params.select {|k,v| v }.map {|k,v| "#{k}=#{v}" }.join('&')
  
  alert_history_url = "#{history_url}?#{query}"
  puts alert_history_url
  response = get(alert_history_url)

  raise "wtf #{alert_history_url}? #{response.code}" unless response.code == 200

  alerts = []
  parse_history_html(response) do |alert|
    alerts << alert
  end

  alerts
end

#cancel_downtime(downtime_id, downtime_type = :host_downtime) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/nagiosharder.rb', line 173

def cancel_downtime(downtime_id, downtime_type = :host_downtime)
  request = {
    :cmd_typ => COMMANDS["del_#{downtime_type}".to_sym],
    :down_id => downtime_id
  }
  
  post_command(request)
end

#cmd_urlObject



399
400
401
# File 'lib/nagiosharder.rb', line 399

def cmd_url
  "#{nagios_url}/cmd.cgi"
end

#disable_service_notifications(host, service, options = {}) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/nagiosharder.rb', line 318

def disable_service_notifications(host, service, options = {})
  request = {
    :host => host
  }

  if service
    request[:cmd_typ] = COMMANDS[:disable_service_notifications]
    request[:service] = service
  else
    request[:cmd_typ] = COMMANDS[:disable_host_service_checks]
    request[:ahas] = true
  end

  # TODO add option to block until the service shows as disabled
  post_command(request)
end

#enable_service_notifications(host, service, options = {}) ⇒ Object



335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/nagiosharder.rb', line 335

def enable_service_notifications(host, service, options = {})
  request = {
    :host => host
  }

  if service
    request[:cmd_typ] = COMMANDS[:enable_service_notifications]
    request[:service] = service
  else
    request[:cmd_typ] = COMMANDS[:enable_host_service_notifications]
    request[:ahas] = true
  end

  # TODO add option to block until the service shows as disabled
  post_command(request)
end

#extinfo_urlObject



403
404
405
# File 'lib/nagiosharder.rb', line 403

def extinfo_url
  "#{nagios_url}/extinfo.cgi"
end

#history_urlObject



407
408
409
# File 'lib/nagiosharder.rb', line 407

def history_url
  "#{nagios_url}/history.cgi"
end

#host_status(host) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/nagiosharder.rb', line 205

def host_status(host)
  host_status_url = "#{status_url}?host=#{host}&embedded=1&noheader=1&limit=0"
  response = get(host_status_url)

  raise "wtf #{host_status_url}? #{response.code}" unless response.code == 200

  services = {}
  parse_status_html(response) do |status|
    services[status[:service]] = status
  end

  services
end

#hostgroups_detail(hostgroup = "all") ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/nagiosharder.rb', line 304

def hostgroups_detail(hostgroup = "all")
  hostgroups_detail_url = "#{status_url}?hostgroup=#{hostgroup}&style=hostdetail&embedded=1&noheader=1&limit=0"
  response = get(hostgroups_detail_url)
  
  raise "wtf #{hostgroups_detail_url}? #{response.code}" unless response.code == 200
  
  hosts = {}
  parse_detail_html(response) do |status|
    hosts[status[:host]] = status
  end
  
  hosts
end

#hostgroups_summary(hostgroup = "all") ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/nagiosharder.rb', line 276

def hostgroups_summary(hostgroup = "all")
  hostgroups_summary_url = "#{status_url}?hostgroup=#{hostgroup}&style=summary"
  response = get(hostgroups_summary_url)

  raise "wtf #{hostgroups_summary_url}? #{response.code}" unless response.code == 200

  hostgroups = {}
  parse_summary_html(response) do |status|
    hostgroups[status[:group]] = status
  end

 hostgroups
end

#post_command(body) ⇒ Object



57
58
59
60
61
62
# File 'lib/nagiosharder.rb', line 57

def post_command(body)
  # cmd_mod is always CMDMODE_COMMIT
  body = {:cmd_mod => 2}.merge(body)
  response = post(cmd_url, :body => body)
  response.code == 200 && response.body.match(/successful/) && true
end

#schedule_host_check(host) ⇒ Object



182
183
184
185
186
187
188
189
190
191
# File 'lib/nagiosharder.rb', line 182

def schedule_host_check(host)
  request = {
    :start_time => formatted_time_for(Time.now),
    :host => host,
    :force_check => true,
    :cmd_typ => COMMANDS[:schedule_host_check],
  }
  
  post_command(request)
end

#schedule_host_downtime(host, options = {}) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/nagiosharder.rb', line 112

def schedule_host_downtime(host, options = {})
  options[:type] ||= :fixed
  
  request = {
    :cmd_typ => COMMANDS[:schedule_host_downtime],
    :com_author => options[:author] || "#{@user} via nagiosharder",
    :com_data => options[:comment] || 'scheduled downtime by nagiosharder',
    :host => host,
    :childoptions => 0,
    :trigger => 0
  }

  # FIXME we could use some option checking...

  request[:fixed] = case options[:type].to_sym
                    when :fixed then 1
                    when :flexible then 0
                    else 1 # default to fixed
                    end

  if request[:fixed] == 0
    request[:hours]   = options[:hours]
    request[:minutes] = options[:minutes]
  end

  request[:start_time] = formatted_time_for(options[:start_time] || Time.now)
  request[:end_time]   = formatted_time_for(options[:end_time]   || Time.now + 1.hour)

  post_command(request)
end

#schedule_service_check(host, service) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/nagiosharder.rb', line 193

def schedule_service_check(host, service)
  request = {
    :start_time => formatted_time_for(Time.now),
    :host => host,
    :service => service,
    :force_check => true,
    :cmd_typ => COMMANDS[:schedule_service_check],
  }
  
  post_command(request)
end

#schedule_service_downtime(host, service, options = {}) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/nagiosharder.rb', line 143

def schedule_service_downtime(host, service, options = {})
  options[:type] ||= :fixed

  request = {
    :cmd_typ => COMMANDS[:schedule_service_downtime],
    :com_author => options[:author] || "#{@user} via nagiosharder",
    :com_data => options[:comment] || 'scheduled downtime by nagiosharder',
    :host => host,
    :service => service,
    :trigger => 0
  }

  request[:fixed] = case options[:type].to_sym
                    when :fixed then 1
                    when :flexible then 0
                    else 1
                    end


 if request[:fixed] == 0
    request[:hours]   = options[:hours]
    request[:minutes] = options[:minutes]
  end

  request[:start_time] = formatted_time_for(options[:start_time] || Time.now)
  request[:end_time]   = formatted_time_for(options[:end_time]   || Time.now + 1.hour)

  post_command(request)
end

#service_notifications_disabled?(host, service) ⇒ Boolean

Returns:

  • (Boolean)


352
353
354
# File 'lib/nagiosharder.rb', line 352

def service_notifications_disabled?(host, service)
  self.host_status(host)[service].notifications_disabled
end

#service_status(options = {}) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/nagiosharder.rb', line 219

def service_status(options = {})
  params = {}

  {
    :host_status_types    => :notification_host,
    :service_status_types => :notification_service,
    :sort_type            => :sort,
    :sort_option          => :sort,
    :host_props           => :host,
    :service_props        => :service,
  }.each do |key, val|
    if options[key] && (options[key].is_a?(Array) || options[key].is_a?(Symbol))
      params[key.to_s.gsub(/_/, '')] = Nagiosharder::Filters.value(val, *options[key])
    end
  end

  # if any of the standard filter params are already integers, those win
  %w(
    :hoststatustypes,
    :servicestatustypes,
    :sorttype,
    :sortoption,
    :hostprops,
    :serviceprops,
  ).each do |key|
    params[key.to_s] = options[:val] if options[:val] && options[:val].match(/^\d*$/)
  end

  if @version == 3
    params['servicegroup'] = options[:group] || 'all'
    params['style'] = 'detail'
    params['embedded'] = '1'
    params['noheader'] = '1'
    params['limit'] = 0
  else
    if options[:group]
      params['servicegroup'] = options[:group]
      params['style'] = 'detail'
    else
      params['host'] = 'all'
    end
  end

  query = params.select {|k,v| v }.map {|k,v| "#{k}=#{v}" }.join('&')
  url = "#{status_url}?#{query}"
  response = get(url)

  raise "wtf #{url}? #{response.code}" unless response.code == 200

  statuses = []
  parse_status_html(response) do |status|
    statuses << status
  end

  statuses
end

#servicegroups_summary(servicegroup = "all") ⇒ Object



290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/nagiosharder.rb', line 290

def servicegroups_summary(servicegroup = "all")
  servicegroups_summary_url = "#{status_url}?servicegroup=#{servicegroup}&style=summary"
  response = get(servicegroups_summary_url)

  raise "wtf #{servicegroups_summary_url}? #{response.code}" unless response.code == 200

  servicegroups = {}
  parse_summary_html(response) do |status|
    servicegroups[status[:group]] = status
  end

  servicegroups
end

#status_urlObject



395
396
397
# File 'lib/nagiosharder.rb', line 395

def status_url
  "#{nagios_url}/status.cgi"
end

#unacknowledge_host(host) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/nagiosharder.rb', line 78

def unacknowledge_host(host)
  request = {
    :cmd_typ => COMMANDS[:remove_host_acknowledgement],
    :host => host
  }

  post_command(request)
end

#unacknowledge_service(host, service) ⇒ Object



102
103
104
105
106
107
108
109
110
# File 'lib/nagiosharder.rb', line 102

def unacknowledge_service(host, service)
  request = {
    :cmd_typ => COMMANDS[:remove_service_acknowledgement],
    :host => host,
    :service => service
  }

  post_command(request)
end