Forum Moderators: coopster

Message Too Old, No Replies

sharing this code snippet to check server status - added mail function

Some may find this useful, or have suggestions of a better way.

         

php4U

6:19 pm on Oct 19, 2007 (gmt 0)

10+ Year Member



The following is a nice code snippet that I found, that some may find useful. It creates a table and echo's the domain names entered, along with the status. I added a little bit to it so it will email you the notification when a server is down.

<?php
$urllist = array("http://example.com/", "http://example2.com/file.ext"); // set these to the files you want to check or remove the end part to check a full domain
set_time_limit(5000); // make sure the script doesn't time out too soon
echo("<table border='1'>"); // create a table
echo("<tr><th>#<th>URL<th>STATUS</tr>"); // more table stuff
for($i=0;$i<count($urllist);$i++){ // loops through each URL in $urllist
if($handle[$i] = @fopen($urllist[$i], 'r')){ // if the server responds
stream_set_timeout($handle[$i], 2); // set the timeout to 2 seconds so that it doesn't waste time
echo("<tr><td>".$i."</td><td><a href=" . $urllist[$i] . ">" . $urllist[$i] . "</a></td><td>Working</td></tr>\n"); // print the url and the status (working)
}
else{ // if the server does not respond

$message="$i\n\n$urllist[$i]";
mail("email@example3.com","Server is down", $message,"From: <notifications@example3.com>\nContent-Type: text/plain");

echo("<tr><td>".$i."</td><td><a href=" . $urllist[$i] . ">" . $urllist[$i] . "</a></td><td>Not Working</td></tr>\n"); // print url and status (not working)
}
}
echo("</tr></table><br>\n"); // end the table
?>

I was thinking of running this in the background as a cron job to periodically check the status of a few domains with known downtime status issues.

[edited by: eelixduppy at 5:30 pm (utc) on Oct. 22, 2007]

eelixduppy

8:49 pm on Oct 24, 2007 (gmt 0)



If I were you I would use CURL [php.net] to check the headers of the pages themselves without getting the whole body of the document. It should be faster.

php4U

1:16 am on Oct 25, 2007 (gmt 0)

10+ Year Member



Thank you for the link eelixduppy. That would make since to check the headers, and not the whole document. I will have to take a look at possibly doing something like this.

SeanW

1:45 am on Oct 25, 2007 (gmt 0)

10+ Year Member



I second the idea of using cURL instead.

[ibm.com...]

ties together some shell script, cURL, and RRDTool to graph the response of your server over time.

Sean

php4U

2:25 am on Oct 27, 2007 (gmt 0)

10+ Year Member



Thank you SeanW. I will check that out. These suggestions have been helpful.