-#!@@PERL@@
+#!/usr/bin/perl
#
# Spong network monitoring script. This runs various tests against IP based
# network services (like nntp, smtp, http, pop, etc...) If it can not
use Spong::Log;
# Check to see if the Time::HiRes module is available
-eval "use Time::HiRes qw( time );";
+eval { require Time::HiRes; import Time::HiRes qw(time); };
if (! $@ ) { $hires = 1; } else { $hires = 0; };
srand( time() ^ ($$ + ($$ << 15 )) );
%PLUGINS = {};
&config_funcs();
+my @bad_checks;
+
# Do the various network tests for each host.
+my $lastcheck = time();
+
while( 1 ) {
+ @bad_checks = (); # Clear out the bad check list
+
+ # Main checking loop, check everything.
foreach $host ( @HOSTS_LIST ) {
&debug( "checking network services on $host" );
- my $check;
- foreach $check ( split(/\s+/,$HOSTS{$host}->{'services'}) ) {
+ foreach my $check ( split(/\s+/,$HOSTS{$host}->{'services'}) ) {
$0 = "spong-network (checking $host/$check)";
&do_check($host,$check);
}
- }
+ }
+
- # If we are suppose to stay alive, then sleep about $SPONGSLEEP seconds, we
- # add a little randomness so that things don't get in sync and pound the
- # spong-server. Otherwise, just exit.
+ # Now recheck the services that were down, but need to be rechecked
+ while ( @bad_checks ) {
+ my @tmp_checks = @bad_checks;
+ @bad_checks = ();
+ foreach $tmp (@tmp_checks) {
+ my ( $host, $check ) = split / /,$tmp;
+ &do_check($host,$check);
+ }
+
+ sleep $RECHECKSLEEP;
+ }
+
+ # If we are suppose to stay alive, then calculate the time for the next
+ # main time loop which is $lastcheck + $SPONGSLEEP, add a little randomness
+ # so that things don't get in sync and pound the spong-server. If we need
+ # to sleep off the difference. Otherwise, just exit.
if( $nosleep ) {
last;
} else {
- my $sleep = int($SPONGSLEEP - (.05 * $SPONGSLEEP) +
- rand(.1 * $SPONGSLEEP));
- &debug( "sleeping for $sleep seconds" );
- $0 = "spong-network (sleeping)";
- sleep $sleep;
+
+ # Calculate the time for the next loop
+ my $nexttime = $lastcheck + int($SPONGSLEEP - (.05 * $SPONGSLEEP) +
+ rand(.1 * $SPONGSLEEP));
+
+ if ( $nexttime > time() ) {
+ my $sleep = $nexttime - time();
+ &debug( "sleeping for $sleep seconds" );
+ $0 = "spong-network (sleeping)";
+ sleep $sleep;
+ }
+ $lastcheck = $nexttime; # Save the curent check time as the last time
}
+
}
unlink( "$SPONGTMP/spong-network.pid" ) unless $nosleep;
if ($status eq 'red') {
$crit_count += 1;
$color = ($crit_count < $CRIT_WARN_LEVEL) ? 'yellow' : 'red';
- $summary = "($crit_count/$CRIT_WARN_LEVEL) " . $summary
+ $summary = "($crit_count/$CRIT_WARN_LEVEL) " . $summary;
+ push @bad_checks, "$host $service" if ($crit_count < $CRIT_WARN_LEVEL);
} else {
$crit_count = 0;
$color = $status;
&debug("$status - $crit_count - $CRIT_WARN_LEVEL - $color - $summary");
# Save the critical counter in the host for the service
- if ($color ne "green") {
- $HOSTS{$host}->{'service'}->{$service}->{'count'} = $crit_count;
- } else {
- undef $HOSTS{$host}->{'service'}->{$service}->{'count'};
- }
+ $HOSTS{$host}->{'service'}->{$service}->{'count'} = $crit_count;
eval {
&status( $SPONGSERVER, $host, $service, $color, $summary, $message );
my( $attempt, $start, $message, $diff, $errcd );
for $timeout ( 3, 5, 12 ) {
- $start = main::time();
+ $start = time();
($errcd,$message) = &check_tcp( $host, $port, $send, $timeout );
- $diff = main::time() - $start;
+ $diff = time() - $start;
$attempt++;
if( $message =~ /$check/ ) { $color = "green"; last; }