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
# 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
  }

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

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



134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/nagiosharder.rb', line 134

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



305
306
307
# File 'lib/nagiosharder.rb', line 305

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

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



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/nagiosharder.rb', line 256

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

  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



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/nagiosharder.rb', line 276

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

  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



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

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

#host_status(host) ⇒ Object



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

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



147
148
149
150
151
152
153
154
155
156
# File 'lib/nagiosharder.rb', line 147

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



102
103
104
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
# File 'lib/nagiosharder.rb', line 102

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



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/nagiosharder.rb', line 158

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



71
72
73
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
# File 'lib/nagiosharder.rb', line 71

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)


296
297
298
# File 'lib/nagiosharder.rb', line 296

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

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



170
171
172
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
# File 'lib/nagiosharder.rb', line 170

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' => 15,
    'servicestatustype' => service_status_type,
    'host' => 'all'
  }


  params = if @version == 3
             [ "servicegroup=all", "style=detail" ]
           else
             if service_group
               ["servicegroup=#{service_group}", "style=detail"]
             else
               ["host=all"]
             end
           end
  params += [
              service_status_type ? "servicestatustypes=#{service_status_type}" : nil,
              sort_type ? "sorttype=#{sort_type}" : nil,
              sort_option ? "sortoption=#{sort_option}" : nil,
              "hoststatustypes=15"
            ]
  query = params.compact.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



301
302
303
# File 'lib/nagiosharder.rb', line 301

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

#unacknowledge_service(host, service) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nagiosharder.rb', line 59

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