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.



18
19
20
21
22
23
24
25
26
# File 'lib/nagiosharder.rb', line 18

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
end

Instance Attribute Details

#default_cookiesObject

Returns the value of attribute default_cookies.



15
16
17
# File 'lib/nagiosharder.rb', line 15

def default_cookies
  @default_cookies
end

#default_optionsObject

Returns the value of attribute default_options.



15
16
17
# File 'lib/nagiosharder.rb', line 15

def default_options
  @default_options
end

#nagios_urlObject

Returns the value of attribute nagios_url.



15
16
17
# File 'lib/nagiosharder.rb', line 15

def nagios_url
  @nagios_url
end

#passwordObject

Returns the value of attribute password.



15
16
17
# File 'lib/nagiosharder.rb', line 15

def password
  @password
end

#userObject

Returns the value of attribute user.



15
16
17
# File 'lib/nagiosharder.rb', line 15

def user
  @user
end

#versionObject

Returns the value of attribute version.



15
16
17
# File 'lib/nagiosharder.rb', line 15

def version
  @version
end

Instance Method Details

#acknowledge_service(host, service, comment) ⇒ Object



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

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

#cmd_urlObject



209
210
211
# File 'lib/nagiosharder.rb', line 209

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

#host_status(host) ⇒ Object



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

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

FIXME need to confirm this functionality exists in nagios 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



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

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/nagiosharder.rb', line 56

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[:type] = case options[:type].to_sym
                   when :fixed then 1
                   when :flexible then 0
                   else 1 # default to fixed
                   end

  if request[:type] == 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



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/nagiosharder.rb', line 113

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

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



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/nagiosharder.rb', line 125

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


  params = {
    'hoststatustype' => 15,
    'servicestatustype' => service_status_type,
    'host' => 'all'
  }


  params = if @version == 3
             [ "servicegroup=all", "style=detail" ]
           else
             [ "host=all" ]
           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



205
206
207
# File 'lib/nagiosharder.rb', line 205

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

#unacknowledge_service(host, service) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/nagiosharder.rb', line 44

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