[240] | 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 libhash-merge-perl |
---|
| 13 | |
---|
| 14 | use strict; |
---|
| 15 | use warnings; |
---|
| 16 | |
---|
| 17 | use CGI; |
---|
| 18 | use HTML::Entities (); |
---|
| 19 | use Nagios::StatusLog; |
---|
| 20 | use URI::Encode qw(uri_encode uri_decode); |
---|
| 21 | use Color::Calc (); |
---|
| 22 | use YAML::Syck; |
---|
| 23 | #use Hash::Merge; |
---|
| 24 | |
---|
| 25 | my $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 | |
---|
| 37 | my $query = CGI->new(); |
---|
| 38 | |
---|
| 39 | my $log = Nagios::StatusLog->new( |
---|
| 40 | Filename => $config->{'status-file'}, |
---|
| 41 | Version => 3.0 |
---|
| 42 | ); |
---|
| 43 | |
---|
| 44 | my $check = uri_decode($query->param('check')); |
---|
| 45 | |
---|
| 46 | sub hostmapping { |
---|
| 47 | my $host = shift; |
---|
| 48 | |
---|
| 49 | return exists $config->{'mapping'}{$host} ? $config->{'mapping'}{$host} : $host; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | sub downtime { |
---|
| 53 | my ($time_change) = @_; |
---|
| 54 | |
---|
| 55 | my $now = time; |
---|
| 56 | return sprintf '%.1f', ($now - $time_change) / (60 * 3600); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | sub alertcolor { |
---|
| 60 | my ($color, $downtime) = @_; |
---|
| 61 | |
---|
| 62 | $downtime = $downtime - $config->{'downtime'}{'min'}; # same color first days |
---|
| 63 | $downtime = $config->{'downtime'}{'max'} if $downtime > $config->{'downtime'}{'max'}; # max 50 days for color |
---|
| 64 | $downtime = 0 if $downtime < 0; |
---|
| 65 | |
---|
| 66 | my $factor = ($downtime * $config->{'downtime'}{'factor'}) / $config->{'downtime'}{'max'}; |
---|
| 67 | return Color::Calc::color_light_html($color, $factor); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | my %hostdown; |
---|
| 71 | my @serviceproblems; |
---|
| 72 | my %hostcount; |
---|
| 73 | my @futurecheck; |
---|
| 74 | HOST: |
---|
| 75 | for my $host (sort $log->list_hosts()) { |
---|
| 76 | my $host_stat = $log->host($host); |
---|
| 77 | |
---|
| 78 | if ($host_stat->status eq 'DOWN') {TESTIF:{ |
---|
| 79 | for my $srv ($log->list_services_on_host($host)) { |
---|
| 80 | last TESTIF if $log->service($host, $srv)->status eq 'OK' or $log->service($host, $srv)->status eq 'PENDING'; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | $hostdown{$host} = $host_stat; |
---|
| 84 | next HOST; |
---|
| 85 | }} |
---|
| 86 | |
---|
| 87 | for my $srv ($log->list_services_on_host($host)) { |
---|
| 88 | if ($log->service($host, $srv)->status ne 'OK') { |
---|
| 89 | push @serviceproblems, $log->service($host, $srv); |
---|
| 90 | $hostcount{$host}++; |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | my $now = time; |
---|
| 96 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $now; |
---|
| 97 | $year += 1900; |
---|
| 98 | $mon++; |
---|
| 99 | my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; |
---|
| 100 | |
---|
| 101 | my $htmlpage = <<"ENDH"; |
---|
| 102 | Content-Type: text/html |
---|
| 103 | |
---|
| 104 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
---|
| 105 | <html lang="en"> |
---|
| 106 | <head> |
---|
| 107 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
---|
| 108 | <title>Nagios Velvice</title> |
---|
| 109 | </head> |
---|
| 110 | <style type="text/css"> |
---|
| 111 | /* https://stackoverflow.com/questions/14920401/how-to-simulate-hfill-with-html-and-css */ |
---|
| 112 | h1 ul { |
---|
| 113 | display: flex; |
---|
| 114 | justify-content: space-between; |
---|
| 115 | } |
---|
| 116 | h1 li { |
---|
| 117 | display: inline; |
---|
| 118 | } |
---|
| 119 | </style> |
---|
| 120 | <body> |
---|
| 121 | <h1> |
---|
| 122 | <ul> |
---|
| 123 | <li>Nagios Velvice Alert Panel : <a href="$config->{'portal-url'}">Core Server</a></li> |
---|
| 124 | <li><small>(<a href='velvice.cgi'>UPDATE</a> - $date)</small></li> |
---|
| 125 | </ul> |
---|
| 126 | </h1> |
---|
| 127 | ENDH |
---|
| 128 | |
---|
| 129 | my %service_name = (); |
---|
| 130 | my %service_level = (); |
---|
| 131 | for my $srv (@serviceproblems) { |
---|
| 132 | $service_name{$srv->service_description}++; |
---|
| 133 | $service_level{$srv->status}++; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | if (scalar @serviceproblems == 0) { |
---|
| 137 | $htmlpage .= "<p>No alert to recheck.</p>\n"; |
---|
| 138 | } |
---|
| 139 | else { |
---|
| 140 | |
---|
| 141 | $htmlpage .= "<p>Alert to recheck - Level:\n"; |
---|
| 142 | $htmlpage .= join ",\n", |
---|
| 143 | ' <a href="velvice.cgi?check=all">ALL</a><small>(' . scalar(@serviceproblems) . ')</small>', |
---|
| 144 | map(" <a href='velvice.cgi?check=" . lc(uri_encode($_)) . "'>$_</a>($service_level{$_})", sort keys %service_level); |
---|
| 145 | $htmlpage .= ".\n"; |
---|
| 146 | $htmlpage .= " <br />\n"; |
---|
| 147 | $htmlpage .= " Service:\n"; |
---|
| 148 | $htmlpage .= join ",\n", map(" <a href='velvice.cgi?check=" . lc(uri_encode($_)) . "'>$_</a><small>($service_name{$_})</small>", sort keys %service_name); |
---|
| 149 | $htmlpage .= ".\n"; |
---|
| 150 | $htmlpage .= "</p>\n"; |
---|
| 151 | |
---|
| 152 | my $nagios_cmd; |
---|
| 153 | open $nagios_cmd, '>>', $config->{'nagios-cmd'} or die "Can't open file filename: $!"; |
---|
| 154 | |
---|
| 155 | my @oomkiller = (); |
---|
| 156 | my %sshdown = (); |
---|
| 157 | my @aptuptodate = (); |
---|
| 158 | my $current_host = ''; |
---|
| 159 | $htmlpage .= "<table border=\"1\">\n"; |
---|
| 160 | for my $srv (@serviceproblems) { |
---|
| 161 | my $hostname = $srv->host_name; |
---|
| 162 | my $service = $srv->service_description; |
---|
| 163 | my $level = $srv->status; |
---|
| 164 | my $downtime = downtime($srv->last_state_change); |
---|
| 165 | my $output = HTML::Entities::encode($srv->plugin_output); |
---|
| 166 | $output =~ s/^[A-Z_\s]+?[:-]//; |
---|
| 167 | |
---|
| 168 | my $color = $level eq 'CRITICAL' ? '#F88888' : '#FFFF00'; |
---|
| 169 | $color = alertcolor($color, $downtime); |
---|
| 170 | $htmlpage .= " <tr style='background:$color;'>\n"; |
---|
| 171 | if ($hostname ne $current_host) { |
---|
| 172 | $current_host = $hostname; |
---|
| 173 | $htmlpage .= " <td rowspan='$hostcount{$hostname}' style='vertical-align:middle;'><a href=\"$config->{'status-cgi'}?host=$hostname\">$hostname</a></td>\n"; |
---|
| 174 | } |
---|
| 175 | if ($service eq 'APT UPTODATE') { |
---|
| 176 | $htmlpage .= " <td><b>$service</b></td>\n"; |
---|
| 177 | } |
---|
| 178 | else { |
---|
| 179 | $htmlpage .= " <td>$service</td>\n"; |
---|
| 180 | } |
---|
| 181 | $htmlpage .= " <td>$level</td>\n"; |
---|
| 182 | $htmlpage .= " <td style='max-width:60%;'><small>$output"; |
---|
| 183 | |
---|
| 184 | $sshdown{$hostname}++ if $service eq 'SSH'; |
---|
| 185 | |
---|
| 186 | # or ($check =~ m/apt/i and $service eq 'APT UPTODATE') |
---|
| 187 | if (($check =~ m/all/i) |
---|
| 188 | or ($check =~ m/^$service$/i) |
---|
| 189 | or ($check =~ m/critical/i and $level eq 'CRITICAL') |
---|
| 190 | or ($check =~ m/warning/i and $level eq 'WARNING') |
---|
| 191 | or ($check =~ m/pending/i and $level eq 'PENDING') |
---|
| 192 | ) { |
---|
| 193 | $now++; |
---|
| 194 | my $interval = $srv->next_check() - $srv->last_check() || 300; |
---|
| 195 | $interval = 240 if $interval < 240; |
---|
| 196 | $interval = 3000 if $interval > 3000; |
---|
| 197 | my $future = $now + 20 + int(rand($interval - 20)); # 5 * 60 = 300 |
---|
| 198 | |
---|
| 199 | $htmlpage .= " -- <b>CHECK</b> [$now/" . ($future - $now) . "]"; |
---|
| 200 | printf $nagios_cmd "[%lu] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%lu\n", $now, $hostname, $service, $now; |
---|
| 201 | # delay future command |
---|
| 202 | push @futurecheck, sprintf "[%lu] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%lu", $future, $hostname, $service, $future; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | push @aptuptodate, $hostname if $service eq 'APT UPTODATE'; |
---|
| 206 | push @oomkiller, $hostname if $service eq 'OOM Killer' and $level ne 'PENDING'; |
---|
| 207 | |
---|
| 208 | $htmlpage .= "</small></td>\n"; |
---|
| 209 | $htmlpage .= " <td style='text-align:right;'>$downtime days</td>\n"; |
---|
| 210 | $htmlpage .= " </tr>\n"; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | $htmlpage .= "</table>\n"; |
---|
| 214 | close $nagios_cmd; |
---|
| 215 | |
---|
| 216 | if (%hostdown) { |
---|
| 217 | $htmlpage .= "<br />\n"; |
---|
| 218 | $htmlpage .= "<table border='1'>\n"; |
---|
| 219 | for my $host (sort keys %hostdown) { |
---|
| 220 | my $host_stat = $hostdown{$host}; |
---|
| 221 | my $hostname = $host_stat->host_name; |
---|
| 222 | my $downtime = downtime($host_stat->last_state_change); |
---|
| 223 | my $color = alertcolor('#F88888', $downtime); |
---|
| 224 | $htmlpage .= " <tr style='background:$color'>\n"; |
---|
| 225 | $htmlpage .= " <td><a href=\"$config->{'status-cgi'}?host=$hostname\">$hostname</a></td>\n"; |
---|
| 226 | my @host_service; |
---|
| 227 | for my $srv ($log->list_services_on_host($host)) { |
---|
| 228 | push @host_service, $log->service($host, $srv)->service_description; |
---|
| 229 | } |
---|
| 230 | $htmlpage .= " <td><small>" . join(', ', @host_service) . "</small></td>\n"; |
---|
| 231 | $htmlpage .= " <td style='text-align:right;'>$downtime days</td>\n"; |
---|
| 232 | $htmlpage .= " </tr>\n"; |
---|
| 233 | } |
---|
| 234 | $htmlpage .= "</table>\n"; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | if (@oomkiller or @aptuptodate) { |
---|
| 238 | require Nagios::Object::Config; |
---|
| 239 | my $parser = Nagios::Object::Config->new(); |
---|
| 240 | $parser->parse("/var/cache/nagios3/objects.cache"); |
---|
| 241 | |
---|
| 242 | @oomkiller = grep !exists $sshdown{$_}, @oomkiller; |
---|
| 243 | if (@oomkiller) { |
---|
| 244 | $htmlpage .= "<h2>OOM Killer</h2>\n"; |
---|
| 245 | $htmlpage .= "<pre>\n"; |
---|
| 246 | if (@oomkiller == 1) { |
---|
| 247 | $htmlpage .= " ssh"; |
---|
| 248 | } |
---|
| 249 | else { |
---|
| 250 | $htmlpage .= " tssh -c 'sudo rm /var/lib/nagios3/nagios_oom_killer.log'"; |
---|
| 251 | } |
---|
| 252 | for my $host (@oomkiller) { |
---|
| 253 | my $object = $parser->find_object("$host", "Nagios::Host"); |
---|
| 254 | $htmlpage .= ' ' . hostmapping($object->address =~ s/\..*$//r); |
---|
| 255 | } |
---|
| 256 | $htmlpage .= " 'sudo rm /var/lib/nagios3/nagios_oom_killer.log'" if (@oomkiller == 1); |
---|
| 257 | $htmlpage .= "</pre>\n"; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | @aptuptodate = grep !exists $sshdown{$_}, @aptuptodate; |
---|
| 261 | if (@aptuptodate) { |
---|
| 262 | $htmlpage .= "<h2>APT UPTODATE</h2>\n"; |
---|
| 263 | $htmlpage .= "<pre>\n"; |
---|
| 264 | if (@aptuptodate == 1) { |
---|
| 265 | $htmlpage .= " ssh"; |
---|
| 266 | } |
---|
| 267 | else { |
---|
| 268 | $htmlpage .= " tssh"; |
---|
| 269 | } |
---|
| 270 | for my $host (@aptuptodate) { |
---|
| 271 | my $object = $parser->find_object("$host", "Nagios::Host"); |
---|
| 272 | $htmlpage .= ' ' . hostmapping($object->address =~ s/\..*$//r); |
---|
| 273 | } |
---|
| 274 | $htmlpage .= "</pre>\n"; |
---|
| 275 | } |
---|
| 276 | } |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | $htmlpage .= <<'ENDH'; |
---|
| 280 | </body> |
---|
| 281 | </html> |
---|
| 282 | ENDH |
---|
| 283 | |
---|
| 284 | print $htmlpage; |
---|
| 285 | |
---|
| 286 | # delay future check |
---|
| 287 | if (@futurecheck) { |
---|
| 288 | sleep 2; |
---|
| 289 | my $nagios_cmd; |
---|
| 290 | open $nagios_cmd, '>>', $config->{'nagios-cmd'} or die "Can't open file filename: $!"; |
---|
| 291 | print $nagios_cmd "$_\n" for @futurecheck; |
---|
| 292 | close $nagios_cmd; |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | __END__ |
---|
| 296 | |
---|
| 297 | |
---|
[241] | 298 | =head1 NAME |
---|
| 299 | |
---|
| 300 | velvice.cgi - nagios velvice alert panel |
---|
| 301 | |
---|
| 302 | |
---|
| 303 | =head1 DESCRIPTION |
---|
| 304 | |
---|
| 305 | Nagios VELVICE is an acronym for "Nagios leVEL serVICE status". |
---|
| 306 | Homepage: http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/wiki/SoftWare/NagiosVelvice |
---|
| 307 | |
---|
| 308 | =head1 AUTHORS |
---|
| 309 | |
---|
| 310 | Written by Gabriel Moreau - Grenoble - France |
---|
| 311 | |
---|
| 312 | |
---|
| 313 | =head1 LICENSE AND COPYRIGHT |
---|
| 314 | |
---|
| 315 | Licence GNU GPL version 2 or later and Perl equivalent |
---|
| 316 | |
---|
| 317 | Copyright (C) 2014-2018 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>. |
---|