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

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