Mayday, Mayday!

Script I.2.2: Monitoring a Remote Web Server

Send e-mail to administrator when a remote server becomes unresponsive.

 #!/usr/local/bin/perl
 # script: remoteSOS

 use LWP::Simple;
 %SERVERS = (
	"Fred's server"   => 'http://www.fred.com',
	"Martha's server" => 'http://www.stewart-living.com',
	"Bill's server"   => 'http://www.whitehouse.gov'
	);
 $MAIL                 =  '/usr/sbin/sendmail';
 $MAIL_FLAGS           =  '-t -oi';
 $WEBMASTER            =  'webmaster';

 foreach (sort keys %SERVERS) {
    sos($_) unless head($SERVERS{$_});
 }

 sub sos {
   my $server = shift;
   open (MAIL,"| $MAIL $MAIL_FLAGS") || die "mail: $!";
   my $date = localtime();
   print MAIL <<END;
 To: $WEBMASTER
 From: The Watchful Web Server Monitor <nobody>
 Subject: $server is down

 I tried to call $server at $date but there was
 no one at home.

 Respectfully yours,

 The Watchful Web Server Monitor   
 END
   close MAIL;
 }


<< Previous Contents >> Next >>

Lincoln D. Stein, lstein@cshl.org
Cold Spring Harbor Laboratory
Last modified: Mon Aug 17 10:43:37 EDT 1998