From 8d7475b3a36e68f80dc196a95fadc549c708f1cb Mon Sep 17 00:00:00 2001 From: Chip Black Date: Mon, 18 Aug 2008 22:41:08 -0500 Subject: [PATCH] Changed program names After consulting runit, it makes more sense to have the individual check runner named "runcheck" and the overmind "runcheckdir" --- runcheck | 118 ++++++++++++++++++++++++++++++++++++++++++++++++ runcheckdir | 127 +++++++++------------------------------------------- runchecks | 35 --------------- 3 files changed, 140 insertions(+), 140 deletions(-) create mode 100755 runcheck delete mode 100755 runchecks diff --git a/runcheck b/runcheck new file mode 100755 index 0000000..d6589f2 --- /dev/null +++ b/runcheck @@ -0,0 +1,118 @@ +#!/usr/bin/perl +use File::Basename; +use strict; + + +my $dir = shift; +unless (-d $dir) { + print "Arg is not a directory"; + exit 1; +} +my $me = basename $dir; +my ($status, $details); + +sub do_command { + my $command = shift; + $command =~ /^(\w+)(\s+(.*))?$/; + my ($do, $args) = ($1, $3); + if ($do eq 'mail') { + open MAIL, '|-', qq!mail -s "$me $status" $args! or die "Could not mail"; + print MAIL $details,"\n"; + close MAIL; + } elsif ($do eq 'exec') { + system($args); + } elsif ($do eq 'pipe') { + open PIPE, '|-', $args; + print PIPE "$status\n"; + print PIPE $details; + close PIPE; + } +} + + +my ($checkcommand, $every, %on); +$on{success} = []; +$on{failure} = []; +$on{change} = []; + +open CONFIG, "$dir/check"; +while () { + my @words = split(/\s+/); + my $command = shift @words; + if ($command eq 'check') { + $checkcommand = join(' ', @words); + } elsif ($command eq 'on') { + my $when = shift @words; + if ($when eq 'failure') { + push @{$on{failure}}, join(' ', @words); + } elsif ($when eq 'success') { + push @{$on{success}}, join(' ', @words); + } elsif ($when eq 'change') { + push @{$on{change}}, join(' ', @words); + } 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"; + } +} +close CONFIG; + +unless ($checkcommand) { + print "Check command not specified in $dir/check\n"; + exit 1; +} +unless (@{$on{failure}} || @{$on{success}} || @{$on{change}}) { + print "No actions specified in $dir/check. This is probably a mistake.\n"; +} + +open STATUS, "$dir/checkstatus"; +chomp(my $oldstatus = ); +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; +} + +my ($pid, $hung, $exitstatus); +$SIG{ALRM} = sub { + kill 9, $pid; + $hung = 1; +}; +alarm 10; + +$pid = open CHECK, '-|', $checkcommand; +$status = ; +$details = join('', ); +close CHECK; +if ($hung) { + $status = 'TIMEOUT'; + $details = 'Check did not complete within ten seconds'; + $exitstatus = -1; +} else { + $exitstatus = $? >> 8; +} + +if ($exitstatus == 0 && $oldstatus != 0) { + do_command($_) foreach @{$on{success}}; +} +if ($exitstatus != 0 && $oldstatus == 0) { + do_command($_) foreach @{$on{failure}}; +} +if ($exitstatus != $oldstatus) { + do_command($_) foreach @{$on{change}}; +} + +open STATUS, ">$dir/checkstatus"; +print STATUS "$exitstatus\n"; +print STATUS "$status\n"; +print STATUS "$details\n"; +close STATUS; + +exit $exitstatus; diff --git a/runcheckdir b/runcheckdir index d6589f2..535fa68 100755 --- a/runcheckdir +++ b/runcheckdir @@ -1,118 +1,35 @@ #!/usr/bin/perl -use File::Basename; use strict; - my $dir = shift; -unless (-d $dir) { - print "Arg is not a directory"; +unless ($dir) { + print "No directory specified\n"; exit 1; } -my $me = basename $dir; -my ($status, $details); - -sub do_command { - my $command = shift; - $command =~ /^(\w+)(\s+(.*))?$/; - my ($do, $args) = ($1, $3); - if ($do eq 'mail') { - open MAIL, '|-', qq!mail -s "$me $status" $args! or die "Could not mail"; - print MAIL $details,"\n"; - close MAIL; - } elsif ($do eq 'exec') { - system($args); - } elsif ($do eq 'pipe') { - open PIPE, '|-', $args; - print PIPE "$status\n"; - print PIPE $details; - close PIPE; - } +unless (-d $dir) { + print "$dir is not a directory\n"; + exit 1; } +$|++; -my ($checkcommand, $every, %on); -$on{success} = []; -$on{failure} = []; -$on{change} = []; - -open CONFIG, "$dir/check"; -while () { - my @words = split(/\s+/); - my $command = shift @words; - if ($command eq 'check') { - $checkcommand = join(' ', @words); - } elsif ($command eq 'on') { - my $when = shift @words; - if ($when eq 'failure') { - push @{$on{failure}}, join(' ', @words); - } elsif ($when eq 'success') { - push @{$on{success}}, join(' ', @words); - } elsif ($when eq 'change') { - push @{$on{change}}, join(' ', @words); +while (1) { + opendir(CHECKS, $dir); + while (my $d = readdir(CHECKS)) { + next if (! -d "$dir/$d" || $d eq '.' || $d eq '..'); + unless (-f "$dir/$d/check") { + print "No check script in $dir/$d\n"; + next; + } + print "Running check $dir/$d: "; + system("runcheckdir $dir/$d"); + my $exitstatus = $? >> 8; + if ($exitstatus) { + print "FAILURE\n"; } else { - print "Unknown event in 'on', $dir/check line $.\n"; + print "OK\n"; } - } elsif ($command eq 'every') { - $every = $words[0]; - } else { - print "Unknown command '$command', $dir/check line $.\n"; + sleep 10; } + closedir(CHECKS); } -close CONFIG; - -unless ($checkcommand) { - print "Check command not specified in $dir/check\n"; - exit 1; -} -unless (@{$on{failure}} || @{$on{success}} || @{$on{change}}) { - print "No actions specified in $dir/check. This is probably a mistake.\n"; -} - -open STATUS, "$dir/checkstatus"; -chomp(my $oldstatus = ); -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; -} - -my ($pid, $hung, $exitstatus); -$SIG{ALRM} = sub { - kill 9, $pid; - $hung = 1; -}; -alarm 10; - -$pid = open CHECK, '-|', $checkcommand; -$status = ; -$details = join('', ); -close CHECK; -if ($hung) { - $status = 'TIMEOUT'; - $details = 'Check did not complete within ten seconds'; - $exitstatus = -1; -} else { - $exitstatus = $? >> 8; -} - -if ($exitstatus == 0 && $oldstatus != 0) { - do_command($_) foreach @{$on{success}}; -} -if ($exitstatus != 0 && $oldstatus == 0) { - do_command($_) foreach @{$on{failure}}; -} -if ($exitstatus != $oldstatus) { - do_command($_) foreach @{$on{change}}; -} - -open STATUS, ">$dir/checkstatus"; -print STATUS "$exitstatus\n"; -print STATUS "$status\n"; -print STATUS "$details\n"; -close STATUS; - -exit $exitstatus; diff --git a/runchecks b/runchecks deleted file mode 100755 index 535fa68..0000000 --- a/runchecks +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/perl -use strict; - -my $dir = shift; -unless ($dir) { - print "No directory specified\n"; - exit 1; -} -unless (-d $dir) { - print "$dir is not a directory\n"; - exit 1; -} - -$|++; - -while (1) { - opendir(CHECKS, $dir); - while (my $d = readdir(CHECKS)) { - next if (! -d "$dir/$d" || $d eq '.' || $d eq '..'); - unless (-f "$dir/$d/check") { - print "No check script in $dir/$d\n"; - next; - } - print "Running check $dir/$d: "; - system("runcheckdir $dir/$d"); - my $exitstatus = $? >> 8; - if ($exitstatus) { - print "FAILURE\n"; - } else { - print "OK\n"; - } - sleep 10; - } - closedir(CHECKS); -} -- 2.25.1