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
}

?>
    a month later

    One thing I noticed is that you're outputting data "to the browser", I would imagine that this script would be most usefull run as a cron job, in which case there is little point in output. Another point which should be noted is that undirected output from programs run as cron jobs is emailed internally (added to the mail spool) of the user which runs the program. If this program is run often for a long time the mail spool for the running user will gradually get bigger and bigger.

    Another thing I though might be pretty cool for extending it's functionality was sockets and response codes. If you used fsockopen to port 80 and built the http request yourself etc. you'd be able to return some slightly more detailed results, the return code for example. You'd also be able to do things like ping the server to determine whether it's just the webserver which is down or the whole box. You could build in a check on the database as well, so even if the site is appears up but the database is down you get notified.

    On the other hand, the more you get notified the more you have to respond ... I'd be cautious 😉 :rolleyes:

      Write a Reply...