Add check throttling in runcheckdir
authorChip Black <bytex64@bytex64.net>
Mon, 18 Aug 2008 07:23:13 +0000 (02:23 -0500)
committerChip Black <bytex64@bytex64.net>
Mon, 18 Aug 2008 07:23:13 +0000 (02:23 -0500)
checks/internets/check
runcheckdir

index 57bb313..d67d1aa 100644 (file)
@@ -1,2 +1,3 @@
 check ping -c 1 208.113.178.19
+every 600
 on change mail chip
index 7dbfa2f..6b5bcca 100755 (executable)
@@ -30,7 +30,7 @@ sub do_command {
 }
 
 
-my ($checkcommand, %on);
+my ($checkcommand, $every, %on);
 $on{success} = [];
 $on{failure} = [];
 $on{change} = [];
@@ -52,6 +52,8 @@ while (<CONFIG>) {
                } else {
                        print "Unknown event in 'on', $dir/check line $.\n";
                }
+       } elsif ($command eq 'every') {
+               $every = $words[0];
        } else {
                print "Unknown command '$command', $dir/check line $.\n";
        }
@@ -68,23 +70,30 @@ unless (@{$on{failure}} || @{$on{success}} || @{$on{change}}) {
 
 open STATUS, "$dir/checkstatus";
 chomp(my $oldstatus = <STATUS>);
+my $oldtime = (stat(STATUS))[9];
 close STATUS;
 
+my $td = time() - $oldtime;
+if ($td < $every) {
+       # Not time to recheck yet
+       #print "Not rechecking; ", $every - $td, " seconds to go.\n";
+       exit $oldstatus;
+}
+
 open CHECK, '-|', $checkcommand;
 $status = <CHECK>;
 $details = join('', <CHECK>);
 close CHECK;
 my $exitstatus = $? >> 8;
 
-my $command;
 if ($exitstatus == 0 && $oldstatus != 0) {
-       do_command($command) foreach $command @{$on{success}};
+       do_command($_) foreach @{$on{success}};
 }
 if ($exitstatus != 0 && $oldstatus == 0) {
-       do_command($command) foreach $command @{$on{failure}};
+       do_command($_) foreach @{$on{failure}};
 }
 if ($exitstatus != $oldstatus) {
-       do_command($command) foreach $command @{$on{change}};
+       do_command($_) foreach @{$on{change}};
 }
 
 open STATUS, ">$dir/checkstatus";
@@ -92,3 +101,5 @@ print STATUS "$exitstatus\n";
 print STATUS "$status\n";
 print STATUS "$details\n";
 close STATUS;
+
+exit $exitstatus;