#!/usr/bin/perl
##############################################################
# BumbleBeeWare.com 2006
# server monitoring with perl
# program to access each server/website and give status response
# use cron to execute this script at a regular interval usually about 15 minutes
##############################################################
# setup array of servers/websites to check
@sitestocheck = ('mywebsite.com','mywebsite2.com');
# the relative url of the website response script in each site
$responseprogram = "/cgi-bin/mystatus3.cgi";
# path to the log file with the response data
$statusdir = "/pathto/statusdir"; # needs to be 0777 web writeable
# mail feature
$mailprog ='/usr/sbin/sendmail';
$adminmail = 'you@yourmail.com';
$frommail = 'websitemonitor@thisserver.com';
###############################################################
# End Configuration
###############################################################
# main program
use LWP::UserAgent;
# now check each url in your array
foreach $sitetocheck (@sitestocheck){
$ua = new LWP::UserAgent;
$req = new HTTP::Request 'GET',"http://$sitetocheck$responseprogram";
$res = $ua->request($req);
if ($res->is_success) {
if ($res->content =~ /I AM WORKING/i){$response = "SERVER OK - $sitetocheck
\n".$res->content;}
else {$response = "WARNING! There appears to be a problem, FIX ME!
\n";}
}
else {
$timestamp = localtime;
$response = "WARNING! UNABLE TO CONNECT TO $sitetocheck at $timestamp";
$traceroute = `/usr/sbin/traceroute $sitetocheck`;
}
# write server status to the main log file
open(FILE,">>$statusdir/statuslog.txt");
flock(FILE, 2);
print FILE "$response\n$traceroute\n\n";
flock(FILE, 8);
chmod 0777, $statusfile; # keep the file writeable
# write to a current status file for each server or website being monitored
open(FILE,">$statusdir/$sitetocheck");
flock(FILE, 2);
print FILE $response;
flock(FILE, 8);
chmod 0777, $statusfile; # keep the file writeable
}
# if there is an error mail the administrator
if ($response =~ /WARNING/i){
open( MAIL, "|$mailprog -t" );
print MAIL "Subject: Server or Website Not Responding\n";
print MAIL "From: $frommail\n";
print MAIL "To: $adminmail\n";
print MAIL "Reply-to: $frommail\n\n";
print MAIL "$response\n$traceroute";
print MAIL "\n\n";
close MAIL;
}
exit;