[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 |
---|
[256] | 5 | # 2018/06/25 Gabriel Moreau - make velvice generic |
---|
[369] | 6 | # 2018/11/03 Gabriel Moreau - ajax |
---|
[240] | 7 | # |
---|
| 8 | # velvice.cgi |
---|
[380] | 9 | # Copyright (C) 2014-2019, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
[240] | 10 | # |
---|
| 11 | # Need NagiosStatus http://exchange.nagios.org/directory/Addons/APIs/Perl/NagiosStatus-2Epm/details |
---|
| 12 | # Possible command http://old.nagios.org/developerinfo/externalcommands/commandlist.php |
---|
| 13 | # |
---|
[250] | 14 | # apt-get install perl-modules libnagios-object-perl libhtml-parser-perl liburi-encode-perl libcolor-calc-perl libyaml-syck-perl |
---|
[322] | 15 | # apt-get install libdatetime-event-recurrence-perl libdatetime-set-perl |
---|
[240] | 16 | |
---|
| 17 | use strict; |
---|
| 18 | use warnings; |
---|
[433] | 19 | use version; our $VERSION = version->declare('0.11.2'); |
---|
[240] | 20 | |
---|
| 21 | use CGI; |
---|
| 22 | use HTML::Entities (); |
---|
| 23 | use Nagios::StatusLog; |
---|
| 24 | use URI::Encode qw(uri_encode uri_decode); |
---|
| 25 | use Color::Calc (); |
---|
| 26 | use YAML::Syck; |
---|
| 27 | |
---|
[257] | 28 | my $query = CGI->new(); |
---|
[388] | 29 | my $cgi_check = uri_decode($query->param('check')) || ''; |
---|
[257] | 30 | my $cgi_script_name = $query->script_name(); |
---|
[260] | 31 | my $cgi_path = $cgi_script_name =~ s{/[^/]+\.cgi$}{}r; |
---|
[388] | 32 | my $param_only = uri_decode($query->param('only')) || ''; |
---|
[369] | 33 | my $cgi_only; |
---|
[388] | 34 | $cgi_only++ if $param_only eq 'body'; |
---|
[263] | 35 | undef $query; |
---|
[388] | 36 | undef $param_only; |
---|
[256] | 37 | |
---|
[321] | 38 | my %STATUS_DB = ( |
---|
[384] | 39 | CRITICAL => {id => 5, color => '#F88888'}, |
---|
| 40 | UNKNOWN => {id => 4, color => '#FFBB55'}, # #4488FF |
---|
[383] | 41 | WARNING => {id => 3, color => '#FFFF00'}, |
---|
| 42 | PENDING => {id => 2, color => '#E0E0E0'}, |
---|
[384] | 43 | OK => {id => 1, color => '#88D066'}, |
---|
[321] | 44 | ); |
---|
| 45 | |
---|
[240] | 46 | my $config = {}; |
---|
| 47 | $config = YAML::Syck::LoadFile('/etc/nagios3/velvice.yml') if -e '/etc/nagios3/velvice.yml'; |
---|
[256] | 48 | $config->{'nagios-server'} ||= {}; |
---|
| 49 | $config->{'nagios-server'}{'status-file'} ||= '/var/cache/nagios3/status.dat'; |
---|
| 50 | $config->{'nagios-server'}{'nagios-cmd'} ||= '/var/lib/nagios3/rw/nagios.cmd'; |
---|
[260] | 51 | $config->{'nagios-server'}{'portal-url'} ||= $cgi_path =~ s{/cgi-bin/}{/}r . '/'; |
---|
[258] | 52 | $config->{'nagios-server'}{'status-cgi'} ||= "$cgi_path/status.cgi"; |
---|
[263] | 53 | $config->{'nagios-server'}{'stylesheets'} ||= $config->{'nagios-server'}{'portal-url'} =~ s{/?$}{/stylesheets}r; |
---|
[328] | 54 | $config->{'nagios-server'}{'image'} ||= $config->{'nagios-server'}{'portal-url'} =~ s{/?$}{/images}r; |
---|
[256] | 55 | $config->{'host-mapping'} ||= {}; |
---|
| 56 | $config->{'color-downtime'} ||= {}; |
---|
[261] | 57 | $config->{'color-downtime'}{'day-min'} ||= 3; |
---|
| 58 | $config->{'color-downtime'}{'day-max'} ||= 50; |
---|
| 59 | $config->{'color-downtime'}{'factor'} ||= 0.7; |
---|
[256] | 60 | $config->{'remote-action'} ||= {}; |
---|
[322] | 61 | $config->{'refresh'} ||= 0; |
---|
[240] | 62 | |
---|
| 63 | sub hostmapping { |
---|
| 64 | my $host = shift; |
---|
| 65 | |
---|
[256] | 66 | return exists $config->{'host-mapping'}{$host} ? $config->{'host-mapping'}{$host} : $host; |
---|
[240] | 67 | } |
---|
| 68 | |
---|
| 69 | sub downtime { |
---|
| 70 | my ($time_change) = @_; |
---|
| 71 | |
---|
| 72 | my $now = time; |
---|
| 73 | return sprintf '%.1f', ($now - $time_change) / (60 * 3600); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | sub alertcolor { |
---|
[320] | 77 | my ($status, $downtime) = @_; |
---|
[240] | 78 | |
---|
[320] | 79 | my $color = '#0000FF'; |
---|
| 80 | $color = $STATUS_DB{$status}->{'color'} if exists $STATUS_DB{$status}; |
---|
| 81 | |
---|
[261] | 82 | $downtime = $downtime - $config->{'color-downtime'}{'day-min'}; # same color first days |
---|
| 83 | $downtime = $config->{'color-downtime'}{'day-max'} if $downtime > $config->{'color-downtime'}{'day-max'}; # max 50 days for color |
---|
[240] | 84 | $downtime = 0 if $downtime < 0; |
---|
| 85 | |
---|
[261] | 86 | my $factor = ($downtime * $config->{'color-downtime'}{'factor'}) / $config->{'color-downtime'}{'day-max'}; |
---|
[240] | 87 | return Color::Calc::color_light_html($color, $factor); |
---|
| 88 | } |
---|
| 89 | |
---|
[284] | 90 | sub nosbreak { |
---|
| 91 | my ($str) = @_; |
---|
| 92 | |
---|
[285] | 93 | return $str =~ s/\s/\ /gr; |
---|
[284] | 94 | } |
---|
| 95 | |
---|
[322] | 96 | my $log = Nagios::StatusLog->new( |
---|
| 97 | Filename => $config->{'nagios-server'}{'status-file'}, |
---|
| 98 | Version => 3.0 |
---|
| 99 | ); |
---|
| 100 | |
---|
| 101 | # refresh configuration |
---|
| 102 | if (exists $config->{'refreshments'}) { |
---|
| 103 | require DateTime::Event::Recurrence; |
---|
| 104 | require DateTime::SpanSet; |
---|
| 105 | |
---|
| 106 | my @refreshments; |
---|
| 107 | SET: |
---|
| 108 | for my $set (@{$config->{'refreshments'}}) { |
---|
| 109 | my $start = DateTime::Event::Recurrence->weekly(days => $set->{'days'}, hours => $set->{'start'}); |
---|
| 110 | my $end = DateTime::Event::Recurrence->weekly(days => $set->{'days'}, hours => $set->{'end'}); |
---|
| 111 | my $spanset = DateTime::SpanSet->from_sets(start_set => $start, end_set => $end); |
---|
| 112 | push @refreshments, {refresh => $set->{'refresh'}, spanset => $spanset}; |
---|
| 113 | } |
---|
| 114 | |
---|
[324] | 115 | my $now = DateTime->now(time_zone => 'local'); |
---|
[322] | 116 | SET: |
---|
| 117 | for my $set (@refreshments) { |
---|
| 118 | next SET if not $set->{'spanset'}->contains($now); |
---|
| 119 | |
---|
| 120 | $config->{'refresh'} = $set->{'refresh'}; |
---|
| 121 | last SET; |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | |
---|
[240] | 125 | my %hostdown; |
---|
| 126 | my @serviceproblems; |
---|
| 127 | my %hostcount; |
---|
| 128 | my @futurecheck; |
---|
| 129 | HOST: |
---|
| 130 | for my $host (sort $log->list_hosts()) { |
---|
| 131 | my $host_stat = $log->host($host); |
---|
| 132 | |
---|
| 133 | if ($host_stat->status eq 'DOWN') {TESTIF:{ |
---|
| 134 | for my $srv ($log->list_services_on_host($host)) { |
---|
| 135 | last TESTIF if $log->service($host, $srv)->status eq 'OK' or $log->service($host, $srv)->status eq 'PENDING'; |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | $hostdown{$host} = $host_stat; |
---|
| 139 | next HOST; |
---|
| 140 | }} |
---|
| 141 | |
---|
[319] | 142 | SRV: |
---|
[240] | 143 | for my $srv ($log->list_services_on_host($host)) { |
---|
[319] | 144 | my $status = $log->service($host, $srv)->status; |
---|
| 145 | |
---|
| 146 | next SRV if $status eq 'OK'; |
---|
| 147 | |
---|
| 148 | push @serviceproblems, $log->service($host, $srv); |
---|
| 149 | |
---|
| 150 | my $downtime = downtime($log->service($host, $srv)->last_state_change); |
---|
[320] | 151 | my $color = alertcolor($status, $downtime); |
---|
[319] | 152 | |
---|
| 153 | my $status_id = 0; |
---|
[320] | 154 | $status_id = $STATUS_DB{$status}->{'id'} if exists $STATUS_DB{$status}; |
---|
[319] | 155 | |
---|
| 156 | #$hostcount{$host}++; |
---|
| 157 | $hostcount{$host} ||= {count => 0, color => $color, status_id => $status_id, downtime => $downtime}; |
---|
| 158 | $hostcount{$host}->{'count'}++; |
---|
[380] | 159 | if (($status_id > $hostcount{$host}->{'status_id'}) |
---|
| 160 | or (($status_id == $hostcount{$host}->{'status_id'}) and ($downtime < $hostcount{$host}->{'downtime'}))) { |
---|
[319] | 161 | $hostcount{$host}->{'downtime'} = $downtime; |
---|
| 162 | $hostcount{$host}->{'status_id'} = $status_id; |
---|
| 163 | $hostcount{$host}->{'color'} = $color; |
---|
[240] | 164 | } |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | my $now = time; |
---|
| 169 | my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime $now; |
---|
| 170 | $year += 1900; |
---|
| 171 | $mon++; |
---|
[377] | 172 | # my $date_sec = sprintf '%04i-%02i-%02i %02i:%02i:%02i', $year, $mon, $mday, $hour, $min, $sec; |
---|
[376] | 173 | my $date_min = nosbreak(sprintf '%04i-%02i-%02i %02i:%02i', $year, $mon, $mday, $hour, $min); |
---|
[240] | 174 | |
---|
[369] | 175 | my $htmlpage; |
---|
| 176 | |
---|
| 177 | $htmlpage .= <<"ENDH" if not $cgi_only; |
---|
[240] | 178 | Content-Type: text/html |
---|
| 179 | |
---|
| 180 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
---|
| 181 | <html lang="en"> |
---|
| 182 | <head> |
---|
| 183 | <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> |
---|
[323] | 184 | ENDH |
---|
[369] | 185 | |
---|
| 186 | $htmlpage .= <<"ENDH" if $cgi_only; |
---|
| 187 | Content-Type: text/xml |
---|
| 188 | |
---|
| 189 | <?xml version="1.0" encoding="utf-8"?> |
---|
| 190 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd"> |
---|
| 191 | ENDH |
---|
| 192 | |
---|
| 193 | #$htmlpage .= " <meta http-equiv=\"Refresh\" content=\"$config->{'refresh'}\">" if $config->{'refresh'} > 59; # minimum 1 min |
---|
| 194 | $htmlpage .= <<"ENDH" if not $cgi_only; |
---|
[240] | 195 | <title>Nagios Velvice</title> |
---|
[282] | 196 | <link rel="stylesheet" type="text/css" href="$config->{'nagios-server'}{'stylesheets'}/velvice.css"> |
---|
| 197 | <link rel="shortcut icon" type="image/ico" href="$config->{'nagios-server'}{'image'}/favicon.ico"> |
---|
[369] | 198 | <script type="text/javascript"> |
---|
| 199 | var refresh_sec = 900; // 15 min = 900 s |
---|
[370] | 200 | var refresh_interval; |
---|
[369] | 201 | function refresh() { |
---|
| 202 | var req = new XMLHttpRequest(); |
---|
[377] | 203 | var datenow = new Date(); |
---|
[369] | 204 | console.log("Grabbing Value"); |
---|
| 205 | req.open("GET", '$cgi_script_name?only=body', true); // Grabs whatever you've written in this file |
---|
| 206 | req.onload = function () { |
---|
| 207 | if (req.status == 200) { |
---|
[372] | 208 | document.getElementById('master-body').innerHTML = req.responseXML.getElementById('master-body').innerHTML; |
---|
[378] | 209 | console.log("Update Page at", datenow.toLocaleTimeString()); |
---|
[371] | 210 | |
---|
[370] | 211 | refresh_next = req.responseXML.getElementById('master-body').getAttribute("refresh"); |
---|
| 212 | if (refresh_next < 60 && refresh_next != 0) { |
---|
| 213 | refresh_next = 60; |
---|
| 214 | } |
---|
| 215 | if (refresh_next != refresh_sec) { |
---|
| 216 | refresh_sec = refresh_next; |
---|
| 217 | clearInterval(refresh_interval); |
---|
| 218 | refresh_interval = setInterval(refresh, refresh_sec * 1000); |
---|
[378] | 219 | console.log("Update at", datenow.toLocaleTimeString(), "Refresh Interval:", refresh_sec); |
---|
[371] | 220 | document.getElementById('master-body').setAttribute("refresh", refresh_sec); |
---|
[370] | 221 | } |
---|
[369] | 222 | } |
---|
| 223 | } |
---|
| 224 | req.send(null); |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | function init() { // This is the function the browser first runs when it's loaded. |
---|
[370] | 228 | refresh_sec = document.getElementById('master-body').getAttribute("refresh"); |
---|
| 229 | if (refresh_sec < 60 && refresh_sec != 0) { |
---|
| 230 | refresh_sec = 60; |
---|
| 231 | } |
---|
[373] | 232 | // Set the refresh() function to run every 900 seconds. 1 second would be 1000 |
---|
| 233 | refresh_interval = setInterval(refresh, refresh_sec * 1000); |
---|
[378] | 234 | console.log("Start Refresh Interval:", refresh_sec); |
---|
[369] | 235 | } |
---|
| 236 | </script> |
---|
[240] | 237 | </head> |
---|
[369] | 238 | ENDH |
---|
| 239 | |
---|
| 240 | $htmlpage .= <<"ENDH"; |
---|
| 241 | <body id="master-body" onload="init()" refresh="$config->{'refresh'}"> |
---|
[274] | 242 | <div class="header"> |
---|
| 243 | <h1> |
---|
| 244 | <ul> |
---|
[394] | 245 | <li>Nagios Velvice Alert Panel : <a href="$config->{'nagios-server'}{'portal-url'}" target="_blank">Core Server</a></li> |
---|
[374] | 246 | <li><small><a id="refresh" href="$cgi_script_name">$date_min</a></small></li> |
---|
[274] | 247 | </ul> |
---|
| 248 | </h1> |
---|
| 249 | </div> |
---|
[240] | 250 | ENDH |
---|
| 251 | |
---|
[254] | 252 | my %service_name = (); |
---|
| 253 | my %service_status = (); |
---|
[240] | 254 | for my $srv (@serviceproblems) { |
---|
| 255 | $service_name{$srv->service_description}++; |
---|
[254] | 256 | $service_status{$srv->status}++; |
---|
[240] | 257 | } |
---|
| 258 | |
---|
| 259 | if (scalar @serviceproblems == 0) { |
---|
| 260 | $htmlpage .= "<p>No alert to recheck.</p>\n"; |
---|
| 261 | } |
---|
| 262 | else { |
---|
| 263 | |
---|
| 264 | $htmlpage .= "<p>Alert to recheck - Level:\n"; |
---|
| 265 | $htmlpage .= join ",\n", |
---|
[283] | 266 | " <span class='button'><a href='$cgi_script_name?check=all'>ALL</a><small>" . scalar(@serviceproblems) . '</small></span>', |
---|
| 267 | map(" <span class='button'><a href='$cgi_script_name?check=" . lc(uri_encode($_)) . "'>$_</a><small>$service_status{$_}</small></span>", |
---|
| 268 | sort keys %service_status); |
---|
[240] | 269 | $htmlpage .= ".\n"; |
---|
| 270 | $htmlpage .= " <br />\n"; |
---|
| 271 | $htmlpage .= " Service:\n"; |
---|
[279] | 272 | $htmlpage .= join ",\n", |
---|
[284] | 273 | map(" <span class='button'><a href='$cgi_script_name?check=" . lc(uri_encode($_)) . "'>" . nosbreak($_) . "</a><small>$service_name{$_}</small></span>", |
---|
[283] | 274 | sort keys %service_name); |
---|
[240] | 275 | $htmlpage .= ".\n"; |
---|
| 276 | $htmlpage .= "</p>\n"; |
---|
| 277 | |
---|
| 278 | my $nagios_cmd; |
---|
[256] | 279 | open $nagios_cmd, '>>', $config->{'nagios-server'}{'nagios-cmd'} or die "Can't open file filename: $!"; |
---|
[240] | 280 | |
---|
[254] | 281 | my %remote_sshdown = (); |
---|
| 282 | my %remote_db = (); |
---|
| 283 | my $remote_flag; |
---|
[245] | 284 | |
---|
[240] | 285 | my $current_host = ''; |
---|
| 286 | $htmlpage .= "<table border=\"1\">\n"; |
---|
[254] | 287 | SERVICE_PROBLEMS: |
---|
[240] | 288 | for my $srv (@serviceproblems) { |
---|
| 289 | my $hostname = $srv->host_name; |
---|
| 290 | my $service = $srv->service_description; |
---|
[245] | 291 | my $status = $srv->status; |
---|
[240] | 292 | my $downtime = downtime($srv->last_state_change); |
---|
[257] | 293 | my $output = HTML::Entities::encode($srv->plugin_output) =~ s/^[A-Z_\s]+?[:-]//r; |
---|
[240] | 294 | |
---|
[320] | 295 | my $color = alertcolor($status, $downtime); |
---|
[286] | 296 | my $stylecolor = "style='background:$color;'"; |
---|
| 297 | $htmlpage .= " <tr>\n"; |
---|
[240] | 298 | if ($hostname ne $current_host) { |
---|
[319] | 299 | $current_host = $hostname; |
---|
| 300 | my $rowspan = $hostcount{$hostname}->{'count'}; |
---|
| 301 | my $rowcolor = "style='background:" . $hostcount{$hostname}->{'color'} . ";'"; |
---|
| 302 | $htmlpage .= " <td $rowcolor rowspan='$rowspan'>" |
---|
[279] | 303 | . "<a href=\"$cgi_script_name?check=" . uri_encode($hostname) . '">↯</a></td>' . "\n"; |
---|
[319] | 304 | $htmlpage .= " <td $rowcolor class='hoop' rowspan='$rowspan'>" |
---|
[394] | 305 | . "<a href=\"$config->{'nagios-server'}{'status-cgi'}?host=" . uri_encode($hostname) . "\" target=\"_blank\">$hostname</a></td>\n"; |
---|
[240] | 306 | } |
---|
[245] | 307 | |
---|
| 308 | my $bold; |
---|
[254] | 309 | ACTION_STYLE: |
---|
[256] | 310 | for my $act_name (keys %{$config->{'remote-action'}}) { |
---|
| 311 | my $act_regex = $config->{'remote-action'}{$act_name}{'regex'}; |
---|
[389] | 312 | my $act_style = $config->{'remote-action'}{$act_name}{'style'} || ''; |
---|
| 313 | $bold++ if $service =~ m/$act_regex/ and $act_style eq 'bold'; |
---|
[240] | 314 | } |
---|
[287] | 315 | $htmlpage .= $bold ? " <td $stylecolor class='hoop bold'>" : " <td $stylecolor class='hoop'>"; |
---|
[246] | 316 | $htmlpage .= "$service</td>\n"; |
---|
[245] | 317 | |
---|
[287] | 318 | $htmlpage .= " <td $stylecolor class='hoop'>$status</td>\n"; |
---|
| 319 | $htmlpage .= " <td $stylecolor class='comment'>$output</td>\n"; |
---|
| 320 | $htmlpage .= " <td $stylecolor class='days'>$downtime days</td>\n"; |
---|
[240] | 321 | |
---|
[257] | 322 | if (($cgi_check =~ m/all/i) |
---|
| 323 | or ($cgi_check =~ m/^$service$/i) |
---|
| 324 | or ($cgi_check =~ m/critical/i and $status eq 'CRITICAL') |
---|
[433] | 325 | or ($cgi_check =~ m/unknown/i and $status eq 'UNKNOWN') |
---|
[257] | 326 | or ($cgi_check =~ m/warning/i and $status eq 'WARNING') |
---|
| 327 | or ($cgi_check =~ m/pending/i and $status eq 'PENDING') |
---|
[433] | 328 | or ($cgi_check eq $hostname and $status =~ m/^(CRITICAL|UNKNOWN|WARNING|PENDING)$/) |
---|
[240] | 329 | ) { |
---|
| 330 | $now++; |
---|
[291] | 331 | my $interval = $srv->next_check() - $srv->last_check() || 300; # 5 * 60 = 300 |
---|
[240] | 332 | $interval = 240 if $interval < 240; |
---|
| 333 | $interval = 3000 if $interval > 3000; |
---|
[291] | 334 | my $future = $now + 20 + int(rand($interval - 20)); |
---|
[240] | 335 | |
---|
[289] | 336 | $htmlpage .= " <td class='checking'>" . ($future - $now) . "</td>\n"; |
---|
[286] | 337 | #$htmlpage .= " -- <b>CHECK</b> [$now/" . ($future - $now) . "]"; |
---|
[240] | 338 | printf $nagios_cmd "[%lu] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%lu\n", $now, $hostname, $service, $now; |
---|
| 339 | # delay future command |
---|
| 340 | push @futurecheck, sprintf "[%lu] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%lu", $future, $hostname, $service, $future; |
---|
| 341 | } |
---|
| 342 | |
---|
[254] | 343 | ACTION_PUSH_AND_DEPEND: |
---|
[256] | 344 | for my $act_name (keys %{$config->{'remote-action'}}) { |
---|
| 345 | my $act_regex = $config->{'remote-action'}{$act_name}{'regex'}; |
---|
| 346 | my $act_status = $config->{'remote-action'}{$act_name}{'status'} || 'ALL'; |
---|
| 347 | my $act_depend = $config->{'remote-action'}{$act_name}{'depend'} || 'SSH'; |
---|
[254] | 348 | |
---|
| 349 | if ($service =~ m/$act_regex/ and ($act_status eq 'ALL' or $status =~ m/$act_status/)) { |
---|
| 350 | $remote_db{$act_name} ||= []; |
---|
| 351 | push @{$remote_db{$act_name}}, $hostname; |
---|
| 352 | $remote_flag++; |
---|
[245] | 353 | } |
---|
[254] | 354 | |
---|
| 355 | # check depend service otherwise |
---|
| 356 | $remote_sshdown{$act_depend} ||= {}; |
---|
| 357 | $remote_sshdown{$act_depend}->{$hostname}++ if $service =~ m/$act_depend/; |
---|
[245] | 358 | } |
---|
[240] | 359 | |
---|
| 360 | $htmlpage .= " </tr>\n"; |
---|
| 361 | } |
---|
| 362 | |
---|
| 363 | $htmlpage .= "</table>\n"; |
---|
| 364 | close $nagios_cmd; |
---|
| 365 | |
---|
[254] | 366 | # host down |
---|
[240] | 367 | if (%hostdown) { |
---|
| 368 | $htmlpage .= "<br />\n"; |
---|
| 369 | $htmlpage .= "<table border='1'>\n"; |
---|
[254] | 370 | HOST_DOWN: |
---|
[240] | 371 | for my $host (sort keys %hostdown) { |
---|
| 372 | my $host_stat = $hostdown{$host}; |
---|
| 373 | my $hostname = $host_stat->host_name; |
---|
| 374 | my $downtime = downtime($host_stat->last_state_change); |
---|
[320] | 375 | my $color = alertcolor('CRITICAL', $downtime); |
---|
[382] | 376 | my $stylecolor = "style='background:$color;'"; |
---|
| 377 | $htmlpage .= " <tr>\n"; |
---|
| 378 | $htmlpage .= " <td $stylecolor>" . "<a href=\"$cgi_script_name?check=" . uri_encode($hostname) . '">↯</a></td>' . "\n"; |
---|
[394] | 379 | $htmlpage .= " <td $stylecolor class='hoop'><a href=\"$config->{'nagios-server'}{'status-cgi'}?host=" . uri_encode($hostname) . "\" target=\"_blank\">$hostname</a></td>\n"; |
---|
[240] | 380 | my @host_service; |
---|
| 381 | for my $srv ($log->list_services_on_host($host)) { |
---|
[382] | 382 | my $service = $log->service($host, $srv)->service_description; |
---|
| 383 | push @host_service, $service; |
---|
| 384 | if ($cgi_check eq $hostname) { |
---|
| 385 | $now++; |
---|
| 386 | printf $nagios_cmd "[%lu] SCHEDULE_FORCED_SVC_CHECK;%s;%s;%lu\n", $now, $hostname, $service, $now; |
---|
| 387 | } |
---|
[240] | 388 | } |
---|
[382] | 389 | $htmlpage .= " <td $stylecolor><small>" . join(', ', @host_service) . "</small></td>\n"; |
---|
[386] | 390 | $htmlpage .= " <td style='background:$color; text-align:right;'>$downtime days</td>\n"; |
---|
[382] | 391 | $htmlpage .= " <td class='checking'></td>\n" if $cgi_check eq $hostname; |
---|
[240] | 392 | $htmlpage .= " </tr>\n"; |
---|
| 393 | } |
---|
| 394 | $htmlpage .= "</table>\n"; |
---|
| 395 | } |
---|
| 396 | |
---|
[254] | 397 | # remote action |
---|
| 398 | if ($remote_flag) { |
---|
[240] | 399 | require Nagios::Object::Config; |
---|
| 400 | my $parser = Nagios::Object::Config->new(); |
---|
| 401 | $parser->parse("/var/cache/nagios3/objects.cache"); |
---|
| 402 | |
---|
[277] | 403 | $htmlpage .= "<div class='action'>\n"; |
---|
[254] | 404 | REMOTE_ACTION: |
---|
| 405 | for my $act_name (keys %remote_db) { |
---|
[256] | 406 | my $act_depend = $config->{'remote-action'}{$act_name}{'depend'} || 'SSH'; |
---|
[254] | 407 | |
---|
| 408 | my @action = grep !exists $remote_sshdown{$act_depend}->{$_}, @{$remote_db{$act_name}}; |
---|
[245] | 409 | if (@action) { |
---|
[256] | 410 | my $srv_title = $config->{'remote-action'}{$act_name}{'title'} || "Action: $act_name"; |
---|
[247] | 411 | $htmlpage .= "<h2>$srv_title</h2>\n"; |
---|
[245] | 412 | $htmlpage .= "<pre>\n"; |
---|
[256] | 413 | my $remote_action = $config->{'remote-action'}{$act_name}{'command'}; |
---|
| 414 | $remote_action = $config->{'remote-action'}{$act_name}{'command-one'} |
---|
| 415 | if @action == 1 and exists $config->{'remote-action'}{$act_name}{'command-one'}; |
---|
[245] | 416 | my @hosts; |
---|
| 417 | for my $host (@action) { |
---|
| 418 | my $object = $parser->find_object("$host", "Nagios::Host"); |
---|
| 419 | push @hosts, hostmapping($object->address =~ s/\..*$//r); |
---|
| 420 | } |
---|
| 421 | my $hosts_list = join ' ', @hosts; |
---|
[246] | 422 | $htmlpage .= ' ' . $remote_action =~ s{\%m}{$hosts_list}r; |
---|
[245] | 423 | $htmlpage .= "</pre>\n"; |
---|
| 424 | } |
---|
| 425 | } |
---|
[277] | 426 | $htmlpage .= "</div>\n"; |
---|
[240] | 427 | } |
---|
| 428 | } |
---|
| 429 | |
---|
[269] | 430 | $htmlpage .= <<"ENDH"; |
---|
[369] | 431 | <hr clear="all" /> |
---|
[274] | 432 | <div class="footer"> |
---|
| 433 | <b><a href="http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/wiki/SoftWare/NagiosVelvice">Velvice</a> |
---|
| 434 | - version: $VERSION</b> |
---|
| 435 | (<a href="http://servforge.legi.grenoble-inp.fr/pub/soft-trokata/nagios-velvice/velvice.html">online manual</a>) |
---|
| 436 | - Written by Gabriel Moreau |
---|
| 437 | <ul> |
---|
| 438 | <li>Licence GNU GPL version 2 or later and Perl equivalent</li> |
---|
[381] | 439 | <li>Copyright (C) 2014-2019, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France</li> |
---|
[274] | 440 | </ul> |
---|
| 441 | </div> |
---|
[240] | 442 | </body> |
---|
[369] | 443 | ENDH |
---|
| 444 | |
---|
| 445 | $htmlpage .= <<"ENDH" if not $cgi_only; |
---|
[240] | 446 | </html> |
---|
| 447 | ENDH |
---|
| 448 | |
---|
| 449 | print $htmlpage; |
---|
| 450 | |
---|
[254] | 451 | # delayed future check |
---|
[240] | 452 | if (@futurecheck) { |
---|
| 453 | sleep 2; |
---|
| 454 | my $nagios_cmd; |
---|
[256] | 455 | open $nagios_cmd, '>>', $config->{'nagios-server'}{'nagios-cmd'} or die "Can't open file filename: $!"; |
---|
[240] | 456 | print $nagios_cmd "$_\n" for @futurecheck; |
---|
| 457 | close $nagios_cmd; |
---|
| 458 | } |
---|
| 459 | |
---|
| 460 | __END__ |
---|
| 461 | |
---|
| 462 | |
---|
[241] | 463 | =head1 NAME |
---|
| 464 | |
---|
| 465 | velvice.cgi - nagios velvice alert panel |
---|
| 466 | |
---|
[265] | 467 | =head1 USAGE |
---|
[241] | 468 | |
---|
[265] | 469 | velvice.cgi |
---|
| 470 | velvice.cgi?check=XXX |
---|
| 471 | |
---|
| 472 | |
---|
[241] | 473 | =head1 DESCRIPTION |
---|
| 474 | |
---|
[275] | 475 | =begin html |
---|
| 476 | |
---|
[386] | 477 | <img width="700" alt="Nagios Velvice Alert Panel" title="Nagios Velvice Alert Panel" style="float:right;" src="velvice.png" /> |
---|
[275] | 478 | |
---|
| 479 | =end html |
---|
| 480 | |
---|
[241] | 481 | Nagios VELVICE is an acronym for "Nagios leVEL serVICE status". |
---|
| 482 | |
---|
[265] | 483 | The Nagios web page is sometimes very graphically charged |
---|
| 484 | and does not necessarily contain the information you need at a glance. |
---|
| 485 | For example, it is quite complicated to restart controls on multiple hosts in one click. |
---|
[241] | 486 | |
---|
[265] | 487 | For example, a server that is down should take only one line and not one per service... |
---|
| 488 | Similarly, a service that has been down for 5 minutes or since yesterday |
---|
| 489 | has more weight than a service that has fallen for 15 days. |
---|
[241] | 490 | |
---|
[265] | 491 | With Velvice Panel, a broken down server takes only one line. |
---|
| 492 | Services that have been falling for a long time gradually lose their color and become pastel colors. |
---|
[241] | 493 | |
---|
[265] | 494 | With Velvice Panel, it is possible through a single click |
---|
| 495 | to redo a check of all services that are in the CRITICAL state. |
---|
| 496 | Similarly, it is possible to restart a check on all SSH services in breakdowns ... |
---|
| 497 | In order not to clog the Nagios server, checks are shifted by 2 seconds in time. |
---|
| 498 | |
---|
| 499 | There is also a link to the web page of the main Nagios server. |
---|
| 500 | For each computer, you have a direct link to its dedicated web page on this server. |
---|
| 501 | |
---|
| 502 | |
---|
| 503 | =head1 CONFIGURATION FILE SPECIFICATION |
---|
| 504 | |
---|
| 505 | The configuration file must be F</etc/nagios3/velvice.yml>. |
---|
| 506 | This is not a required file. |
---|
| 507 | The file is in YAML format because this is a human-readable text file style. |
---|
| 508 | Other formats could have been Plain XML, RDF, JSON... but they are much less readable. |
---|
| 509 | |
---|
| 510 | You can find in the software nagios-velvice an example of configuration: |
---|
| 511 | L<velvice.sample.yml|http://servforge.legi.grenoble-inp.fr/pub/soft-trokata/nagios-velvice/velvice.sample.yml>. |
---|
| 512 | This one is in fact the master reference specification! |
---|
| 513 | |
---|
| 514 | The main keys C<nagios-server> and C<color-downtime> have good default values. |
---|
| 515 | No secondary key is required... |
---|
[268] | 516 | The Velvice script try hard to replace ~ by the good value automatically. |
---|
[265] | 517 | |
---|
[268] | 518 | nagios-server: |
---|
| 519 | status-file: /var/cache/nagios3/status.dat |
---|
| 520 | nagios-cmd: /var/lib/nagios3/rw/nagios.cmd |
---|
| 521 | portal-url: ~/nagios3/ |
---|
| 522 | status-cgi: ~/cgi-bin/nagios3/status.cgi |
---|
| 523 | stylesheets: ~/nagios3/stylesheets |
---|
| 524 | |
---|
| 525 | The background color of the faulty service line display remains stable with a bright color for at least 3 days. |
---|
| 526 | Then, it decreases and becomes pastel after 53 days with an intensity of 70% (100% is white and 0% is black). |
---|
| 527 | |
---|
| 528 | color-downtime: |
---|
| 529 | day-min: 3 |
---|
| 530 | day-max: 50 |
---|
| 531 | factor: 0.7 |
---|
| 532 | |
---|
[265] | 533 | With key C<host-mapping>, |
---|
| 534 | it's good to map C<localhost> to the real name of the computer (hostname). |
---|
| 535 | |
---|
[268] | 536 | host-mapping: |
---|
| 537 | localhost: srv-nagios |
---|
| 538 | toto: titi |
---|
| 539 | |
---|
[265] | 540 | The only important key is C<remote-action>. |
---|
| 541 | You can affiliate as many subkeys as you want. |
---|
| 542 | Let's take an example: |
---|
| 543 | |
---|
| 544 | remote-action: |
---|
| 545 | oom-killer: |
---|
| 546 | regex: ^OOM Killer |
---|
| 547 | title: OOM Killer |
---|
| 548 | command: tssh -c 'sudo rm /var/lib/nagios3/nagios_oom_killer.log' %m |
---|
| 549 | command-one: ssh %m 'sudo rm /var/lib/nagios3/nagios_oom_killer.log' |
---|
| 550 | depend: ^SSH |
---|
| 551 | status: ALL |
---|
| 552 | style: bold |
---|
| 553 | |
---|
| 554 | C<oom-killer> is just a key for your remote action. |
---|
| 555 | The regex is used to find which service has a problem... |
---|
| 556 | The title is use in the result web page (not mandatory - otherwise, it will be C<Action: oom-killer>). |
---|
| 557 | The C<command> is just written on this web page. |
---|
| 558 | You have the responsibility to copy / cut it on a terminal. |
---|
| 559 | For security reasons, the nagios server does not have the right to launch the command on the remote host. |
---|
| 560 | The wildcard C<%m> is replaced by the list of the host (separated by the space). |
---|
| 561 | Sometime, the command could be different if there is only one computer (just SSH and no parallel SSH). |
---|
| 562 | If your command is based on SSH, |
---|
| 563 | you can have an SSH action only if the remote SSH is running. |
---|
| 564 | So you can make the remote action depend on the SSH service through a regular expression of your choice. |
---|
| 565 | |
---|
| 566 | The last two keys. |
---|
| 567 | The C<status> key is for CRITICAL or WARNING (or ALL). |
---|
| 568 | The key C<style> is there to mark in bold the service in error on the web page. |
---|
| 569 | |
---|
[387] | 570 | The web page will be partially updated every 15 minutes by default (if Javascript is enabled in your browser). |
---|
| 571 | It is possible to have a finer setting depending on the time of day and the day of the week |
---|
| 572 | by using Perl objects C<DateTime::Event::Recurrence> via some YAML parameters in the configuration file. |
---|
| 573 | See the sample configuration file |
---|
| 574 | L<velvice.sample.yml|http://servforge.legi.grenoble-inp.fr/pub/soft-trokata/nagios-velvice/velvice.sample.yml> |
---|
| 575 | for a very detailed case. |
---|
| 576 | With the following configuration, |
---|
| 577 | the refresh of the web page will take place every 5 min (300 s) from 9 am to noon and from 1 pm to 6 pm from Monday to Friday. |
---|
| 578 | From noon to 1 pm and from 6 pm to 8 pm on weekdays, this will take place every 10 minutes. |
---|
| 579 | |
---|
| 580 | refreshments: |
---|
| 581 | - |
---|
| 582 | refresh: 300 |
---|
| 583 | days: [ 1 .. 5 ] |
---|
| 584 | start: [ 9, 13 ] |
---|
| 585 | end: [ 12, 18 ] |
---|
| 586 | - |
---|
| 587 | refresh: 600 |
---|
| 588 | days: [ 1 .. 5 ] |
---|
| 589 | start: [ 12, 18 ] |
---|
| 590 | end: [ 13, 20 ] |
---|
| 591 | |
---|
| 592 | |
---|
[265] | 593 | =head1 SEE ALSO |
---|
| 594 | |
---|
[387] | 595 | yamllint(1), ysh(1), YAML, Nagios::StatusLog, Color::Calc, DateTime::Event::Recurrence |
---|
[265] | 596 | |
---|
| 597 | In Debian GNU/Linux distribution, packages for C<yamllint> and C<ysh> are: |
---|
| 598 | |
---|
| 599 | =over |
---|
| 600 | |
---|
| 601 | =item * C<yamllint> - Linter for YAML files (Python) |
---|
| 602 | |
---|
| 603 | =item * C<libyaml-shell-perl> - YAML test shell (Perl) |
---|
| 604 | |
---|
| 605 | =back |
---|
| 606 | |
---|
| 607 | |
---|
| 608 | Own project ressources: |
---|
| 609 | |
---|
| 610 | =over |
---|
| 611 | |
---|
[293] | 612 | =item * L<Web Site|http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/wiki/SoftWare/NagiosVelvice> |
---|
[265] | 613 | |
---|
| 614 | =item * L<Online Manual|http://servforge.legi.grenoble-inp.fr/pub/soft-trokata/nagios-velvice/velvice.html> |
---|
| 615 | |
---|
[387] | 616 | =item * L<SVN Repository|http://servforge.legi.grenoble-inp.fr/svn/soft-trokata/trunk/nagios-velvice> - |
---|
| 617 | L<Forge Browser|http://servforge.legi.grenoble-inp.fr/projects/soft-trokata/browser/trunk/nagios-velvice> |
---|
[265] | 618 | |
---|
[293] | 619 | =item * L<Debian Package|http://servforge.legi.grenoble-inp.fr/pub/soft-trokata/nagios-velvice/download/> |
---|
| 620 | |
---|
[265] | 621 | =back |
---|
| 622 | |
---|
| 623 | |
---|
| 624 | =head1 VERSION |
---|
| 625 | |
---|
[266] | 626 | $Id: velvice.cgi 433 2020-07-27 10:10:33Z g7moreau $ |
---|
[265] | 627 | |
---|
| 628 | |
---|
| 629 | =head1 AUTHOR |
---|
| 630 | |
---|
| 631 | Written by Gabriel Moreau <Gabriel.Moreau(A)univ-grenoble-alpes.fr>, LEGI UMR 5519, CNRS, Grenoble - France |
---|
| 632 | |
---|
| 633 | |
---|
[241] | 634 | =head1 LICENSE AND COPYRIGHT |
---|
| 635 | |
---|
| 636 | Licence GNU GPL version 2 or later and Perl equivalent |
---|
| 637 | |
---|
[381] | 638 | Copyright (C) 2014-2019, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|