#!/usr/bin/perl # # Audit the list of supposed cfengine hosts to see which are actually # responding. Do this by using cfrun to try to run a sample job on them # and interpreting the output. # # David J. Bianco # use Getopt::Long; use Mail::Mailer; $CFRUN="/var/cfengine/sbin/cfrun"; $CFHOSTS="/local/cfengine/etc/cfrun.hosts"; $DEBUG = 0; # Customize this email address. This is where the report will be sent. $MAILADDR="cfengine\@yoursite.com"; $TIMEOUT=300; $errorstrings="Couldn't|failed"; $downhosts=""; $messages=""; GetOptions("cfrun=s" => \$CFRUN, "cfhosts=s" => \$CFHOSTS, "nomail" => \$NOMAIL, "smtpserver" => \$SMTP_SERVER, "timeout=i" => \$TIMEOUT, "mailaddr=s" => \$MAILADDR, "debug" => \$DEBUG); open(CFHOSTS,$CFHOSTS) || die "Can't open $CFHOSTS: $!\n"; $SIG{ALRM} = sub {die "Operation timed out"; }; while() { $host = $_; chomp $host; print "********* Trying host $host\n" if $DEBUG; eval { alarm($TIMEOUT); $output = `$CFRUN -f $CFHOSTS $host 2>&1`; alarm(0); }; alarm(0); print "******** OUTPUT: $host **********\n$output\n\n\n" if $DEBUG; if(($output =~ m/$errorstrings/) || ($output eq "") || ($@ eq "Operation timed out")) { $downhosts .= "$host\n"; $messages .= "************** $host ***************\n$output\n\n"; if($@ eq "Operation timed out") { $messages .= "[OPERATION TIMED OUT]"; } } } $outputstring = "The following hosts are down:\n$downhosts\n\n"; $outputstring .= "Full details for these hosts follow:\n$messages\n\n"; print $outputstring; if(! $NOMAIL) { $mailer = Mail::Mailer->new("sendmail"); # Customize this next line, to include a valid return email address. $mailer->open({ From => "CFEngine Audit Service ", To => $MAILADDR, Subject => "CFEngine Audit Report", }) or die "Can't send mail: $!\n"; print $mailer $outputstring; $mailer->close(); }