Quick Monitor and Restart Script
hey guys, just thought I would share a simple script that will call a url on your server (it must be on the same server it is monitoring) and check for a certain response, then it will proceed to log and restart apache if the script does not get the proper outputYou can install this as a cron job to run every minute - make sure to set the permissions so the file can execute and you have to modify the URL and LOG FILE and commands to restart apache (the ones in the file for restarting apache will work on redhat, cobalt and other)
You should create a folder called /serverup and in there a file called index.html with JUST: SERVER IS UP and a line after it. All capitals and nothing else.
Heres the script:
#!/usr/bin/perl
## REPLACE THE URL BELOW ###########
$url = "http://www.yourdomain.com";
$log = "/home/admin/downlog.txt";
#######################################
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$ua->agent("HTTP/1.0");
my $req = new HTTP::Request('GET', '$url/serverup/index.html');
$req->content_type('text/html');
my $res = $ua->request($req);
$Result = $res->content;
chomp $Result;
$up = 1;
if ($Result ne "SERVER IS UP"){
$up = 0;
}
$date = `date`;
chomp $date;
if ($up eq "1"){
}else {
`/etc/rc.d/init.d/httpd stop`;
`/etc/rc.d/init.d/httpd start`;
open(LOG,">>$log");
print LOG "$date: HTTP Service Down [$Result]!\n";
close(LOG);
}
--------------------------
Remember, it's a 2 second job, if it decides to each your server for lunch and format the hard drive It's not my fault

Avi Brender