Press "Enter" to skip to content

Check web page contents

Another simple script I’ve written and use often (actually runs via cron every 5 minutes) to verify the contents of a particular page to determine of it has changed for any reason like a webserver cracked/hacked or if the dynamic content is not what is expected.

In this case it is specifically monitoring a page from a PeopleSoft application that I do not entirely trust, if the content doesn’t match my checksum that generally means the Tuxedo application server is hosed and needs to be restarted.

Easily adapted to fit your particular needs, is use it on all of my public websites that are on shared hosting accounts etc…

Comments for bigger sections are thrown in to give you an idea of what to tweak for your needs, remember to update $file2 with a new copy of the markup if you modify the landing page you are monitoring!


# This just checks what the current working directory is,
# if dev or test then set the debug flags to on…etc.
unshift(@INC, “/pshome/psmgr/bin”);
unshift(@INC, “/pshome/psmgr/bin.test”) if (“/pshome/psmgr/bin.test” eq “$ENV{‘PWD’}”);
unshift(@INC, “/pshome/psmgr/stat/bin”) if (“/pshome/psmgr/stat/bin” eq “$ENV{‘PWD’}”);
require sr;

$debug = 1;
$debug = 1 if (“/home/bin.test” eq “$ENV{‘PWD’}”);
$debug = 1 if (“/home/stat/bin” eq “$ENV{‘PWD’}”);
$execute = 1;

$file1 = “/home/files/sitename.tmp”;
$file2 = “/home/files/sitename_known_good.txt”;
$url = “”;
$url = “” if (“/pshome/psmgr/bin.test” eq “$ENV{‘PWD’}”);
$url = “” if (“/pshome/psmgr/stat/bin” eq “$ENV{‘PWD’}”);
$restarting = “/pshome/psmgr/files/careers.restarting”;
$restartingold = “/pshome/psmgr/files/careers.restarting.old”;
$check = “0”;

# check to see if the restart is already in progress so we don’t interrupt it
# likely a cleaner way to do this, but it works for now!
if (-e $restarting) { # check if restart file exists
print “Restart file exists, servers may be restarting, check again in 5 minsn”;
print “Renaming file and Exitingn”;
$cmd = “mv $restarting $restartingold”;
print “$cmdn” if ($debug);
system ($cmd) if ($execute);
} #end if restart file exists

if (-e $restartingold) { # check if restarting.old file still exists
print “Restarting file existed for at least 5 mins, servers should be back up, rechecking”;
print “Remove restart filen”;
$check = “1”;
$cmd = “rm $restartingold”;
print “$cmdn” if ($debug);
system ($cmd) if ($execute);
} #end if restarting.1 file exists

$cmd = “wget -b –no-check-certificate –output-document=$file1 $url”;
print “$cmdn” if ($debug);
system ($cmd) if ($execute);

$cmd = “sleep 15”;
print “$cmd – sleeping 15 secs to allow complete xfer of filen” if ($debug);
system ($cmd) if ($execute);

$diff1=`cksum $file1`;
$diff2=`cksum $file2`;
$diff1value = substr($diff1, 0, 9);
$diff2value = substr($diff2, 0, 9);

print “diff1value = $diff1valuen” if ($debug);
print “diff2value = $diff2valuen” if ($debug);

if ($diff1value != $diff2value) {
} else {
print “Files match, this site is up!n”;
print “check = $checkn” if ($debug);
if ($check == “1”) { #check if recovering from restart
} # end check if recovered from restart

sub notifydown
print “This site appears to be down, sending page and emailn” if ($debug);
&sr::send_email($debug,”Site Down!”,”Site is down, restarting app servers now!”,”David Cochran”);
&sr::send_page($debug,”Careers site is down, restarting app servers now!”,”David Cochran”);
} # end sub notifydown

sub repair
{ # restart HRMSEXT App servers
$cmd = “mv $file1 $restarting”;
print “$cmdn” if ($debug);
system ($cmd) if ($execute);
$cmd = “/commands to restart the application server go here”;
print “$cmdn” if ($debug);
system ($cmd) if ($execute);
} #end sub repair

sub cleanup
{ # cleanup temp files
$cmd = “rm -f wget-log*”;
print “$cmdn” if ($debug);
system ($cmd) if ($execute);
$cmd = “rm -f $file1”;
print “$cmdn” if ($debug);
system ($cmd) if ($execute);
} #end sub cleanup

sub notifyup
print “site is back up, sending page and email.n” if ($debug);
&sr::send_email($debug,”site is OK!”,”Careers site is OK.”,”David Cochran”);
&sr::send_page($debug,”Careers site is back up”,”David Cochran”);
} # end sub notifyup