source: trunk/nagios-velvice/velvice.cgi @ 245

Last change on this file since 245 was 245, checked in by g7moreau, 6 years ago
  • First try to make remote command more generic...
File size: 11.7 KB
Line 
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
13
14use strict;
15use warnings;
16use version; our $VERSION = version->declare('0.5.1');
17
18use CGI;
19use HTML::Entities ();
20use Nagios::StatusLog;
21use URI::Encode qw(uri_encode uri_decode);
22use Color::Calc ();
23use YAML::Syck;
24
25my $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
38my $query = CGI->new();
39
40my $log = Nagios::StatusLog->new(
41   Filename => $config->{'status-file'},
42   Version  => 3.0
43   );
44
45my $check = uri_decode($query->param('check'));
46
47sub hostmapping {
48   my $host = shift;
49
50   return exists $config->{'mapping'}{$host} ? $config->{'mapping'}{$host} : $host;
51   }
52
53sub downtime {
54   my ($time_change) = @_;
55
56   my $now = time;
57   return sprintf '%.1f', ($now - $time_change) / (60 * 3600);
58   }
59
60sub 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
71my %hostdown;
72my @serviceproblems;
73my %hostcount;
74my @futurecheck;
75HOST:
76for 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
96my $now = time;
97my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $now;
98$year += 1900;
99$mon++;
100my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min;
101
102my $htmlpage = <<"ENDH";
103Content-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 */
113h1 ul {
114   display: flex;
115   justify-content: space-between;
116   }
117h1 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>
128ENDH
129
130my %service_name  = ();
131my %service_level = ();
132for my $srv (@serviceproblems) {
133   $service_name{$srv->service_description}++;
134   $service_level{$srv->status}++;
135   }
136
137if (scalar @serviceproblems == 0) {
138   $htmlpage .= "<p>No alert to recheck.</p>\n";
139   }
140else {
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 %cmdafter      = ();
160   my $after;
161
162   my $current_host  = '';
163   $htmlpage .= "<table border=\"1\">\n";
164   for my $srv (@serviceproblems) {
165      my $hostname = $srv->host_name;
166      my $service  = $srv->service_description;
167      my $status   = $srv->status;
168      my $downtime = downtime($srv->last_state_change);
169      my $output   = HTML::Entities::encode($srv->plugin_output);
170      $output =~ s/^[A-Z_\s]+?[:-]//;
171
172      my $color = $status eq 'CRITICAL' ? '#F88888' : '#FFFF00';
173      $color = alertcolor($color, $downtime);
174      $htmlpage .= " <tr style='background:$color;'>\n";
175      if ($hostname ne $current_host) {
176         $current_host = $hostname;
177         $htmlpage .= "  <td rowspan='$hostcount{$hostname}' style='vertical-align:middle;'><a href=\"$config->{'status-cgi'}?host=$hostname\">$hostname</a></td>\n";
178         }
179
180      my $bold;
181      for my $srv_name (keys %{$config->{'service'}}) {
182         my $srv_regex = $config->{'service'}{$srv_name}{'regex'};
183         $bold++ if $service =~ m/$srv_regex/ and $config->{'service'}{$srv_name}{'style'} eq 'bold';
184         }
185      $htmlpage .= "  <td>";
186      $htmlpage .= "<b>" if $bold;
187      $htmlpage .= "$service";
188      $htmlpage .= "</b>" if $bold;
189      $htmlpage .= "</td>\n";
190
191      $htmlpage .= "  <td>$status</td>\n";
192      $htmlpage .= "  <td style='max-width:60%;'><small>$output";
193
194      $sshdown{$hostname}++ if $service eq 'SSH';
195
196      if (($check =~ m/all/i)
197            or ($check =~ m/^$service$/i)
198            or ($check =~ m/critical/i and $status eq 'CRITICAL')
199            or ($check =~ m/warning/i  and $status eq 'WARNING')
200            or ($check =~ m/pending/i  and $status eq 'PENDING')
201            ) {
202         $now++;
203         my $interval = $srv->next_check() - $srv->last_check() || 300;
204         $interval =  240 if $interval <  240;
205         $interval = 3000 if $interval > 3000;
206         my $future = $now + 20 + int(rand($interval - 20)); # 5 * 60 = 300
207
208         $htmlpage .= " -- <b>CHECK</b> [$now/" . ($future - $now) . "]";
209         printf $nagios_cmd "[%lu] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%lu\n", $now, $hostname, $service, $now;
210         # delay future command
211         push @futurecheck, sprintf "[%lu] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%lu", $future, $hostname, $service, $future;
212         }
213
214      push @aptuptodate, $hostname if $service eq 'APT UPTODATE';
215      push @oomkiller,   $hostname if $service eq 'OOM Killer' and $status ne 'PENDING';
216      for my $srv_name (keys %{$config->{'service'}}) {
217         my $srv_regex  = $config->{'service'}{$srv_name}{'regex'};
218         my $srv_status = $config->{'service'}{$srv_name}{'status'};
219         if ($service =~ m/$srv_regex/ and ($srv_status eq 'ALL' or $status =~ m/$srv_status/)) {
220            $cmdafter{$srv_name} ||= [];
221            push @{$cmdafter{$srv_name}}, $hostname;
222            $after++;
223            }
224         }
225
226      $htmlpage .= "</small></td>\n";
227      $htmlpage .= "  <td style='text-align:right;'>$downtime days</td>\n";
228      $htmlpage .= " </tr>\n";
229      }
230
231   $htmlpage .= "</table>\n";
232   close $nagios_cmd;
233
234   if (%hostdown) {
235      $htmlpage .= "<br />\n";
236      $htmlpage .= "<table border='1'>\n";
237      for my $host (sort keys %hostdown) {
238         my $host_stat = $hostdown{$host};
239         my $hostname = $host_stat->host_name;
240         my $downtime = downtime($host_stat->last_state_change);
241         my $color = alertcolor('#F88888', $downtime);
242         $htmlpage .= " <tr style='background:$color'>\n";
243         $htmlpage .= "  <td><a href=\"$config->{'status-cgi'}?host=$hostname\">$hostname</a></td>\n";
244         my @host_service;
245         for my $srv ($log->list_services_on_host($host)) {
246            push @host_service, $log->service($host, $srv)->service_description;
247            }
248         $htmlpage .= "  <td><small>" . join(', ', @host_service) . "</small></td>\n";
249         $htmlpage .= "  <td style='text-align:right;'>$downtime days</td>\n";
250         $htmlpage .= " </tr>\n";
251         }
252      $htmlpage .= "</table>\n";
253      }
254
255   if (@oomkiller or @aptuptodate or $after) {
256      require Nagios::Object::Config;
257      my $parser = Nagios::Object::Config->new();
258      $parser->parse("/var/cache/nagios3/objects.cache");
259
260      for my $srv_name (keys %cmdafter) {
261         my @action = grep !exists $sshdown{$_}, @{$cmdafter{$srv_name}};
262         if (@action) {
263            $htmlpage .= "<h2>Action: $srv_name</h2>\n";
264            $htmlpage .= "<pre>\n";
265            my $remote_action = $config->{'service'}{$srv_name}{'command'};
266            $remote_action = $config->{'service'}{$srv_name}{'command-one'} if @action == 1;
267            my @hosts;
268            for my $host (@action) {
269               my $object = $parser->find_object("$host", "Nagios::Host");
270               push @hosts, hostmapping($object->address =~ s/\..*$//r);
271               }
272            my $hosts_list = join ' ', @hosts;
273            $htmlpage .= $remote_action =~ s{\%m}{$hosts_list}r;
274            $htmlpage .= "</pre>\n";
275            }
276         }
277
278      @oomkiller = grep !exists $sshdown{$_}, @oomkiller;
279      if (@oomkiller) {
280         $htmlpage .= "<h2>OOM Killer</h2>\n";
281         $htmlpage .= "<pre>\n";
282         if (@oomkiller == 1) {
283            $htmlpage .= " ssh";
284            }
285         else {
286            $htmlpage .= " tssh -c 'sudo rm /var/lib/nagios3/nagios_oom_killer.log'";
287            }
288         for my $host (@oomkiller) {
289            my $object = $parser->find_object("$host", "Nagios::Host");
290            $htmlpage .= ' ' . hostmapping($object->address =~ s/\..*$//r);
291            }
292         $htmlpage .= " 'sudo rm /var/lib/nagios3/nagios_oom_killer.log'" if (@oomkiller == 1);
293         $htmlpage .= "</pre>\n";
294         }
295
296      @aptuptodate = grep !exists $sshdown{$_}, @aptuptodate;
297      if (@aptuptodate) {
298         $htmlpage .= "<h2>APT UPTODATE</h2>\n";
299         $htmlpage .= "<pre>\n";
300         if (@aptuptodate == 1) {
301            $htmlpage .= " ssh";
302            }
303         else {
304            $htmlpage .= " tssh";
305            }
306         for my $host (@aptuptodate) {
307            my $object = $parser->find_object("$host", "Nagios::Host");
308            $htmlpage .= ' ' . hostmapping($object->address =~ s/\..*$//r);
309            }
310         $htmlpage .= "</pre>\n";
311         }
312      }
313   }
314
315$htmlpage .= <<'ENDH';
316</body>
317</html>
318ENDH
319
320print $htmlpage;
321
322# delay future check
323if (@futurecheck) {
324   sleep 2;
325   my $nagios_cmd;
326   open $nagios_cmd, '>>', $config->{'nagios-cmd'} or die "Can't open file filename: $!";
327   print $nagios_cmd "$_\n" for @futurecheck;
328   close $nagios_cmd;
329   }
330
331__END__
332
333
334=head1 NAME
335
336velvice.cgi - nagios velvice alert panel
337
338
339=head1 DESCRIPTION
340
341Nagios VELVICE is an acronym for "Nagios leVEL serVICE status".
342Homepage: http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/wiki/SoftWare/NagiosVelvice
343
344=head1 AUTHORS
345
346Written by Gabriel Moreau - Grenoble - France
347
348
349=head1 LICENSE AND COPYRIGHT
350
351Licence GNU GPL version 2 or later and Perl equivalent
352
353Copyright (C) 2014-2018 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>.
Note: See TracBrowser for help on using the repository browser.