Class: NOMS::Nagui

Inherits:
HttpClient show all
Defined in:
lib/noms/nagui.rb

Constant Summary collapse

@@validtypes =
['hosts','services','hostgroups','servicegroups','contacts','commands',
'downtimes','timeperiods','status','contactgroups']

Instance Method Summary collapse

Methods inherited from HttpClient

#allow_partial_updates, #allow_put_to_create, #default_content_type, #handle_mock, #ignore_content_type, #initialize, #ltrim, #method_missing, mock!, mockery, #myconfig, #opt, #rtrim, #trim

Constructor Details

This class inherits a constructor from NOMS::HttpClient

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class NOMS::HttpClient

Instance Method Details

#ack_host(host, user, comment) ⇒ Object



179
180
181
182
# File 'lib/noms/nagui.rb', line 179

def ack_host(host,user,comment)
  cmd="COMMAND [#{Time.now.to_i}] ACKNOWLEDGE_HOST_PROBLEM;#{host};0;1;1;#{user};#{comment}"
  process_command(cmd)
end

#ack_service(host, service, user, comment) ⇒ Object



184
185
186
187
# File 'lib/noms/nagui.rb', line 184

def ack_service(host,service,user,comment)
  cmd="COMMAND [#{Time.now.to_i}] ACKNOWLEDGE_SVC_PROBLEM;#{host};#{service};0;1;1;#{user};#{comment}"
  process_command(cmd)
end

#calc_time(str) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/noms/nagui.rb', line 122

def calc_time(str)
  case str
  when /(\d+)m/
    #minutes
    $1.to_i * 60
  when /(\d+)h/
    #hours
    $1.to_i * 3600
  when /(\d+)d/
    #days
    $1.to_i * 86400
  else
    str.to_i
  end
end

#check_host_online(host) ⇒ Object



201
202
203
204
205
206
207
208
209
# File 'lib/noms/nagui.rb', line 201

def check_host_online(host)
  @opt['host_up_command'] = 'check-host-alive' unless @opt.has_key?('host_up_command')
  nagcheck=do_request(:GET => "/nagcheck/command/#{host}/#{@opt['host_up_command']}")
  if nagcheck['state'] == 0
    true
  else
    false
  end
end

#comment(host, service, user, comment) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/noms/nagui.rb', line 170

def comment(host,service,user,comment)
  if service == nil
    cmd="COMMAND [#{Time.now.to_i}] ADD_HOST_COMMENT;#{host};1;#{user};#{comment}"
  else
    cmd="COMMAND [#{Time.now.to_i}] ADD_SVC_COMMENT;#{host};#{service};1;#{user};#{comment}"
  end
  process_command(cmd)
end

#config_keyObject

def initialize(opt)

@opt = opt
self.dbg "Initialized with options: #{opt.inspect}"

end



41
42
43
# File 'lib/noms/nagui.rb', line 41

def config_key
  'nagui'
end

#dbg(msg) ⇒ Object



30
31
32
33
34
# File 'lib/noms/nagui.rb', line 30

def dbg(msg)
    if @opt.has_key? 'debug' and @opt['debug'] > 2
        puts "DBG(#{self.class}): #{msg}"
    end
end

#default_query_key(type) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/noms/nagui.rb', line 49

def default_query_key(type)
  case type
  when 'host'
    'name'
  when 'service'
    'description'
  when 'hostgroup'
    'name'
  end
end

#downtime_host(host, length, user, comment) ⇒ Object



142
143
144
145
146
147
# File 'lib/noms/nagui.rb', line 142

def downtime_host(host,length,user,comment)
  starttime=Time.now.to_i
  endtime = starttime + calc_time(length)
  cmd="COMMAND [#{Time.now.to_i}] SCHEDULE_HOST_DOWNTIME;#{host};#{starttime};#{endtime};1;0;0;#{user};#{comment}"
  process_command(cmd)
end

#downtime_service(host, service, length, user, comment) ⇒ Object



148
149
150
151
152
153
# File 'lib/noms/nagui.rb', line 148

def downtime_service(host,service,length,user,comment)
  starttime=Time.now.to_i
  endtime = starttime + calc_time(length)
  cmd="COMMAND [#{Time.now.to_i}] SCHEDULE_SVC_DOWNTIME;#{host};#{service};#{starttime};#{endtime};1;0;0;#{user};#{comment}"
  process_command(cmd)
end

#host(hostname) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/noms/nagui.rb', line 112

def host(hostname)
  lql = "GET hosts|Filter: name = #{hostname}"
  results = do_request(:GET => '/nagui/nagios_live.cgi', :query => "query=#{CGI.escape(lql)}")
  if results.kind_of?(Array) && results.length > 0
    results[0]
  else
    nil
  end
end

#hostgroup(name) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/noms/nagui.rb', line 85

def hostgroup(name)
  lql = "GET hostgroups|Filter: name = #{name}"
  results = do_request(:GET => '/nagui/nagios_live.cgi', :query => "query=#{CGI.escape(lql)}")
  if results.kind_of?(Array) && results.length > 0
    results[0]
  else
    nil
  end
end

#make_lql(type, queries) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/noms/nagui.rb', line 60

def make_lql(type,queries)
  if !queries.kind_of?(Array)
    queries=[queries]
  end
  lql='GET ' 
  lql << make_plural(type) 
  queries.each do |q|
    query = /(\w+)([!~>=<]+)(.*)/.match(q)
    if query == nil
      lql << "|Filter: #{default_query_key(type)} ~~ #{q}"
    else
      lql << "|Filter: #{query[1]} #{query[2]} #{query[3]}"
    end
  end
  lql
end

#make_plural(str) ⇒ Object



45
46
47
# File 'lib/noms/nagui.rb', line 45

def make_plural(str)
  "#{str}s"
end

#nagcheck_host(host) ⇒ Object



189
190
191
192
193
# File 'lib/noms/nagui.rb', line 189

def nagcheck_host(host)
  url = "/nagcheck/host/#{host}"
  dbg("nagcheck url= #{url}")
  nagcheck=do_request(:GET => url, :query => "report=true")
end

#nagcheck_service(host, service) ⇒ Object



195
196
197
198
199
# File 'lib/noms/nagui.rb', line 195

def nagcheck_service(host,service)
  url = "/nagcheck/service/#{host}/#{service}"
  dbg("nagcheck url= #{url}")
  nagcheck=do_request(:GET => url, :query => "report=true")
end

#process_command(cmd) ⇒ Object



138
139
140
# File 'lib/noms/nagui.rb', line 138

def process_command(cmd)
  do_request(:POST => '/nagui/nagios_live.cgi', :body => "query=#{CGI.escape(cmd)}", :content_type => 'application/x-www-form-urlencoded')
end

#query(type, queries) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/noms/nagui.rb', line 77

def query(type,queries)
  unless @@validtypes.include? make_plural(type)
    puts "#{type} is not a valid type"
    Process.exit(1)
  end
  query_string = make_lql(type,queries)
  results = do_request(:GET => '/nagui/nagios_live.cgi', :query => "query=#{CGI.escape(query_string)}")
end

#service(host, description) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/noms/nagui.rb', line 94

def service(host,description)
  lql = "GET services|Filter: host_name = #{host}|Filter: description = #{description}"
  results = do_request(:GET => '/nagui/nagios_live.cgi', :query => "query=#{CGI.escape(lql)}")
  if results.kind_of?(Array) && results.length > 0
    results[0]
  else
    nil
  end
end

#servicegroup(name) ⇒ Object



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

def servicegroup(name)
  lql = "query=GET hosts|Filter: name = #{name}"
  results = do_request(:GET => '/nagui/nagios_live.cgi', :query => "query=#{CGI.escape(lql)}")
  if results.kind_of?(Array) && results.length > 0
    results[0]
  else
    nil
  end
end

#undowntime(host, service = nil) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/noms/nagui.rb', line 154

def undowntime(host,service=nil)
  if service == nil
    host_record = host(host)
    host_record['downtimes'].each do |id|
      cmd = "COMMAND [#{Time.now.to_i}] DEL_HOST_DOWNTIME;#{id}"
      process_command(cmd)
    end    
  else
    service_record = service(host,service)
    service_record['downtimes'].each do |id|
      cmd = "COMMAND [#{Time.now.to_i}] DEL_SVC_DOWNTIME;#{id}"  
      process_command(cmd)
    end  
  end
end