Server Status Script

phool4fool

203.***.***.***
1,936 days ago

Server Status Script

This simple PHP script will attempt to connect to port 80 on a server and if port 80 is unresponsive the script will send out an e-mail to a specified e-mail address.

PHP Example:

PHP code:

//Websites to check the status of. You can list as many as you want. If you want to check the status of only one site remove the others.

$sites = array();
$sites[] = 'cnn.com';
$sites[] = 'example.com';
$fails = array();

foreach($sites as $site)
{
$sock = fsockopen($site, 80, $errno, $errstr, 5);

if (!$sock)
{
$fails[] = $site;
}
}

if(count($fails) > 0)
{
// The e-mail address you want the report of a unresponsive server sent to. Example: 4251234567@tmomail.net.
// You can send the report to multiple addresses, i.e.: 4251234567@tmomail.net,7035551234@messaging.nextel.com.

$to = "4251234567@tmomail.net";

//This code sends out the e-mail.

$subject = "Server Status: " . count($fails) . " sites down";
$message = "The following " . count($fails) . " sites failed to respond on " . date("r") . ":\r\n\r\n";

foreach($fails as $failed)
{
$message .= $failed . "\r\n";
}

mail($to, $subject, $message);
}

mtajim

202.***.***.***
1,832 days ago
Thats pretty good script, but whats the use of this scripts can u plz explain in detail, I am a bit newbie in php

bdm

213.***.***.***
1,825 days ago
it's just a simple socket check script..

1: make an array ( list ) , line 0-5
2: cycle the host listed in the list , line 7
3: it opens a sock ( connection ) to the host, line 9
4: if statement, on the sock, is it open or no .. if no sends a mail , line 23
5: stop.

for PHOOL4FOOL, Why make X mails when you can send only one ?

Drack

66.***.***.***
1,805 days ago
The script has an error. If site is down PHP function cannot connect no email is sent.

Bill

84.***.***.***
977 days ago
I improved the script a little, hope no one minds. I mostly cleaned up the formatting and changed the code to only send one e-mail with a list of all the failed connections. The e-mail report now also contains a little more information.

buffalokill

66.***.***.***
938 days ago
So do you need to access this script manually or via cron job in order for it to check the server?

Bill

84.***.***.***
936 days ago
Welcome to the forum buffalokill;

Either would work of course, but without a cron job the point of the script is gone since then you might as well just skip the middle man and check the site manually.