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) ⇒ Site

Returns a new instance of Site.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/nagiosharder.rb', line 27

def initialize(nagios_url, user, password, version = 3)
  @nagios_url = nagios_url.gsub(/\/$/, '')
  @user = user
  @password = password
  @default_options = {}
  @default_cookies = {}
  @version = version
  basic_auth(@user, @password) if @user && @password

  @nagios_time_format = if @version.to_i < 3
                          "%m-%d-%Y %H:%M:%S"
                        else
                          "%Y-%m-%d %H:%M:%S"
                        end
end

Instance Attribute Details

#default_cookiesObject

Returns the value of attribute default_cookies.



24
25
26
# File 'lib/nagiosharder.rb', line 24

def default_cookies
  @default_cookies
end

#default_optionsObject

Returns the value of attribute default_options.



24
25
26
# File 'lib/nagiosharder.rb', line 24

def default_options
  @default_options
end

#nagios_time_formatObject

Returns the value of attribute nagios_time_format.



24
25
26
# File 'lib/nagiosharder.rb', line 24

def nagios_time_format
  @nagios_time_format
end

#nagios_urlObject

Returns the value of attribute nagios_url.



24
25
26
# File 'lib/nagiosharder.rb', line 24

def nagios_url
  @nagios_url
end

#passwordObject

Returns the value of attribute password.



24
25
26
# File 'lib/nagiosharder.rb', line 24

def password
  @password
end

#userObject

Returns the value of attribute user.



24
25
26
# File 'lib/nagiosharder.rb', line 24

def user
  @user
end

#versionObject

Returns the value of attribute version.



24
25
26
# File 'lib/nagiosharder.rb', line 24

def version
  @version
end

Instance Method Details

#acknowledge_service(host, service, comment) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nagiosharder.rb', line 43

def acknowledge_service(host, service, comment)
  # extra options: sticky_arg, send_notification, persistent

  request = {
    :cmd_typ => 34,
    :cmd_mod => 2,
    :com_author => @user,
    :com_data => comment,
    :host => host,
    :service => service,
    :send_notification => true,
    :persistent => false,
    :sticky_ack => true
  }

  response = post(cmd_url, :body => request)
  response.code == 200 && response.body =~ /successful/
end

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



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/nagiosharder.rb', line 137

def cancel_downtime(downtime_id, downtime_type = :host_downtime)
  downtime_types = {
    :host_downtime => 78,
    :service_downtime => 79
  }
  response = post(cmd_url, :body => {
                                    :cmd_typ => downtime_types[downtime_type],
                                    :cmd_mod => 2,
                                    :down_id => downtime_id
                                    })
  response.code == 200 && response.body =~ /successful/
end

#cmd_urlObject



319
320
321
# File 'lib/nagiosharder.rb', line 319

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

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



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/nagiosharder.rb', line 258

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

  if service
    request[:cmd_typ] = 23
    request[:service] = service
  else
    request[:cmd_typ] = 29
    request[:ahas] = true
  end

  response = post(cmd_url, :body => request)
  if response.code == 200 && response.body =~ /successful/
    # TODO enable waiting. seems to hang intermittently
    #if options[:wait]
    #  sleep(3) until service_notifications_disabled?(host, service)
    #end
    true
  else
    false
  end
end

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



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/nagiosharder.rb', line 284

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

  if service
    request[:cmd_typ] = 22
    request[:service] = service
  else
    request[:cmd_typ] = 28
    request[:ahas] = true
  end

  response = post(cmd_url, :body => request)
  if response.code == 200 && response.body =~ /successful/
    # TODO enable waiting. seems to hang intermittently
    #if options[:wait]
    #  sleep(3) while service_notifications_disabled?(host, service)
    #end
    true
  else
    false
  end
end

#extinfo_urlObject



323
324
325
# File 'lib/nagiosharder.rb', line 323

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

#host_status(host) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/nagiosharder.rb', line 244

def host_status(host)
  host_status_url = "#{status_url}?host=#{host}"
  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

#schedule_host_check(host) ⇒ Object



150
151
152
153
154
155
156
157
158
159
# File 'lib/nagiosharder.rb', line 150

def schedule_host_check(host)
  response = post(cmd_url, :body => {
                                      :start_time => formatted_time_for(Time.now),
                                      :host => host,
                                      :force_check => true,
                                      :cmd_typ => 96,
                                      :cmd_mod => 2
                                    })
  response.code == 200 && response.body =~ /successful/
end

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



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/nagiosharder.rb', line 105

def schedule_host_downtime(host, options = {})
  request = {
    :cmd_mod => 2,
    :cmd_typ => 55,
    :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])
  request[:end_time]   = formatted_time_for(options[:end_time])

  response = post(cmd_url, :body => request)

  response.code == 200 && response.body =~ /successful/
end

#schedule_service_check(host, service) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/nagiosharder.rb', line 161

def schedule_service_check(host, service)
  response = post(cmd_url, :body => {
                                      :start_time => formatted_time_for(Time.now),
                                      :host => host,
                                      :service => service,
                                      :force_check => true,
                                      :cmd_typ => 7,
                                      :cmd_mod => 2
                                    })
  response.code == 200 && response.body =~ /successful/
end

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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/nagiosharder.rb', line 74

def schedule_service_downtime(host, service, options = {})
  request = {
    :cmd_mod => 2,
    :cmd_typ => 56,
    :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])
  request[:end_time]   = formatted_time_for(options[:end_time])

  response = post(cmd_url, :body => request)

  response.code == 200 && response.body =~ /successful/
end

#service_notifications_disabled?(host, service) ⇒ Boolean

Returns:

  • (Boolean)


310
311
312
# File 'lib/nagiosharder.rb', line 310

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

#service_status(type, options = {}) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/nagiosharder.rb', line 173

def service_status(type, options = {})
  service_status_type = case type
                        when :ok then 2
                        when :warning then 4
                        when :unknown then 8
                        when :critical then 16
                        when :pending then 1
                        when :all_problems then 28
                        when :all then nil
                        else
                          raise "Unknown type"
                        end

  sort_type = case options[:sort_type]
              when :asc then 1
              when :desc then 2
              when nil then nil
              else
                raise "Invalid options[:sort_type]"
              end

  sort_option = case options[:sort_option]
                when :host then 1
                when :service then 2
                when :status then 3
                when :last_check then 4
                when :duration then 6
                when :attempts then 5
                when nil then nil
                else
                  raise "Invalid options[:sort_option]"
                end

  service_group = options[:group]


  params = {
    'hoststatustype' => options[:hoststatustype] || 15,
    'servicestatustypes' => options[:servicestatustypes] || service_status_type,
    'sorttype' => options[:sorttype] || sort_type,
    'sortoption' => options[:sortoption] || sort_option,
    'hoststatustypes' => options[:hoststatustypes],
    'serviceprops' => options[:serviceprops]
  }

  if @version == 3
    params['servicegroup'] = service_group || 'all'
    params['style'] = 'detail'
  else
    if service_group
      params['servicegroup'] = service_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

#status_urlObject



315
316
317
# File 'lib/nagiosharder.rb', line 315

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

#unacknowledge_service(host, service) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/nagiosharder.rb', line 62

def unacknowledge_service(host, service)
  request = {
    :cmd_typ => 52,
    :cmd_mod => 2,
    :host => host,
    :service => service
  }

  response = post(cmd_url, :body => request)
  response.code == 200 && response.body =~ /successful/
end