1 | #!/usr/bin/env perl |
---|
2 | # |
---|
3 | # 2014/05/15 Gabriel Moreau <Gabriel.Moreau@univ-grenoble-alpes.fr> |
---|
4 | # 2017/06/22 Gabriel Moreau - big update |
---|
5 | # |
---|
6 | # velvice.cgi |
---|
7 | # Copyright (C) 2014-2018, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
8 | # |
---|
9 | # Need NagiosStatus http://exchange.nagios.org/directory/Addons/APIs/Perl/NagiosStatus-2Epm/details |
---|
10 | # Possible command http://old.nagios.org/developerinfo/externalcommands/commandlist.php |
---|
11 | # |
---|
12 | # apt-get install perl-modules libnagios-object-perl libhtml-parser-perl liburi-encode-perl libcolor-calc-perl libyaml-syck-perl |
---|
13 | |
---|
14 | use strict; |
---|
15 | use warnings; |
---|
16 | use version; our $VERSION = version->declare('0.5.4'); |
---|
17 | |
---|
18 | use CGI; |
---|
19 | use HTML::Entities (); |
---|
20 | use Nagios::StatusLog; |
---|
21 | use URI::Encode qw(uri_encode uri_decode); |
---|
22 | use Color::Calc (); |
---|
23 | use YAML::Syck; |
---|
24 | |
---|
25 | my $config = {}; |
---|
26 | $config = YAML::Syck::LoadFile('/etc/nagios3/velvice.yml') if -e '/etc/nagios3/velvice.yml'; |
---|
27 | $config->{'status-file'} ||= '/var/cache/nagios3/status.dat'; |
---|
28 | $config->{'nagios-cmd'} ||= '/var/lib/nagios3/rw/nagios.cmd'; |
---|
29 | $config->{'portal-url'} ||= 'http://localhost/nagios3/'; |
---|
30 | $config->{'status-cgi'} ||= 'http://localhost/cgi-bin/nagios3/status.cgi'; |
---|
31 | $config->{'mapping'} ||= {}; |
---|
32 | $config->{'downtime'} ||= {}; |
---|
33 | $config->{'downtime'}{'min'} ||= 3; |
---|
34 | $config->{'downtime'}{'max'} ||= 50; |
---|
35 | $config->{'downtime'}{'factor'} ||= 0.7; |
---|
36 | $config->{'service'} ||= {}; |
---|
37 | |
---|
38 | my $query = CGI->new(); |
---|
39 | |
---|
40 | my $log = Nagios::StatusLog->new( |
---|
41 | Filename => $config->{'status-file'}, |
---|
42 | Version => 3.0 |
---|
43 | ); |
---|
44 | |
---|
45 | my $check = uri_decode($query->param('check')); |
---|
46 | my $script_name = $query->script_name(); |
---|
47 | |
---|
48 | sub hostmapping { |
---|
49 | my $host = shift; |
---|
50 | |
---|
51 | return exists $config->{'mapping'}{$host} ? $config->{'mapping'}{$host} : $host; |
---|
52 | } |
---|
53 | |
---|
54 | sub downtime { |
---|
55 | my ($time_change) = @_; |
---|
56 | |
---|
57 | my $now = time; |
---|
58 | return sprintf '%.1f', ($now - $time_change) / (60 * 3600); |
---|
59 | } |
---|
60 | |
---|
61 | sub alertcolor { |
---|
62 | my ($color, $downtime) = @_; |
---|
63 | |
---|
64 | $downtime = $downtime - $config->{'downtime'}{'min'}; # same color first days |
---|
65 | $downtime = $config->{'downtime'}{'max'} if $downtime > $config->{'downtime'}{'max'}; # max 50 days for color |
---|
66 | $downtime = 0 if $downtime < 0; |
---|
67 | |
---|
68 | my $factor = ($downtime * $config->{'downtime'}{'factor'}) / $config->{'downtime'}{'max'}; |
---|
69 | return Color::Calc::color_light_html($color, $factor); |
---|
70 | } |
---|
71 | |
---|
72 | my %hostdown; |
---|
73 | my @serviceproblems; |
---|
74 | my %hostcount; |
---|
75 | my @futurecheck; |
---|
76 | HOST: |
---|
77 | for my $host (sort $log->list_hosts()) { |
---|
78 | my $host_stat = $log->host($host); |
---|
79 | |
---|
80 | if ($host_stat->status eq 'DOWN') {TESTIF:{ |
---|
81 | for my $srv ($log->list_services_on_host($host)) { |
---|
82 | last TESTIF if $log->service($host, $srv)->status eq 'OK' or $log->service($host, $srv)->status eq 'PENDING'; |
---|
83 | } |
---|
84 | |
---|
85 | $hostdown{$host} = $host_stat; |
---|
86 | next HOST; |
---|
87 | }} |
---|
88 | |
---|
89 | for my $srv ($log->list_services_on_host($host)) { |
---|
90 | if ($log->service($host, $srv)->status ne 'OK') { |
---|
91 | push @serviceproblems, $log->service($host, $srv); |
---|
92 | $hostcount{$host}++; |
---|
93 | } |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | my $now = time; |
---|
98 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $now; |
---|
99 | $year += 1900; |
---|
100 | $mon++; |
---|
101 | my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; |
---|
102 | |
---|
103 | my $htmlpage = <<"ENDH"; |
---|
104 | Content-Type: text/html |
---|
105 | |
---|
106 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
---|
107 | <html lang="en"> |
---|
108 | <head> |
---|
109 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
---|
110 | <title>Nagios Velvice</title> |
---|
111 | </head> |
---|
112 | <style type="text/css"> |
---|
113 | /* https://stackoverflow.com/questions/14920401/how-to-simulate-hfill-with-html-and-css */ |
---|
114 | h1 ul { |
---|
115 | display: flex; |
---|
116 | justify-content: space-between; |
---|
117 | } |
---|
118 | h1 li { |
---|
119 | display: inline; |
---|
120 | } |
---|
121 | td.bold { |
---|
122 | font-weight: bold; |
---|
123 | } |
---|
124 | </style> |
---|
125 | <body> |
---|
126 | <h1> |
---|
127 | <ul> |
---|
128 | <li>Nagios Velvice Alert Panel : <a href="$config->{'portal-url'}">Core Server</a></li> |
---|
129 | <li><small>(<a href="$script_name">UPDATE</a> - $date)</small></li> |
---|
130 | </ul> |
---|
131 | </h1> |
---|
132 | ENDH |
---|
133 | |
---|
134 | my %service_name = (); |
---|
135 | my %service_level = (); |
---|
136 | for my $srv (@serviceproblems) { |
---|
137 | $service_name{$srv->service_description}++; |
---|
138 | $service_level{$srv->status}++; |
---|
139 | } |
---|
140 | |
---|
141 | if (scalar @serviceproblems == 0) { |
---|
142 | $htmlpage .= "<p>No alert to recheck.</p>\n"; |
---|
143 | } |
---|
144 | else { |
---|
145 | |
---|
146 | $htmlpage .= "<p>Alert to recheck - Level:\n"; |
---|
147 | $htmlpage .= join ",\n", |
---|
148 | " <a href='$script_name?check=all'>ALL</a><small>(" . scalar(@serviceproblems) . ')</small>', |
---|
149 | map(" <a href='$script_name?check=" . lc(uri_encode($_)) . "'>$_</a>($service_level{$_})", sort keys %service_level); |
---|
150 | $htmlpage .= ".\n"; |
---|
151 | $htmlpage .= " <br />\n"; |
---|
152 | $htmlpage .= " Service:\n"; |
---|
153 | $htmlpage .= join ",\n", map(" <a href='$script_name?check=" . lc(uri_encode($_)) . "'>$_</a><small>($service_name{$_})</small>", sort keys %service_name); |
---|
154 | $htmlpage .= ".\n"; |
---|
155 | $htmlpage .= "</p>\n"; |
---|
156 | |
---|
157 | my $nagios_cmd; |
---|
158 | open $nagios_cmd, '>>', $config->{'nagios-cmd'} or die "Can't open file filename: $!"; |
---|
159 | |
---|
160 | my %sshdown = (); |
---|
161 | my %cmdafter = (); |
---|
162 | my $after; |
---|
163 | |
---|
164 | my $current_host = ''; |
---|
165 | $htmlpage .= "<table border=\"1\">\n"; |
---|
166 | for my $srv (@serviceproblems) { |
---|
167 | my $hostname = $srv->host_name; |
---|
168 | my $service = $srv->service_description; |
---|
169 | my $status = $srv->status; |
---|
170 | my $downtime = downtime($srv->last_state_change); |
---|
171 | my $output = HTML::Entities::encode($srv->plugin_output); |
---|
172 | $output =~ s/^[A-Z_\s]+?[:-]//; |
---|
173 | |
---|
174 | my $color = $status eq 'CRITICAL' ? '#F88888' : '#FFFF00'; |
---|
175 | $color = alertcolor($color, $downtime); |
---|
176 | $htmlpage .= " <tr style='background:$color;'>\n"; |
---|
177 | if ($hostname ne $current_host) { |
---|
178 | $current_host = $hostname; |
---|
179 | $htmlpage .= " <td rowspan='$hostcount{$hostname}' style='vertical-align:middle;'><a href=\"$config->{'status-cgi'}?host=$hostname\">$hostname</a></td>\n"; |
---|
180 | } |
---|
181 | |
---|
182 | my $bold; |
---|
183 | for my $srv_name (keys %{$config->{'service'}}) { |
---|
184 | my $srv_regex = $config->{'service'}{$srv_name}{'regex'}; |
---|
185 | $bold++ if $service =~ m/$srv_regex/ and $config->{'service'}{$srv_name}{'style'} eq 'bold'; |
---|
186 | } |
---|
187 | $htmlpage .= $bold ? ' <td class="bold">' : ' <td>'; |
---|
188 | $htmlpage .= "$service</td>\n"; |
---|
189 | |
---|
190 | $htmlpage .= " <td>$status</td>\n"; |
---|
191 | $htmlpage .= " <td style='max-width:60%;'><small>$output"; |
---|
192 | |
---|
193 | $sshdown{$hostname}++ if $service eq 'SSH'; |
---|
194 | |
---|
195 | if (($check =~ m/all/i) |
---|
196 | or ($check =~ m/^$service$/i) |
---|
197 | or ($check =~ m/critical/i and $status eq 'CRITICAL') |
---|
198 | or ($check =~ m/warning/i and $status eq 'WARNING') |
---|
199 | or ($check =~ m/pending/i and $status eq 'PENDING') |
---|
200 | ) { |
---|
201 | $now++; |
---|
202 | my $interval = $srv->next_check() - $srv->last_check() || 300; |
---|
203 | $interval = 240 if $interval < 240; |
---|
204 | $interval = 3000 if $interval > 3000; |
---|
205 | my $future = $now + 20 + int(rand($interval - 20)); # 5 * 60 = 300 |
---|
206 | |
---|
207 | $htmlpage .= " -- <b>CHECK</b> [$now/" . ($future - $now) . "]"; |
---|
208 | printf $nagios_cmd "[%lu] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%lu\n", $now, $hostname, $service, $now; |
---|
209 | # delay future command |
---|
210 | push @futurecheck, sprintf "[%lu] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%lu", $future, $hostname, $service, $future; |
---|
211 | } |
---|
212 | |
---|
213 | for my $srv_name (keys %{$config->{'service'}}) { |
---|
214 | my $srv_regex = $config->{'service'}{$srv_name}{'regex'}; |
---|
215 | my $srv_status = $config->{'service'}{$srv_name}{'status'}; |
---|
216 | if ($service =~ m/$srv_regex/ and ($srv_status eq 'ALL' or $status =~ m/$srv_status/)) { |
---|
217 | $cmdafter{$srv_name} ||= []; |
---|
218 | push @{$cmdafter{$srv_name}}, $hostname; |
---|
219 | $after++; |
---|
220 | } |
---|
221 | } |
---|
222 | |
---|
223 | $htmlpage .= "</small></td>\n"; |
---|
224 | $htmlpage .= " <td style='text-align:right;'>$downtime days</td>\n"; |
---|
225 | $htmlpage .= " </tr>\n"; |
---|
226 | } |
---|
227 | |
---|
228 | $htmlpage .= "</table>\n"; |
---|
229 | close $nagios_cmd; |
---|
230 | |
---|
231 | if (%hostdown) { |
---|
232 | $htmlpage .= "<br />\n"; |
---|
233 | $htmlpage .= "<table border='1'>\n"; |
---|
234 | for my $host (sort keys %hostdown) { |
---|
235 | my $host_stat = $hostdown{$host}; |
---|
236 | my $hostname = $host_stat->host_name; |
---|
237 | my $downtime = downtime($host_stat->last_state_change); |
---|
238 | my $color = alertcolor('#F88888', $downtime); |
---|
239 | $htmlpage .= " <tr style='background:$color'>\n"; |
---|
240 | $htmlpage .= " <td><a href=\"$config->{'status-cgi'}?host=$hostname\">$hostname</a></td>\n"; |
---|
241 | my @host_service; |
---|
242 | for my $srv ($log->list_services_on_host($host)) { |
---|
243 | push @host_service, $log->service($host, $srv)->service_description; |
---|
244 | } |
---|
245 | $htmlpage .= " <td><small>" . join(', ', @host_service) . "</small></td>\n"; |
---|
246 | $htmlpage .= " <td style='text-align:right;'>$downtime days</td>\n"; |
---|
247 | $htmlpage .= " </tr>\n"; |
---|
248 | } |
---|
249 | $htmlpage .= "</table>\n"; |
---|
250 | } |
---|
251 | |
---|
252 | if ($after) { |
---|
253 | require Nagios::Object::Config; |
---|
254 | my $parser = Nagios::Object::Config->new(); |
---|
255 | $parser->parse("/var/cache/nagios3/objects.cache"); |
---|
256 | |
---|
257 | for my $srv_name (keys %cmdafter) { |
---|
258 | my @action = grep !exists $sshdown{$_}, @{$cmdafter{$srv_name}}; |
---|
259 | if (@action) { |
---|
260 | my $srv_title = $config->{'service'}{$srv_name}{'title'} || "Action: $srv_name"; |
---|
261 | $htmlpage .= "<h2>$srv_title</h2>\n"; |
---|
262 | $htmlpage .= "<pre>\n"; |
---|
263 | my $remote_action = $config->{'service'}{$srv_name}{'command'}; |
---|
264 | $remote_action = $config->{'service'}{$srv_name}{'command-one'} if @action == 1; |
---|
265 | my @hosts; |
---|
266 | for my $host (@action) { |
---|
267 | my $object = $parser->find_object("$host", "Nagios::Host"); |
---|
268 | push @hosts, hostmapping($object->address =~ s/\..*$//r); |
---|
269 | } |
---|
270 | my $hosts_list = join ' ', @hosts; |
---|
271 | $htmlpage .= ' ' . $remote_action =~ s{\%m}{$hosts_list}r; |
---|
272 | $htmlpage .= "</pre>\n"; |
---|
273 | } |
---|
274 | } |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | $htmlpage .= <<'ENDH'; |
---|
279 | </body> |
---|
280 | </html> |
---|
281 | ENDH |
---|
282 | |
---|
283 | print $htmlpage; |
---|
284 | |
---|
285 | # delay future check |
---|
286 | if (@futurecheck) { |
---|
287 | sleep 2; |
---|
288 | my $nagios_cmd; |
---|
289 | open $nagios_cmd, '>>', $config->{'nagios-cmd'} or die "Can't open file filename: $!"; |
---|
290 | print $nagios_cmd "$_\n" for @futurecheck; |
---|
291 | close $nagios_cmd; |
---|
292 | } |
---|
293 | |
---|
294 | __END__ |
---|
295 | |
---|
296 | |
---|
297 | =head1 NAME |
---|
298 | |
---|
299 | velvice.cgi - nagios velvice alert panel |
---|
300 | |
---|
301 | |
---|
302 | =head1 DESCRIPTION |
---|
303 | |
---|
304 | Nagios VELVICE is an acronym for "Nagios leVEL serVICE status". |
---|
305 | Homepage: http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/wiki/SoftWare/NagiosVelvice |
---|
306 | |
---|
307 | =head1 AUTHORS |
---|
308 | |
---|
309 | Written by Gabriel Moreau - Grenoble - France |
---|
310 | |
---|
311 | |
---|
312 | =head1 LICENSE AND COPYRIGHT |
---|
313 | |
---|
314 | Licence GNU GPL version 2 or later and Perl equivalent |
---|
315 | |
---|
316 | Copyright (C) 2014-2018 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>. |
---|