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

Last change on this file since 254 was 254, checked in by g7moreau, 6 years ago
  • Change to action and make ssh depend generic
File size: 10.9 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 perl-modules libnagios-object-perl libhtml-parser-perl liburi-encode-perl libcolor-calc-perl libyaml-syck-perl
13
14use strict;
15use warnings;
16use version; our $VERSION = version->declare('0.5.4');
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->{'action'}              ||= {};
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'));
46my $script_name = $query->script_name();
47
48sub hostmapping {
49   my $host = shift;
50
51   return exists $config->{'mapping'}{$host} ? $config->{'mapping'}{$host} : $host;
52   }
53
54sub downtime {
55   my ($time_change) = @_;
56
57   my $now = time;
58   return sprintf '%.1f', ($now - $time_change) / (60 * 3600);
59   }
60
61sub 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
72my %hostdown;
73my @serviceproblems;
74my %hostcount;
75my @futurecheck;
76HOST:
77for 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
97my $now = time;
98my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $now;
99$year += 1900;
100$mon++;
101my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min;
102
103my $htmlpage = <<"ENDH";
104Content-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 */
114h1 ul {
115   display: flex;
116   justify-content: space-between;
117   }
118h1 li {
119   display: inline;
120   }
121td.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>
132ENDH
133
134my %service_name   = ();
135my %service_status = ();
136for my $srv (@serviceproblems) {
137   $service_name{$srv->service_description}++;
138   $service_status{$srv->status}++;
139   }
140
141if (scalar @serviceproblems == 0) {
142   $htmlpage .= "<p>No alert to recheck.</p>\n";
143   }
144else {
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_status{$_})", sort keys %service_status);
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 %remote_sshdown = ();
161   my %remote_db      = ();
162   my $remote_flag;
163
164   my $current_host  = '';
165   $htmlpage .= "<table border=\"1\">\n";
166   SERVICE_PROBLEMS:
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      ACTION_STYLE:
185      for my $act_name (keys %{$config->{'action'}}) {
186         my $act_regex = $config->{'action'}{$act_name}{'regex'};
187         $bold++ if $service =~ m/$act_regex/ and $config->{'action'}{$act_name}{'style'} eq 'bold';
188         }
189      $htmlpage .= $bold ? '  <td class="bold">' : '  <td>';
190      $htmlpage .= "$service</td>\n";
191
192      $htmlpage .= "  <td>$status</td>\n";
193      $htmlpage .= "  <td style='max-width:60%;'><small>$output";
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      ACTION_PUSH_AND_DEPEND:
214      for my $act_name (keys %{$config->{'action'}}) {
215         my $act_regex  = $config->{'action'}{$act_name}{'regex'};
216         my $act_status = $config->{'action'}{$act_name}{'status'} || 'ALL';
217         my $act_depend = $config->{'action'}{$act_name}{'depend'} || 'SSH';
218
219         if ($service =~ m/$act_regex/ and ($act_status eq 'ALL' or $status =~ m/$act_status/)) {
220            $remote_db{$act_name} ||= [];
221            push @{$remote_db{$act_name}}, $hostname;
222            $remote_flag++;
223            }
224
225         # check depend service otherwise
226         $remote_sshdown{$act_depend} ||= {};
227         $remote_sshdown{$act_depend}->{$hostname}++ if $service =~ m/$act_depend/;
228         }
229
230      $htmlpage .= "</small></td>\n";
231      $htmlpage .= "  <td style='text-align:right;'>$downtime days</td>\n";
232      $htmlpage .= " </tr>\n";
233      }
234
235   $htmlpage .= "</table>\n";
236   close $nagios_cmd;
237
238   # host down
239   if (%hostdown) {
240      $htmlpage .= "<br />\n";
241      $htmlpage .= "<table border='1'>\n";
242      HOST_DOWN:
243      for my $host (sort keys %hostdown) {
244         my $host_stat = $hostdown{$host};
245         my $hostname = $host_stat->host_name;
246         my $downtime = downtime($host_stat->last_state_change);
247         my $color = alertcolor('#F88888', $downtime);
248         $htmlpage .= " <tr style='background:$color'>\n";
249         $htmlpage .= "  <td><a href=\"$config->{'status-cgi'}?host=$hostname\">$hostname</a></td>\n";
250         my @host_service;
251         for my $srv ($log->list_services_on_host($host)) {
252            push @host_service, $log->service($host, $srv)->service_description;
253            }
254         $htmlpage .= "  <td><small>" . join(', ', @host_service) . "</small></td>\n";
255         $htmlpage .= "  <td style='text-align:right;'>$downtime days</td>\n";
256         $htmlpage .= " </tr>\n";
257         }
258      $htmlpage .= "</table>\n";
259      }
260
261   # remote action
262   if ($remote_flag) {
263      require Nagios::Object::Config;
264      my $parser = Nagios::Object::Config->new();
265      $parser->parse("/var/cache/nagios3/objects.cache");
266
267      REMOTE_ACTION:
268      for my $act_name (keys %remote_db) {
269         my $act_depend = $config->{'action'}{$act_name}{'depend'} || 'SSH';
270
271         my @action = grep !exists $remote_sshdown{$act_depend}->{$_}, @{$remote_db{$act_name}};
272         if (@action) {
273            my $srv_title = $config->{'action'}{$act_name}{'title'} || "Action: $act_name";
274            $htmlpage .= "<h2>$srv_title</h2>\n";
275            $htmlpage .= "<pre>\n";
276            my $remote_action = $config->{'action'}{$act_name}{'command'};
277            $remote_action = $config->{'action'}{$act_name}{'command-one'} if @action == 1 and exists $config->{'action'}{$act_name}{'command-one'};
278            my @hosts;
279            for my $host (@action) {
280               my $object = $parser->find_object("$host", "Nagios::Host");
281               push @hosts, hostmapping($object->address =~ s/\..*$//r);
282               }
283            my $hosts_list = join ' ', @hosts;
284            $htmlpage .= ' ' . $remote_action =~ s{\%m}{$hosts_list}r;
285            $htmlpage .= "</pre>\n";
286            }
287         }
288      }
289   }
290
291$htmlpage .= <<'ENDH';
292</body>
293</html>
294ENDH
295
296print $htmlpage;
297
298# delayed future check
299if (@futurecheck) {
300   sleep 2;
301   my $nagios_cmd;
302   open $nagios_cmd, '>>', $config->{'nagios-cmd'} or die "Can't open file filename: $!";
303   print $nagios_cmd "$_\n" for @futurecheck;
304   close $nagios_cmd;
305   }
306
307__END__
308
309
310=head1 NAME
311
312velvice.cgi - nagios velvice alert panel
313
314
315=head1 DESCRIPTION
316
317Nagios VELVICE is an acronym for "Nagios leVEL serVICE status".
318Homepage: http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/wiki/SoftWare/NagiosVelvice
319
320=head1 AUTHORS
321
322Written by Gabriel Moreau - Grenoble - France
323
324
325=head1 LICENSE AND COPYRIGHT
326
327Licence GNU GPL version 2 or later and Perl equivalent
328
329Copyright (C) 2014-2018 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>.
Note: See TracBrowser for help on using the repository browser.