[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 | # |
---|
[245] | 12 | # apt-get install libnagios-object-perl libhtml-parser-perl perl-modules liburi-encode-perl libcolor-calc-perl libyaml-syck-perl |
---|
[240] | 13 | |
---|
| 14 | use strict; |
---|
| 15 | use warnings; |
---|
[244] | 16 | use version; our $VERSION = version->declare('0.5.1'); |
---|
[240] | 17 | |
---|
| 18 | use CGI; |
---|
| 19 | use HTML::Entities (); |
---|
| 20 | use Nagios::StatusLog; |
---|
| 21 | use URI::Encode qw(uri_encode uri_decode); |
---|
| 22 | use Color::Calc (); |
---|
| 23 | use YAML::Syck; |
---|
| 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; |
---|
[245] | 36 | $config->{'service'} ||= {}; |
---|
[240] | 37 | |
---|
| 38 | my $query = CGI->new(); |
---|
| 39 | |
---|
| 40 | my $log = Nagios::StatusLog->new( |
---|
| 41 | Filename => $config->{'status-file'}, |
---|
| 42 | Version => 3.0 |
---|
| 43 | ); |
---|
| 44 | |
---|
| 45 | my $check = uri_decode($query->param('check')); |
---|
| 46 | |
---|
| 47 | sub hostmapping { |
---|
| 48 | my $host = shift; |
---|
| 49 | |
---|
| 50 | return exists $config->{'mapping'}{$host} ? $config->{'mapping'}{$host} : $host; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | sub downtime { |
---|
| 54 | my ($time_change) = @_; |
---|
| 55 | |
---|
| 56 | my $now = time; |
---|
| 57 | return sprintf '%.1f', ($now - $time_change) / (60 * 3600); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | sub 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 | |
---|
| 71 | my %hostdown; |
---|
| 72 | my @serviceproblems; |
---|
| 73 | my %hostcount; |
---|
| 74 | my @futurecheck; |
---|
| 75 | HOST: |
---|
| 76 | for 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 | |
---|
| 96 | my $now = time; |
---|
| 97 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $now; |
---|
| 98 | $year += 1900; |
---|
| 99 | $mon++; |
---|
| 100 | my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; |
---|
| 101 | |
---|
| 102 | my $htmlpage = <<"ENDH"; |
---|
| 103 | Content-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 */ |
---|
| 113 | h1 ul { |
---|
| 114 | display: flex; |
---|
| 115 | justify-content: space-between; |
---|
| 116 | } |
---|
| 117 | h1 li { |
---|
| 118 | display: inline; |
---|
| 119 | } |
---|
[247] | 120 | td.bold { |
---|
[246] | 121 | font-weight: bold; |
---|
| 122 | } |
---|
[240] | 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> |
---|
| 131 | ENDH |
---|
| 132 | |
---|
| 133 | my %service_name = (); |
---|
| 134 | my %service_level = (); |
---|
| 135 | for my $srv (@serviceproblems) { |
---|
| 136 | $service_name{$srv->service_description}++; |
---|
| 137 | $service_level{$srv->status}++; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | if (scalar @serviceproblems == 0) { |
---|
| 141 | $htmlpage .= "<p>No alert to recheck.</p>\n"; |
---|
| 142 | } |
---|
| 143 | else { |
---|
| 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 | |
---|
[247] | 159 | # my @oomkiller = (); |
---|
[240] | 160 | my %sshdown = (); |
---|
[247] | 161 | # my @aptuptodate = (); |
---|
[245] | 162 | my %cmdafter = (); |
---|
| 163 | my $after; |
---|
| 164 | |
---|
[240] | 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; |
---|
[245] | 170 | my $status = $srv->status; |
---|
[240] | 171 | my $downtime = downtime($srv->last_state_change); |
---|
| 172 | my $output = HTML::Entities::encode($srv->plugin_output); |
---|
| 173 | $output =~ s/^[A-Z_\s]+?[:-]//; |
---|
| 174 | |
---|
[245] | 175 | my $color = $status eq 'CRITICAL' ? '#F88888' : '#FFFF00'; |
---|
[240] | 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 | } |
---|
[245] | 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'; |
---|
[240] | 187 | } |
---|
[246] | 188 | $htmlpage .= $bold ? ' <td class="bold">' : ' <td>'; |
---|
| 189 | $htmlpage .= "$service</td>\n"; |
---|
[245] | 190 | |
---|
| 191 | $htmlpage .= " <td>$status</td>\n"; |
---|
[240] | 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) |
---|
[245] | 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') |
---|
[240] | 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 | |
---|
[247] | 214 | # push @aptuptodate, $hostname if $service eq 'APT UPTODATE'; |
---|
| 215 | # push @oomkiller, $hostname if $service eq 'OOM Killer' and $status ne 'PENDING'; |
---|
[245] | 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 | } |
---|
[240] | 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 | |
---|
[247] | 255 | # if (@oomkiller or @aptuptodate or $after) { |
---|
| 256 | if ($after) { |
---|
[240] | 257 | require Nagios::Object::Config; |
---|
| 258 | my $parser = Nagios::Object::Config->new(); |
---|
| 259 | $parser->parse("/var/cache/nagios3/objects.cache"); |
---|
| 260 | |
---|
[245] | 261 | for my $srv_name (keys %cmdafter) { |
---|
| 262 | my @action = grep !exists $sshdown{$_}, @{$cmdafter{$srv_name}}; |
---|
| 263 | if (@action) { |
---|
[247] | 264 | my $srv_title = $config->{'service'}{$srv_name}{'title'} || "Action: $srv_name"; |
---|
| 265 | $htmlpage .= "<h2>$srv_title</h2>\n"; |
---|
[245] | 266 | $htmlpage .= "<pre>\n"; |
---|
| 267 | my $remote_action = $config->{'service'}{$srv_name}{'command'}; |
---|
| 268 | $remote_action = $config->{'service'}{$srv_name}{'command-one'} if @action == 1; |
---|
| 269 | my @hosts; |
---|
| 270 | for my $host (@action) { |
---|
| 271 | my $object = $parser->find_object("$host", "Nagios::Host"); |
---|
| 272 | push @hosts, hostmapping($object->address =~ s/\..*$//r); |
---|
| 273 | } |
---|
| 274 | my $hosts_list = join ' ', @hosts; |
---|
[246] | 275 | $htmlpage .= ' ' . $remote_action =~ s{\%m}{$hosts_list}r; |
---|
[245] | 276 | $htmlpage .= "</pre>\n"; |
---|
| 277 | } |
---|
| 278 | } |
---|
| 279 | |
---|
[247] | 280 | # @oomkiller = grep !exists $sshdown{$_}, @oomkiller; |
---|
| 281 | # if (@oomkiller) { |
---|
| 282 | # $htmlpage .= "<h2>OOM Killer</h2>\n"; |
---|
| 283 | # $htmlpage .= "<pre>\n"; |
---|
| 284 | # if (@oomkiller == 1) { |
---|
| 285 | # $htmlpage .= " ssh"; |
---|
| 286 | # } |
---|
| 287 | # else { |
---|
| 288 | # $htmlpage .= " tssh -c 'sudo rm /var/lib/nagios3/nagios_oom_killer.log'"; |
---|
| 289 | # } |
---|
| 290 | # for my $host (@oomkiller) { |
---|
| 291 | # my $object = $parser->find_object("$host", "Nagios::Host"); |
---|
| 292 | # $htmlpage .= ' ' . hostmapping($object->address =~ s/\..*$//r); |
---|
| 293 | # } |
---|
| 294 | # $htmlpage .= " 'sudo rm /var/lib/nagios3/nagios_oom_killer.log'" if (@oomkiller == 1); |
---|
| 295 | # $htmlpage .= "</pre>\n"; |
---|
| 296 | # } |
---|
| 297 | # |
---|
| 298 | # @aptuptodate = grep !exists $sshdown{$_}, @aptuptodate; |
---|
| 299 | # if (@aptuptodate) { |
---|
| 300 | # $htmlpage .= "<h2>APT UPTODATE</h2>\n"; |
---|
| 301 | # $htmlpage .= "<pre>\n"; |
---|
| 302 | # if (@aptuptodate == 1) { |
---|
| 303 | # $htmlpage .= " ssh"; |
---|
| 304 | # } |
---|
| 305 | # else { |
---|
| 306 | # $htmlpage .= " tssh"; |
---|
| 307 | # } |
---|
| 308 | # for my $host (@aptuptodate) { |
---|
| 309 | # my $object = $parser->find_object("$host", "Nagios::Host"); |
---|
| 310 | # $htmlpage .= ' ' . hostmapping($object->address =~ s/\..*$//r); |
---|
| 311 | # } |
---|
| 312 | # $htmlpage .= "</pre>\n"; |
---|
| 313 | # } |
---|
[240] | 314 | } |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | $htmlpage .= <<'ENDH'; |
---|
| 318 | </body> |
---|
| 319 | </html> |
---|
| 320 | ENDH |
---|
| 321 | |
---|
| 322 | print $htmlpage; |
---|
| 323 | |
---|
| 324 | # delay future check |
---|
| 325 | if (@futurecheck) { |
---|
| 326 | sleep 2; |
---|
| 327 | my $nagios_cmd; |
---|
| 328 | open $nagios_cmd, '>>', $config->{'nagios-cmd'} or die "Can't open file filename: $!"; |
---|
| 329 | print $nagios_cmd "$_\n" for @futurecheck; |
---|
| 330 | close $nagios_cmd; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | __END__ |
---|
| 334 | |
---|
| 335 | |
---|
[241] | 336 | =head1 NAME |
---|
| 337 | |
---|
| 338 | velvice.cgi - nagios velvice alert panel |
---|
| 339 | |
---|
| 340 | |
---|
| 341 | =head1 DESCRIPTION |
---|
| 342 | |
---|
| 343 | Nagios VELVICE is an acronym for "Nagios leVEL serVICE status". |
---|
| 344 | Homepage: http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/wiki/SoftWare/NagiosVelvice |
---|
| 345 | |
---|
| 346 | =head1 AUTHORS |
---|
| 347 | |
---|
| 348 | Written by Gabriel Moreau - Grenoble - France |
---|
| 349 | |
---|
| 350 | |
---|
| 351 | =head1 LICENSE AND COPYRIGHT |
---|
| 352 | |
---|
| 353 | Licence GNU GPL version 2 or later and Perl equivalent |
---|
| 354 | |
---|
| 355 | Copyright (C) 2014-2018 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>. |
---|