return to first page linux journal archive
keywordscontents

Listing 4. Monitor.pl

# !/usr/bin/perl
$URLToPing = $ARGV[0];
$PIN = $ARGV[1];
$Delay = $ARGV[2];
$RetryDelay = $ARGV[3];
$Debug = $ARGV[4];
$HTTPping = "/bin/HTTPping.pl";
print "URL = $URLToPing\n" if ($Debug ==1);
print "Pin = $PIN\n" if ($Debug==1);
print "Delay = $Delay seconds\n" if ($Debug==1);
print "Retry Delay = $RetryDelay seconds\n" if ($Debug==1);
# sleep a random amount up to one delay period
# this helps 'spread out' multiple instances of 
# script running
simultaneously
# sleep(int(rand($Delay)) + 1);
# start out using the current ping result as the
# current server status
$HTTPPingResult = HTTPPing($URLToPing);
$ServerStatus = $HTTPPingResult;
# send a page when the monitor starts
SendPage("Monitor for $URLToPing up at " .
      localtime(), $PIN);
if ($ServerStatus == 1) {
   SendPage("$URLToPing is UP at startup " .
      localtime(), $PIN);
   } else {
   SendPage("$URLToPing is DOWN at startup " .
      localtime(), $PIN);
}
# loop indefinately, checking the site and paging 
# if necessary
while (1==1) {
   # HTTP ping the site
   $HTTPPingResult = HTTPPing($URLToPing);
   print "ServerStatus = $ServerStatus\n"
      if ($Debug == 1);
   print "HTTPPingResult = $HTTPPingResult\n"
      if ($Debug == 1);
   # check to see if the site went down
   if (($ServerStatus == 1) and 
    ($HTTPPingResult == 0)) {
      print 
   "The site appears down, waiting to verify.\n" 
      if ($Debug == 1);
      sleep($RetryDelay);
      # re-ping the site
      $HTTPPingResult = HTTPPing($URLToPing);
      if ($HTTPPingResult == 0) {
         print "  Verified. site down\n";
         $ServerStatus = 0;
         SendPage("$URLToPing is DOWN " .
	    localtime(), $PIN);
      } else {
         print "Did not verify, site still up\n";
      }
   }
   # check to see if the site is up
   if (($ServerStatus == 0) and 
    ($HTTPPingResult == 1)) {
      $ServerStatus = 1;   
      print "$URLToPing up at " . 
         localtime() . "\n" if ($Debug ==1);
      SendPage("$URLToPing is UP at " .
         localtime(), $PIN);
   }
   # wait between pings
   sleep ($Delay);
}
sub HTTPPing {
   print "HTTPPing called\n" if ($Debug == 1);
   my $URL = $ARGV[0];
        $command = "$HTTPping $URL";
   my $pingresult = system($command)/256;
   return $pingresult;
}
sub SendPage {
   use HTTP::Request::Common;
        require LWP::UserAgent;
   my ($Message, $PIN) = @_;
   $to = "$PIN,1";
   $ua = new LWP::UserAgent;
   $req = POST 
'http://204.71.160.11/destineer/ss_paging.cgi',
      [ cmd => 'send',
        to  => $to,
        msg => $Message];
   $response = $ua->request($req);
}