From 5f8b8ca01ea48a201e29dbc79f253e2ad097a9c5 Mon Sep 17 00:00:00 2001 From: Chip Black Date: Mon, 18 Aug 2008 01:08:36 -0500 Subject: [PATCH] Added tool that parses and runs a check based on the contents of a directory --- runcheckdir.pl | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100755 runcheckdir.pl diff --git a/runcheckdir.pl b/runcheckdir.pl new file mode 100755 index 0000000..5f07d21 --- /dev/null +++ b/runcheckdir.pl @@ -0,0 +1,96 @@ +#!/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, %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"; + } + } 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 = ); +close STATUS; + +open CHECK, '-|', $checkcommand; +$status = ; +$details = join('', ); +close CHECK; +my $exitstatus = $? >> 8; + +if ($exitstatus == 0) { + foreach my $command (@{$on{success}}) { + do_command $command; + } +} else { + foreach my $command (@{$on{failure}}) { + do_command $command; + } +} +if ($exitstatus != $oldstatus) { + foreach my $command(@{$on{change}}) { + do_command $command; + } +} + +open STATUS, ">$dir/checkstatus"; +print STATUS "$exitstatus\n"; +close STATUS; -- 2.25.1