From f1ae29268987876a7d0ee86efe39b57717fb91ee Mon Sep 17 00:00:00 2001 From: Jamie Cameron Date: Tue, 22 May 2012 19:37:11 -0700 Subject: [PATCH] Finished off monitor history project --- smart-status/status_monitor.pl | 5 ++++- status/CHANGELOG | 1 + status/change-monitor.pl | 13 +++++++----- status/consume-monitor.pl | 2 ++ status/edit_mon.cgi | 36 ++++++++++++++++++++++++++++++---- status/file-monitor.pl | 12 ++++++++++-- status/load-monitor.pl | 6 ++++-- status/mailq-monitor.pl | 2 ++ status/memory-monitor.pl | 6 ++++++ status/monitor.pl | 14 ++++++++++--- status/oldfile-monitor.pl | 8 ++++++-- status/proc-monitor.pl | 11 ++++++----- status/sensors-monitor.pl | 17 +++++++++------- status/space-monitor.pl | 7 +++++++ 14 files changed, 109 insertions(+), 31 deletions(-) diff --git a/smart-status/status_monitor.pl b/smart-status/status_monitor.pl index 84aa0d444..00fc5fc77 100755 --- a/smart-status/status_monitor.pl +++ b/smart-status/status_monitor.pl @@ -48,18 +48,21 @@ elsif (!$st->{'check'}) { elsif ($st->{'errors'} && $_[1]->{'errors'} == 1) { # Errors found, and failing on any errors return { 'up' => 0, + 'value' => $st->{'errors'}, 'desc' => &text('monitor_errorsfound', $st->{'errors'}) }; } elsif ($st->{'errors'} && $_[1]->{'errors'} == 2 && $st->{'errors'} > $lasterrors{$_[1]->{'drive'}}) { # Errors found and have increased return { 'up' => 0, + 'value' => $st->{'errors'}, 'desc' => &text('monitor_errorsinced', $st->{'errors'}, $lasterrors{$_[1]->{'drive'}}) }; } else { # All OK! - return { 'up' => 1 }; + return { 'up' => 1, + 'value' => $st->{'errors'} }; } } diff --git a/status/CHANGELOG b/status/CHANGELOG index 8f7553c67..e87d80612 100644 --- a/status/CHANGELOG +++ b/status/CHANGELOG @@ -79,3 +79,4 @@ Enhanced the free memory monitor to be able to check virtual memory as well. Update the SSL certificate monitor to check alternate names as well when looking for hostname mismatches. ---- Changes since 1.590 ---- Commands run when a monitor goes up or down can now access details of the monitor via environment variables starting with STATUS_ +A history of the status of each monitor's scheduled checks is now logged for 30 days, and can be viewed on the Edit Monitor page. This also includes the value associated with the monitor (such as free disk space), if any. diff --git a/status/change-monitor.pl b/status/change-monitor.pl index 7deb62a03..8f02a64b7 100755 --- a/status/change-monitor.pl +++ b/status/change-monitor.pl @@ -7,19 +7,22 @@ local %change; &read_file("$module_config_directory/change", \%change); local $t = $change{$_[0]->{'file'}}; local @st = stat($_[0]->{'file'}); -local $rv; +local $up; if ($t && $st[9] != $t) { - $rv = { 'up' => 0 }; + $up = 0; } elsif (!$t) { - $rv = { 'up' => -1 }; + $up = -1; } else { - $rv = { 'up' => 1 }; + $up = 1; } $change{$_[0]->{'file'}} = $st[9]; &write_file("$module_config_directory/change", \%change); -return $rv; +return { 'up' => $up, + 'value' => $st[9], + 'nice_value' => &make_date($st[9]), + }; } sub show_change_dialog diff --git a/status/consume-monitor.pl b/status/consume-monitor.pl index d0a8bc5ad..e86bccfe7 100755 --- a/status/consume-monitor.pl +++ b/status/consume-monitor.pl @@ -24,6 +24,8 @@ if ($m) { 'desc' => &text('consume_high', &nice_size($diff*1024)) }; } + $rv->{'value'} = $diff*1024; + $rv->{'nice_value'} = &nice_size($diff*1024); } $consume{$_[0]->{'fs'}} = "$sp[1] $now"; &write_file("$module_config_directory/consume", \%consume); diff --git a/status/edit_mon.cgi b/status/edit_mon.cgi index d7089a6fc..2823faddd 100755 --- a/status/edit_mon.cgi +++ b/status/edit_mon.cgi @@ -251,6 +251,8 @@ if (!$in{'type'}) { if (@history) { print &ui_hidden_table_start($text{'mon_header4'}, "width=100%", 2, "history", defined($in{'all'}) || defined($in{'changes'})); + + # Build links to switch to changes-only mode or show all history @links = ( ); if ($in{'changes'}) { push(@links, "{'old'} ne $_->{'new'} } @history; } + + # Check if any history events have a value + $anyvalue = 0; + foreach $h (@history) { + foreach my $hv (split(/\//, $h->{'value'})) { + my ($vhost, $v) = split(/=/, $hv, 2); + if ($v ne '') { + $anyvalue++; + last; + } + } + } + + # Show history table $links = &ui_links_row(\@links); - $table = &ui_columns_start([ $text{'mon_hwhen'}, - $text{'mon_hold'}, - $text{'mon_hnew'} ]); + $table = &ui_columns_start([ + $text{'mon_hwhen'}, + $text{'mon_hold'}, + $text{'mon_hnew'}, + $anyvalue ? ( $text{'mon_hvalue'} ) : ( ) ]); foreach $h (reverse(@history)) { my @cols = ( &make_date($h->{'time'}) ); foreach my $s ($h->{'old'}, $h->{'new'}) { @@ -289,7 +307,17 @@ if (@history) { } push(@cols, join(" ", @ups)); } - push(@cols, $h->{'value_show'} || $h->{'value'}); + if ($anyvalue) { + my @vlist; + my @values = split(/\//, $h->{'value'}); + my @nice_values = split(/\//, $h->{'nice_value'}); + for(my $i=0; $i<@values; $i++) { + my ($vhost, $v) = split(/=/, $values[$i], 2); + my ($_, $nv) = split(/=/, $nice_values[$i], 2); + push(@vlist, $nv || $v); + } + push(@cols, join(" ", @vlist)); + } $table .= &ui_columns_row(\@cols); } $table .= &ui_columns_end(); diff --git a/status/file-monitor.pl b/status/file-monitor.pl index 51a2fe5a9..72e60dc6a 100755 --- a/status/file-monitor.pl +++ b/status/file-monitor.pl @@ -11,6 +11,8 @@ else { @files = ( $_[0]->{'file'} ); } my $allup = 1; +my @allfiles; +my @allsizes; foreach my $f (@files) { local @st = stat($f); local $size; @@ -33,6 +35,7 @@ foreach my $f (@files) { $allup = 0; push(@allfiles, $f); } + push(@allsizes, $size) if (defined($size)); } my $desc; if (@files > 1 && @allfiles) { @@ -44,8 +47,13 @@ if (@files > 1 && @allfiles) { $desc = &text('file_elarge', $desc); } } -return { 'up' => $up, - 'desc' => $desc }; +my $rv = { 'up' => $up, + 'desc' => $desc }; +if (@allsizes == 1) { + $rv->{'value'} = $allsizes[0]; + $rv->{'nice_value'} = &nice_size($allsizes[0]); + } +return $rv; } sub show_file_dialog diff --git a/status/load-monitor.pl b/status/load-monitor.pl index 00989d995..4d27249c1 100755 --- a/status/load-monitor.pl +++ b/status/load-monitor.pl @@ -8,11 +8,13 @@ if (!@u) { return { 'up' => -1 } } elsif ($u[$_[0]->{'time'}] >= $_[0]->{'max'}) { - return { 'up' => 0 } + return { 'up' => 0, + 'value' => $u[$_[0]->{'time'}] }; } else { return { 'up' => 1, - 'desc' => "Load is $u[$_[0]->{'time'}]" }; + 'desc' => "Load is $u[$_[0]->{'time'}]", + 'value' => $u[$_[0]->{'time'}] }; } } diff --git a/status/mailq-monitor.pl b/status/mailq-monitor.pl index e85211854..e7a5c6032 100755 --- a/status/mailq-monitor.pl +++ b/status/mailq-monitor.pl @@ -19,10 +19,12 @@ elsif ($m eq "postfix") { } if (@qfiles > $_[0]->{'size'}) { return { 'up' => 0, + 'value' => scalar(@qfiles), 'desc' => "".&text('mailq_toomany', scalar(@qfiles))."" }; } else { return { 'up' => 1, + 'value' => scalar(@qfiles), 'desc' => &text('mailq_ok', scalar(@qfiles)) }; } } diff --git a/status/memory-monitor.pl b/status/memory-monitor.pl index 23e241ed2..bb6743bb8 100755 --- a/status/memory-monitor.pl +++ b/status/memory-monitor.pl @@ -12,10 +12,14 @@ if ($@) { } elsif ($mem[1] < $_[0]->{'min'}) { return { 'up' => 0, + 'value' => $mem[1]*1024, + 'nice_value' => &nice_size($mem[1]*1024), 'desc' => &text('memory_freelow', &nice_size($mem[1]*1024)) }; } elsif ($mem[2] && $mem[3] < $_[0]->{'minswap'}) { return { 'up' => 0, + 'value' => $mem[3]*1024, + 'nice_value' => &nice_size($mem[3]*1024), 'desc' => &text('memory_freelowswap', &nice_size($mem[3]*1024)) }; } @@ -25,6 +29,8 @@ else { push(@desc, &text('memory_freeswap', &nice_size($mem[2]*1024))); } return { 'up' => 1, + 'value' => $mem[1]*1024, + 'nice_value' => &nice_size($mem[1]*1024), 'desc' => join(", ", @desc) }; } } diff --git a/status/monitor.pl b/status/monitor.pl index 0b2a12ba4..261efaaec 100755 --- a/status/monitor.pl +++ b/status/monitor.pl @@ -92,6 +92,8 @@ foreach $serv (@services) { # Check for a status change or failure on each monitored host, # and perform the appropriate action $newstats = { }; + $newvalues = { }; + $newvalues_nice = { }; foreach $r (@remotes) { # Work out the hostname local $host = $r eq "*" ? $thishost : $r; @@ -191,6 +193,8 @@ foreach $serv (@services) { } } $newstats->{$r} = $up; + $newvalues->{$r} = $stat->{'value'}; + $newvalues_nice->{$r} = $stat->{'nice_value'}; if ($serv->{'email'} && $thisemail) { # If this service has an extra email address specified, @@ -225,9 +229,13 @@ foreach $serv (@services) { } # Log the status - $newstatus = join(" ", map { "$_=$newstats->{$_}" } @remotes); + $newstatus_str = join(" ", map { "$_=$newstats->{$_}" } @remotes); + $newvalues_str = join("/", map { "$_=$newvalues->{$_}" } @remotes); + $newvalues_nice_str = join("/", map { "$_=$newvalues_nice->{$_}" } @remotes); %history = ( 'time' => $nowunix, - 'new' => $newstatus, + 'new' => $newstatus_str, + 'value' => $newvalues_str, + 'nice_value' => $newvalues_nice_str, 'by' => $by ); if (defined($oldstatus{$serv->{'id'}})) { $history{'old'} = $oldstatus{$serv->{'id'}}; @@ -235,7 +243,7 @@ foreach $serv (@services) { &add_history($serv, \%history); # Update old status hash - $oldstatus{$serv->{'id'}} = $newstatus; + $oldstatus{$serv->{'id'}} = $newstatus_str; } # Close oldstatus and fails files diff --git a/status/oldfile-monitor.pl b/status/oldfile-monitor.pl index d270f62ca..b938f15c0 100755 --- a/status/oldfile-monitor.pl +++ b/status/oldfile-monitor.pl @@ -11,11 +11,15 @@ if (!@st) { } elsif ($st[9] < time()-$_[0]->{'diff'}) { # File hasn't been changed lately - return { 'up' => 0 }; + return { 'up' => 0, + 'value' => $st[9], + 'nice_value' => &make_date($st[9]) }; } else { # File has been changed lately - return { 'up' => 1 }; + return { 'up' => 1, + 'value' => $st[9], + 'nice_value' => &make_date($st[9]) }; } } diff --git a/status/proc-monitor.pl b/status/proc-monitor.pl index 9907b1cd1..2bd92de03 100755 --- a/status/proc-monitor.pl +++ b/status/proc-monitor.pl @@ -16,19 +16,20 @@ foreach $p (&foreign_call("proc", "list_processes")) { local $thresh = $_[0]->{'thresh'} || 1; if ($_[0]->{'not'}) { if ($count >= $thresh) { - return { 'up' => 0 }; + return { 'up' => 0, 'value' => $count }; } else { - return { 'up' => 1 }; + return { 'up' => 1, 'value' => $count }; } } else { if ($count >= $thresh) { - return { 'up' => 1, 'desc' => &text('proc_pid', - join(" ", @found)) }; + return { 'up' => 1, + 'desc' => &text('proc_pid', join(" ", @found)), + 'value' => $count }; } else { - return { 'up' => 0 }; + return { 'up' => 0, 'value' => $count }; } } } diff --git a/status/sensors-monitor.pl b/status/sensors-monitor.pl index 6bffc301a..e2c31a279 100755 --- a/status/sensors-monitor.pl +++ b/status/sensors-monitor.pl @@ -9,13 +9,16 @@ return { 'up' => 1 } if (!$sens); if ($_[0]->{'mode'} == 0) { return $sens->{'alarm'} ? { 'up' => 0 } : { 'up' => 1 }; } -elsif ($_[0]->{'mode'} == 1) { - return $sens->{'value'} < $_[0]->{'min'} ? { 'up' => 0 } - : { 'up' => 1 }; - } -elsif ($_[0]->{'mode'} == 2) { - return $sens->{'value'} > $_[0]->{'max'} ? { 'up' => 0 } - : { 'up' => 1 }; +else { + local $up; + if ($_[0]->{'mode'} == 1) { + $up = $sens->{'value'} < $_[0]->{'min'} ? 0 : 1; + } + elsif ($_[0]->{'mode'} == 2) { + $up = $sens->{'value'} > $_[0]->{'max'} ? 0 : 1; + } + return { 'up' => $up, + 'value' => $sens->{'value'} }; } } diff --git a/status/space-monitor.pl b/status/space-monitor.pl index e06c931c6..8da007a86 100755 --- a/status/space-monitor.pl +++ b/status/space-monitor.pl @@ -20,6 +20,8 @@ if ($m) { local $pc = $sp[1] * 100.0 / $sp[0]; if ($pc < $1) { return { 'up' => 0, + 'value' => $pc, + 'nice_value' => $pc.'%', 'desc' => &text('space_perr', int($pc)) }; } } @@ -27,6 +29,8 @@ if ($m) { # Compare absolute size if ($sp[1] < $_[0]->{'min'}) { return { 'up' => 0, + 'value' => $sp[1]*1024, + 'nice_value' => &nice_size($sp[1]*1024), 'desc' => &text('space_merr', &nice_size($sp[1]*1024)) }; } @@ -37,11 +41,14 @@ if ($m) { local @isp = &mount::inode_space($m->[2], $m->[0]); if ($isp[1] < $_[0]->{'inode'}) { return { 'up' => 0, + 'value' => $isp[1], 'desc' => &text('space_ierr', $isp[1]) }; } } return { 'up' => 1, + 'value' => $sp[1]*1024, + 'nice_value' => &nice_size($sp[1]*1024), 'desc' => &text('space_desc', &nice_size($sp[1]*1024)) }; } else {