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