forked from ripienaar/ruby-nagios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest-status-server.rb
63 lines (47 loc) · 1.56 KB
/
rest-status-server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
56
57
58
59
60
61
62
63
#!/usr/bin/env ruby
require 'rubygems'
require 'sinatra'
require 'json'
require 'nagios/status.rb'
STATUS_FILE = "status.dat"
get '/:host' do
nagios = Nagios::Status.new
begin
nagios.parsestatus(STATUS_FILE)
rescue
[500, "Unable to parse service status! Contact support."]
end
begin
services = nagios.status["hosts"][params[:host]]["servicestatus"]
service_statuses = Array.new
services.each do |service|
status_hash = { :name => service[1]["service_description"],
:state => service[1]["current_state"],
:detail => service[1]["plugin_output"],
:timestamp => service[1]["last_check"] }
service_statuses.push status_hash
end
return service_statuses.to_json
rescue
[404, "This host does not exist!"]
end
end
get '/:host/:service/status' do
nagios = Nagios::Status.new
begin
nagios.parsestatus(STATUS_FILE)
rescue
[500, "Unable to parse service status! Contact support."]
end
begin
state = nagios.status["hosts"][params[:host]]["servicestatus"][params[:service]]["current_state"]
detail = nagios.status["hosts"][params[:host]]["servicestatus"][params[:service]]["plugin_output"]
timestamp = nagios.status["hosts"][params[:host]]["servicestatus"][params[:service]]["last_check"]
statusobject = {:service => params[:service], :state => state, :timestamp => timestamp, :detail => detail}
return statusobject.to_json
rescue
[404, "This service does not exist!"]
end
end
def fetch_nagios_data
end