[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 | # |
---|
[250] | 12 | # apt-get install perl-modules libnagios-object-perl libhtml-parser-perl liburi-encode-perl libcolor-calc-perl libyaml-syck-perl |
---|
[240] | 13 | |
---|
| 14 | use strict; |
---|
| 15 | use warnings; |
---|
[255] | 16 | use version; our $VERSION = version->declare('0.5.6'); |
---|
[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; |
---|
[254] | 36 | $config->{'action'} ||= {}; |
---|
[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')); |
---|
[250] | 46 | my $script_name = $query->script_name(); |
---|
[240] | 47 | |
---|
| 48 | sub hostmapping { |
---|
| 49 | my $host = shift; |
---|
| 50 | |
---|
| 51 | return exists $config->{'mapping'}{$host} ? $config->{'mapping'}{$host} : $host; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | sub downtime { |
---|
| 55 | my ($time_change) = @_; |
---|
| 56 | |
---|
| 57 | my $now = time; |
---|
| 58 | return sprintf '%.1f', ($now - $time_change) / (60 * 3600); |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | sub 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 | |
---|
| 72 | my %hostdown; |
---|
| 73 | my @serviceproblems; |
---|
| 74 | my %hostcount; |
---|
| 75 | my @futurecheck; |
---|
| 76 | HOST: |
---|
| 77 | for 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 | |
---|
| 97 | my $now = time; |
---|
| 98 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $now; |
---|
| 99 | $year += 1900; |
---|
| 100 | $mon++; |
---|
| 101 | my $date = sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min; |
---|
| 102 | |
---|
| 103 | my $htmlpage = <<"ENDH"; |
---|
| 104 | Content-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 */ |
---|
| 114 | h1 ul { |
---|
| 115 | display: flex; |
---|
| 116 | justify-content: space-between; |
---|
| 117 | } |
---|
| 118 | h1 li { |
---|
| 119 | display: inline; |
---|
| 120 | } |
---|
[247] | 121 | td.bold { |
---|
[246] | 122 | font-weight: bold; |
---|
| 123 | } |
---|
[240] | 124 | </style> |
---|
| 125 | <body> |
---|
| 126 | <h1> |
---|
| 127 | <ul> |
---|
| 128 | <li>Nagios Velvice Alert Panel : <a href="$config->{'portal-url'}">Core Server</a></li> |
---|
[250] | 129 | <li><small>(<a href="$script_name">UPDATE</a> - $date)</small></li> |
---|
[240] | 130 | </ul> |
---|
| 131 | </h1> |
---|
| 132 | ENDH |
---|
| 133 | |
---|
[254] | 134 | my %service_name = (); |
---|
| 135 | my %service_status = (); |
---|
[240] | 136 | for my $srv (@serviceproblems) { |
---|
| 137 | $service_name{$srv->service_description}++; |
---|
[254] | 138 | $service_status{$srv->status}++; |
---|
[240] | 139 | } |
---|
| 140 | |
---|
| 141 | if (scalar @serviceproblems == 0) { |
---|
| 142 | $htmlpage .= "<p>No alert to recheck.</p>\n"; |
---|
| 143 | } |
---|
| 144 | else { |
---|
| 145 | |
---|
| 146 | $htmlpage .= "<p>Alert to recheck - Level:\n"; |
---|
| 147 | $htmlpage .= join ",\n", |
---|
[250] | 148 | " <a href='$script_name?check=all'>ALL</a><small>(" . scalar(@serviceproblems) . ')</small>', |
---|
[254] | 149 | map(" <a href='$script_name?check=" . lc(uri_encode($_)) . "'>$_</a>($service_status{$_})", sort keys %service_status); |
---|
[240] | 150 | $htmlpage .= ".\n"; |
---|
| 151 | $htmlpage .= " <br />\n"; |
---|
| 152 | $htmlpage .= " Service:\n"; |
---|
[250] | 153 | $htmlpage .= join ",\n", map(" <a href='$script_name?check=" . lc(uri_encode($_)) . "'>$_</a><small>($service_name{$_})</small>", sort keys %service_name); |
---|
[240] | 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 | |
---|
[254] | 160 | my %remote_sshdown = (); |
---|
| 161 | my %remote_db = (); |
---|
| 162 | my $remote_flag; |
---|
[245] | 163 | |
---|
[240] | 164 | my $current_host = ''; |
---|
| 165 | $htmlpage .= "<table border=\"1\">\n"; |
---|
[254] | 166 | SERVICE_PROBLEMS: |
---|
[240] | 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; |
---|
[254] | 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'; |
---|
[240] | 188 | } |
---|
[246] | 189 | $htmlpage .= $bold ? ' <td class="bold">' : ' <td>'; |
---|
| 190 | $htmlpage .= "$service</td>\n"; |
---|
[245] | 191 | |
---|
| 192 | $htmlpage .= " <td>$status</td>\n"; |
---|
[240] | 193 | $htmlpage .= " <td style='max-width:60%;'><small>$output"; |
---|
| 194 | |
---|
| 195 | if (($check =~ m/all/i) |
---|
| 196 | or ($check =~ m/^$service$/i) |
---|
[245] | 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') |
---|
[240] | 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 | |
---|
[254] | 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++; |
---|
[245] | 223 | } |
---|
[254] | 224 | |
---|
| 225 | # check depend service otherwise |
---|
| 226 | $remote_sshdown{$act_depend} ||= {}; |
---|
| 227 | $remote_sshdown{$act_depend}->{$hostname}++ if $service =~ m/$act_depend/; |
---|
[245] | 228 | } |
---|
[240] | 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 | |
---|
[254] | 238 | # host down |
---|
[240] | 239 | if (%hostdown) { |
---|
| 240 | $htmlpage .= "<br />\n"; |
---|
| 241 | $htmlpage .= "<table border='1'>\n"; |
---|
[254] | 242 | HOST_DOWN: |
---|
[240] | 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 | |
---|
[254] | 261 | # remote action |
---|
| 262 | if ($remote_flag) { |
---|
[240] | 263 | require Nagios::Object::Config; |
---|
| 264 | my $parser = Nagios::Object::Config->new(); |
---|
| 265 | $parser->parse("/var/cache/nagios3/objects.cache"); |
---|
| 266 | |
---|
[254] | 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}}; |
---|
[245] | 272 | if (@action) { |
---|
[254] | 273 | my $srv_title = $config->{'action'}{$act_name}{'title'} || "Action: $act_name"; |
---|
[247] | 274 | $htmlpage .= "<h2>$srv_title</h2>\n"; |
---|
[245] | 275 | $htmlpage .= "<pre>\n"; |
---|
[254] | 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'}; |
---|
[245] | 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; |
---|
[246] | 284 | $htmlpage .= ' ' . $remote_action =~ s{\%m}{$hosts_list}r; |
---|
[245] | 285 | $htmlpage .= "</pre>\n"; |
---|
| 286 | } |
---|
| 287 | } |
---|
[240] | 288 | } |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | $htmlpage .= <<'ENDH'; |
---|
| 292 | </body> |
---|
| 293 | </html> |
---|
| 294 | ENDH |
---|
| 295 | |
---|
| 296 | print $htmlpage; |
---|
| 297 | |
---|
[254] | 298 | # delayed future check |
---|
[240] | 299 | if (@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 | |
---|
[241] | 310 | =head1 NAME |
---|
| 311 | |
---|
| 312 | velvice.cgi - nagios velvice alert panel |
---|
| 313 | |
---|
| 314 | |
---|
| 315 | =head1 DESCRIPTION |
---|
| 316 | |
---|
| 317 | Nagios VELVICE is an acronym for "Nagios leVEL serVICE status". |
---|
| 318 | Homepage: http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/wiki/SoftWare/NagiosVelvice |
---|
| 319 | |
---|
| 320 | =head1 AUTHORS |
---|
| 321 | |
---|
| 322 | Written by Gabriel Moreau - Grenoble - France |
---|
| 323 | |
---|
| 324 | |
---|
| 325 | =head1 LICENSE AND COPYRIGHT |
---|
| 326 | |
---|
| 327 | Licence GNU GPL version 2 or later and Perl equivalent |
---|
| 328 | |
---|
| 329 | Copyright (C) 2014-2018 Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>. |
---|