Hi folks,
Our server kept going down so I wrote this to stick on another server so it could moniter if our main site was up or down, if there is a change in the status it sends me a text message but that function has been removed from this one - it will just send an email right now.
Please let me know your thoughts, hope it comes in handy.
It's only small so here's the code but it's also available as a zip with the plain text file to log the status on.
Regards
Andy
<?php
// author andrew moore - [email]andrew@palacemarketinglimited.com[/email]
// last updated may 18th 2005
// simple script to check the availability of a remote server
// this should be executed by cron every minute, less if you prefer
// there are more comments than code.....
// no licence it's code so just do with it what you need and want.....
// if you like it a lot you can always send me nice stuff in the post.....
// requirements:
// php
// sockets / fopen enabled
// url to monitor on one server
// url on another server to do the monitoring from
// instructions
// set the variables $url $urlname $userfile and $email below
// upload remote-check.php to the server to do the checking from
// upload server-status.txt to the same directory
// chmod 777 server-status.txt so we can write data to it
// test in the browser
// setup a cron job to run every minute of every day
// * * * * * lynx -dump [url]http://www.url-on-your-second-server.com/remote-check.php[/url]
// variables used - please edit before uploading and testing
// specify the url you wish to check the availability of
$url = "http://www.your-url.com/";
// specify a friendly name for the site you are monitoring
$urlname = "Friendly name for URL";
// specify the file to read from and write to
// remember this must be chmodded to 777
$userfile = "server-status.txt";
// specify the email address to send up and down reports to
// remember that if you server is down and that is where this email is hosted it's pointless.....
$email = "your.email@some.isp.com";
// ends variable configurations
// nothing else needs changing below this point unless you wish to add sending of sms text messages or advanced logging.....
// open the url to check
// this is just a simple fopen
// if it opens okay the status is up
// if it fails to open the status is down
if((@fopen("$url","r"))){
$status = "up";
}else{
$status = "down";
}
// now we read the contents of the text file to see what the server status was after it changed last
$serverresult = file_get_contents($userfile);
// echo some results to the browser
echo "$urlname status now: $status
<br/>
$urlname status at last check: $serverresult
<br/>";
// check if the status now is the same as the last time we ran
if($status=="$serverresult"){
echo "$urlname has not chnged status";
}else{
echo "$urlname status has changed, was $serverresult is now $status
<br/>";
// formulate the message to send
$messagebody = "$urlname is $status";
// send email to alert in the change of status
$subject = "IPALERT - $messagebody";
$header = "$subject";
if(mail("$email", "$subject", "$messagebody", "From: $email")){
echo "Email sent to $email to alert of change of status";
}
// to send a sms text message you will need an account with a suitable aggregator
// see [url]http://forums.devshed.com/t178113/s.html[/url] for a list of possible sms providers
// becuase the status has changed write this to the file so we know for next time we check
$file = fopen($userfile, 'w');
fwrite($file, $status);
fclose($file);
// ends status has changed if statement
}
?>