forked from RECIA/naglight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naglight.rb
138 lines (119 loc) · 4.85 KB
/
naglight.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
64
65
66
67
68
69
70
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
101
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
133
134
135
136
137
138
require 'json'
require 'haml'
require 'sinatra'
require 'chronic'
require 'action_view'
require 'lib/helpers' # load our view helpers
require 'lib/utils'
require 'lib/ruby-mk-livestatus'
# You can override some defaults of ruby-mk-livestatus
# Respect the Array !
$mk_livestatus_socket_paths += ["/home/nagios/live"]
# true / false
$mk_livestatus_debug=false
require 'lib/mk-calls' # put some MK Livestatus calls in external file
include ActionView::Helpers::TextHelper
include ActionView::Helpers::UrlHelper # Need mail_to for auto_link
# Need to improve this...
before do
# number / short / end of "num_services_foo" key
@simple_states_list = [ [0, "OK", "OK"],
[1, "WARN", "WARN"],
[2, "CRIT", "CRIT"],
[3, "UNKN", "UNKNOWN"],
[4, "DEP", "PENDING"] ]
end
get '/' do
@title = "Index"
haml :index
end
get '/hosts' do
@title = "All Hosts"
@allhosts = get_mk({:table => "hosts"})
haml :"hosts/index"
end
get '/hosts/more/:host_name' do
@title = "Host #{params[:host_name]}"
extras_headers = "Filter: host_name = #{params[:host_name]}\n"
@host = get_mk({:table => "hosts", :extras_headers => extras_headers}).first
haml :"hosts/extended_infos"
end
get '/services' do
@title = "All Services"
group = "StatsGroupBy: host_name\n" # TODO: get it from params[:group_by]
@allservices = get_mk({:table => "services", :extras_headers => group})
haml :"services/index"
end
get '/services/hosts/:host_name' do
@title = "Services of #{params[:host_name]}"
filter = "Filter: host_name = #{params[:host_name]}\n"
@services = get_mk({:table => "services", :extras_headers => filter})
@allservices = @services
haml :"services/one_host"
end
get '/services/more/:host_name/:service_name' do
service_name = params[:service_name].gsub("+", " ")
@title = "Service #{service_name}"
filter = "Filter: host_name = #{params[:host_name]}\n"
filter << "Filter: display_name = #{service_name}\n"
@service = get_mk({:table => "services", :extras_headers => filter}).first
haml :"services/extended_infos"
end
get '/logs' do
@title = "Logs"
@from_date = Chronic.parse("today", :guess => false, :context => :past).first
extras_headers = "Filter: time >= #{@from_date.to_i}\n"
@logs = get_mk({:table => "log",
:extras_headers => extras_headers,
:columns => ['host_name', 'message', 'options', 'plugin_output', 'service_description', 'state', 'state_type', 'time', 'type']})
haml :"other/logs"
end
get '/contacts' do
@title = "Contacts"
@contacts = get_mk({:table => "contacts"})
haml :"other/contacts"
end
##################################
########## API STUFF ##########
##################################
get '/api/?' do
@title = "API Index"
haml :"api/index"
end
get '/api/get/allhosts' do
response.header['Content-type'] = 'application/x-javascript; charset=UTF-8'
return mk_array_to_hash(JSON.parse(get_mk_livestatus({:table => "hosts"}))).to_json
end
get '/api/get/host/:host_name' do
response.header['Content-type'] = 'application/x-javascript; charset=UTF-8'
extras_headers = "Filter: host_name = #{params[:host_name]}\n"
return mk_array_to_hash(JSON.parse(get_mk_livestatus({:table => "hosts", :extras_headers => extras_headers}))).first.to_json
end
get '/api/get/contacts' do
response.header['Content-type'] = 'application/x-javascript; charset=UTF-8'
return mk_array_to_hash(JSON.parse(get_mk_livestatus({:table => "contacts"}))).to_json
end
get '/api/get/services' do
response.header['Content-type'] = 'application/x-javascript; charset=UTF-8'
return mk_array_to_hash(JSON.parse(get_mk_livestatus({:table => "services"}))).to_json
end
get '/api/get/services/host/:host_name' do
response.header['Content-type'] = 'application/x-javascript; charset=UTF-8'
filter = "Filter: host_name = #{params[:host_name]}\n"
return mk_array_to_hash(JSON.parse(get_mk_livestatus({:table => "services", :extras_headers => filter}))).to_json
end
get '/api/get/services/more/:host_name/:service_name' do
response.header['Content-type'] = 'application/x-javascript; charset=UTF-8'
service_name = params[:service_name].gsub("+", " ")
filter = "Filter: host_name = #{params[:host_name]}\n"
filter << "Filter: display_name = #{service_name}\n"
return mk_array_to_hash(JSON.parse(get_mk_livestatus({:table => "services", :extras_headers => filter}))).first.to_json
end
get '/api/get/logs' do
response.header['Content-type'] = 'application/x-javascript; charset=UTF-8'
@from_date = Chronic.parse("today", :guess => false, :context => :past).first
extras_headers = "Filter: time >= #{@from_date.to_i}\n"
return mk_array_to_hash(JSON.parse(get_mk_livestatus({:table => "log",
:extras_headers => extras_headers,
:columns => ['host_name', 'message', 'options', 'plugin_output', 'service_description', 'state', 'state_type', 'time', 'type']}))).to_json
end