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

Last change on this file since 246 was 246, checked in by g7moreau, 6 years ago
  • Add title support and use css for bold style
File size: 11.8 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.bold td {
121   font-weight: bold;
122   }
123</style>
124<body>
125<h1>
126 <ul>
127   <li>Nagios Velvice Alert Panel : <a href="$config->{'portal-url'}">Core Server</a></li>
128   <li><small>(<a href='velvice.cgi'>UPDATE</a> - $date)</small></li>
129 </ul>
130</h1>
131ENDH
132
133my %service_name  = ();
134my %service_level = ();
135for my $srv (@serviceproblems) {
136   $service_name{$srv->service_description}++;
137   $service_level{$srv->status}++;
138   }
139
140if (scalar @serviceproblems == 0) {
141   $htmlpage .= "<p>No alert to recheck.</p>\n";
142   }
143else {
144
145   $htmlpage .= "<p>Alert to recheck - Level:\n";
146   $htmlpage .= join ",\n",
147      ' <a href="velvice.cgi?check=all">ALL</a><small>(' . scalar(@serviceproblems) . ')</small>',
148      map(" <a href='velvice.cgi?check=" . lc(uri_encode($_)) . "'>$_</a>($service_level{$_})", sort keys %service_level);
149   $htmlpage .= ".\n";
150   $htmlpage .= " <br />\n";
151   $htmlpage .= " Service:\n";
152   $htmlpage .= join ",\n", map(" <a href='velvice.cgi?check=" . lc(uri_encode($_)) . "'>$_</a><small>($service_name{$_})</small>", sort keys %service_name);
153   $htmlpage .= ".\n";
154   $htmlpage .= "</p>\n";
155
156   my $nagios_cmd;
157   open $nagios_cmd, '>>', $config->{'nagios-cmd'} or die "Can't open file filename: $!";
158
159   my @oomkiller     = ();
160   my %sshdown       = ();
161   my @aptuptodate   = ();
162   my %cmdafter      = ();
163   my $after;
164
165   my $current_host  = '';
166   $htmlpage .= "<table border=\"1\">\n";
167   for my $srv (@serviceproblems) {
168      my $hostname = $srv->host_name;
169      my $service  = $srv->service_description;
170      my $status   = $srv->status;
171      my $downtime = downtime($srv->last_state_change);
172      my $output   = HTML::Entities::encode($srv->plugin_output);
173      $output =~ s/^[A-Z_\s]+?[:-]//;
174
175      my $color = $status eq 'CRITICAL' ? '#F88888' : '#FFFF00';
176      $color = alertcolor($color, $downtime);
177      $htmlpage .= " <tr style='background:$color;'>\n";
178      if ($hostname ne $current_host) {
179         $current_host = $hostname;
180         $htmlpage .= "  <td rowspan='$hostcount{$hostname}' style='vertical-align:middle;'><a href=\"$config->{'status-cgi'}?host=$hostname\">$hostname</a></td>\n";
181         }
182
183      my $bold;
184      for my $srv_name (keys %{$config->{'service'}}) {
185         my $srv_regex = $config->{'service'}{$srv_name}{'regex'};
186         $bold++ if $service =~ m/$srv_regex/ and $config->{'service'}{$srv_name}{'style'} eq 'bold';
187         }
188      $htmlpage .= $bold ? '  <td class="bold">' : '  <td>';
189      $htmlpage .= "$service</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            my $srv_title = $config->{'service'}{$srv_name}{'title'} || $srv_name;
264            $htmlpage .= "<h2>Action: $srv_title</h2>\n";
265            $htmlpage .= "<pre>\n";
266            my $remote_action = $config->{'service'}{$srv_name}{'command'};
267            $remote_action = $config->{'service'}{$srv_name}{'command-one'} if @action == 1;
268            my @hosts;
269            for my $host (@action) {
270               my $object = $parser->find_object("$host", "Nagios::Host");
271               push @hosts, hostmapping($object->address =~ s/\..*$//r);
272               }
273            my $hosts_list = join ' ', @hosts;
274            $htmlpage .= ' ' . $remote_action =~ s{\%m}{$hosts_list}r;
275            $htmlpage .= "</pre>\n";
276            }
277         }
278
279      @oomkiller = grep !exists $sshdown{$_}, @oomkiller;
280      if (@oomkiller) {
281         $htmlpage .= "<h2>OOM Killer</h2>\n";
282         $htmlpage .= "<pre>\n";
283         if (@oomkiller == 1) {
284            $htmlpage .= " ssh";
285            }
286         else {
287            $htmlpage .= " tssh -c 'sudo rm /var/lib/nagios3/nagios_oom_killer.log'";
288            }
289         for my $host (@oomkiller) {
290            my $object = $parser->find_object("$host", "Nagios::Host");
291            $htmlpage .= ' ' . hostmapping($object->address =~ s/\..*$//r);
292            }
293         $htmlpage .= " 'sudo rm /var/lib/nagios3/nagios_oom_killer.log'" if (@oomkiller == 1);
294         $htmlpage .= "</pre>\n";
295         }
296
297      @aptuptodate = grep !exists $sshdown{$_}, @aptuptodate;
298      if (@aptuptodate) {
299         $htmlpage .= "<h2>APT UPTODATE</h2>\n";
300         $htmlpage .= "<pre>\n";
301         if (@aptuptodate == 1) {
302            $htmlpage .= " ssh";
303            }
304         else {
305            $htmlpage .= " tssh";
306            }
307         for my $host (@aptuptodate) {
308            my $object = $parser->find_object("$host", "Nagios::Host");
309            $htmlpage .= ' ' . hostmapping($object->address =~ s/\..*$//r);
310            }
311         $htmlpage .= "</pre>\n";
312         }
313      }
314   }
315
316$htmlpage .= <<'ENDH';
317</body>
318</html>
319ENDH
320
321print $htmlpage;
322
323# delay future check
324if (@futurecheck) {
325   sleep 2;
326   my $nagios_cmd;
327   open $nagios_cmd, '>>', $config->{'nagios-cmd'} or die "Can't open file filename: $!";
328   print $nagios_cmd "$_\n" for @futurecheck;
329   close $nagios_cmd;
330   }
331
332__END__
333
334
335=head1 NAME
336
337velvice.cgi - nagios velvice alert panel
338
339
340=head1 DESCRIPTION
341
342Nagios VELVICE is an acronym for "Nagios leVEL serVICE status".
343Homepage: http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/wiki/SoftWare/NagiosVelvice
344
345=head1 AUTHORS
346
347Written by Gabriel Moreau - Grenoble - France
348
349
350=head1 LICENSE AND COPYRIGHT
351
352Licence GNU GPL version 2 or later and Perl equivalent
353
354Copyright (C) 2014-2018 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>.
Note: See TracBrowser for help on using the repository browser.