#!/usr/bin/env perl # # 2014/05/15 Gabriel Moreau # 2017/06/22 Gabriel Moreau - big update # # velvice.cgi # Copyright (C) 2014-2018, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France # # Need NagiosStatus http://exchange.nagios.org/directory/Addons/APIs/Perl/NagiosStatus-2Epm/details # Possible command http://old.nagios.org/developerinfo/externalcommands/commandlist.php # # apt-get install perl-modules libnagios-object-perl libhtml-parser-perl liburi-encode-perl libcolor-calc-perl libyaml-syck-perl use strict; use warnings; use version; our $VERSION = version->declare('0.5.4'); use CGI; use HTML::Entities (); use Nagios::StatusLog; use URI::Encode qw(uri_encode uri_decode); use Color::Calc (); use YAML::Syck; my $config = {}; $config = YAML::Syck::LoadFile('/etc/nagios3/velvice.yml') if -e '/etc/nagios3/velvice.yml'; $config->{'status-file'} ||= '/var/cache/nagios3/status.dat'; $config->{'nagios-cmd'} ||= '/var/lib/nagios3/rw/nagios.cmd'; $config->{'portal-url'} ||= 'http://localhost/nagios3/'; $config->{'status-cgi'} ||= 'http://localhost/cgi-bin/nagios3/status.cgi'; $config->{'mapping'} ||= {}; $config->{'downtime'} ||= {}; $config->{'downtime'}{'min'} ||= 3; $config->{'downtime'}{'max'} ||= 50; $config->{'downtime'}{'factor'} ||= 0.7; $config->{'action'} ||= {}; my $query = CGI->new(); my $log = Nagios::StatusLog->new( Filename => $config->{'status-file'}, Version => 3.0 ); my $check = uri_decode($query->param('check')); my $script_name = $query->script_name(); sub hostmapping { my $host = shift; return exists $config->{'mapping'}{$host} ? $config->{'mapping'}{$host} : $host; } sub downtime { my ($time_change) = @_; my $now = time; return sprintf '%.1f', ($now - $time_change) / (60 * 3600); } sub alertcolor { my ($color, $downtime) = @_; $downtime = $downtime - $config->{'downtime'}{'min'}; # same color first days $downtime = $config->{'downtime'}{'max'} if $downtime > $config->{'downtime'}{'max'}; # max 50 days for color $downtime = 0 if $downtime < 0; my $factor = ($downtime * $config->{'downtime'}{'factor'}) / $config->{'downtime'}{'max'}; return Color::Calc::color_light_html($color, $factor); } my %hostdown; my @serviceproblems; my %hostcount; my @futurecheck; HOST: for my $host (sort $log->list_hosts()) { my $host_stat = $log->host($host); if ($host_stat->status eq 'DOWN') {TESTIF:{ for my $srv ($log->list_services_on_host($host)) { last TESTIF if $log->service($host, $srv)->status eq 'OK' or $log->service($host, $srv)->status eq 'PENDING'; } $hostdown{$host} = $host_stat; next HOST; }} for my $srv ($log->list_services_on_host($host)) { if ($log->service($host, $srv)->status ne 'OK') { push @serviceproblems, $log->service($host, $srv); $hostcount{$host}++; } } } my $now = time; my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $now; $year += 1900; $mon++; my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; my $htmlpage = <<"ENDH"; Content-Type: text/html Nagios Velvice

ENDH my %service_name = (); my %service_status = (); for my $srv (@serviceproblems) { $service_name{$srv->service_description}++; $service_status{$srv->status}++; } if (scalar @serviceproblems == 0) { $htmlpage .= "

No alert to recheck.

\n"; } else { $htmlpage .= "

Alert to recheck - Level:\n"; $htmlpage .= join ",\n", " ALL(" . scalar(@serviceproblems) . ')', map(" $_($service_status{$_})", sort keys %service_status); $htmlpage .= ".\n"; $htmlpage .= "
\n"; $htmlpage .= " Service:\n"; $htmlpage .= join ",\n", map(" $_($service_name{$_})", sort keys %service_name); $htmlpage .= ".\n"; $htmlpage .= "

\n"; my $nagios_cmd; open $nagios_cmd, '>>', $config->{'nagios-cmd'} or die "Can't open file filename: $!"; my %remote_sshdown = (); my %remote_db = (); my $remote_flag; my $current_host = ''; $htmlpage .= "\n"; SERVICE_PROBLEMS: for my $srv (@serviceproblems) { my $hostname = $srv->host_name; my $service = $srv->service_description; my $status = $srv->status; my $downtime = downtime($srv->last_state_change); my $output = HTML::Entities::encode($srv->plugin_output); $output =~ s/^[A-Z_\s]+?[:-]//; my $color = $status eq 'CRITICAL' ? '#F88888' : '#FFFF00'; $color = alertcolor($color, $downtime); $htmlpage .= " \n"; if ($hostname ne $current_host) { $current_host = $hostname; $htmlpage .= " \n"; } my $bold; ACTION_STYLE: for my $act_name (keys %{$config->{'action'}}) { my $act_regex = $config->{'action'}{$act_name}{'regex'}; $bold++ if $service =~ m/$act_regex/ and $config->{'action'}{$act_name}{'style'} eq 'bold'; } $htmlpage .= $bold ? ' \n"; $htmlpage .= " \n"; $htmlpage .= " \n"; $htmlpage .= " \n"; $htmlpage .= " \n"; } $htmlpage .= "
{'status-cgi'}?host=$hostname\">$hostname' : ' '; $htmlpage .= "$service$status$output"; if (($check =~ m/all/i) or ($check =~ m/^$service$/i) or ($check =~ m/critical/i and $status eq 'CRITICAL') or ($check =~ m/warning/i and $status eq 'WARNING') or ($check =~ m/pending/i and $status eq 'PENDING') ) { $now++; my $interval = $srv->next_check() - $srv->last_check() || 300; $interval = 240 if $interval < 240; $interval = 3000 if $interval > 3000; my $future = $now + 20 + int(rand($interval - 20)); # 5 * 60 = 300 $htmlpage .= " -- CHECK [$now/" . ($future - $now) . "]"; printf $nagios_cmd "[%lu] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%lu\n", $now, $hostname, $service, $now; # delay future command push @futurecheck, sprintf "[%lu] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%lu", $future, $hostname, $service, $future; } ACTION_PUSH_AND_DEPEND: for my $act_name (keys %{$config->{'action'}}) { my $act_regex = $config->{'action'}{$act_name}{'regex'}; my $act_status = $config->{'action'}{$act_name}{'status'} || 'ALL'; my $act_depend = $config->{'action'}{$act_name}{'depend'} || 'SSH'; if ($service =~ m/$act_regex/ and ($act_status eq 'ALL' or $status =~ m/$act_status/)) { $remote_db{$act_name} ||= []; push @{$remote_db{$act_name}}, $hostname; $remote_flag++; } # check depend service otherwise $remote_sshdown{$act_depend} ||= {}; $remote_sshdown{$act_depend}->{$hostname}++ if $service =~ m/$act_depend/; } $htmlpage .= "$downtime days
\n"; close $nagios_cmd; # host down if (%hostdown) { $htmlpage .= "
\n"; $htmlpage .= "\n"; HOST_DOWN: for my $host (sort keys %hostdown) { my $host_stat = $hostdown{$host}; my $hostname = $host_stat->host_name; my $downtime = downtime($host_stat->last_state_change); my $color = alertcolor('#F88888', $downtime); $htmlpage .= " \n"; $htmlpage .= " \n"; my @host_service; for my $srv ($log->list_services_on_host($host)) { push @host_service, $log->service($host, $srv)->service_description; } $htmlpage .= " \n"; $htmlpage .= " \n"; $htmlpage .= " \n"; } $htmlpage .= "
{'status-cgi'}?host=$hostname\">$hostname" . join(', ', @host_service) . "$downtime days
\n"; } # remote action if ($remote_flag) { require Nagios::Object::Config; my $parser = Nagios::Object::Config->new(); $parser->parse("/var/cache/nagios3/objects.cache"); REMOTE_ACTION: for my $act_name (keys %remote_db) { my $act_depend = $config->{'action'}{$act_name}{'depend'} || 'SSH'; my @action = grep !exists $remote_sshdown{$act_depend}->{$_}, @{$remote_db{$act_name}}; if (@action) { my $srv_title = $config->{'action'}{$act_name}{'title'} || "Action: $act_name"; $htmlpage .= "

$srv_title

\n"; $htmlpage .= "
\n";
            my $remote_action = $config->{'action'}{$act_name}{'command'};
            $remote_action = $config->{'action'}{$act_name}{'command-one'} if @action == 1 and exists $config->{'action'}{$act_name}{'command-one'};
            my @hosts;
            for my $host (@action) {
               my $object = $parser->find_object("$host", "Nagios::Host");
               push @hosts, hostmapping($object->address =~ s/\..*$//r);
               }
            my $hosts_list = join ' ', @hosts;
            $htmlpage .= ' ' . $remote_action =~ s{\%m}{$hosts_list}r;
            $htmlpage .= "
\n"; } } } } $htmlpage .= <<'ENDH'; ENDH print $htmlpage; # delayed future check if (@futurecheck) { sleep 2; my $nagios_cmd; open $nagios_cmd, '>>', $config->{'nagios-cmd'} or die "Can't open file filename: $!"; print $nagios_cmd "$_\n" for @futurecheck; close $nagios_cmd; } __END__ =head1 NAME velvice.cgi - nagios velvice alert panel =head1 DESCRIPTION Nagios VELVICE is an acronym for "Nagios leVEL serVICE status". Homepage: http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/wiki/SoftWare/NagiosVelvice =head1 AUTHORS Written by Gabriel Moreau - Grenoble - France =head1 LICENSE AND COPYRIGHT Licence GNU GPL version 2 or later and Perl equivalent Copyright (C) 2014-2018 Gabriel Moreau .