From Freelock Knowledge Base
#!/usr/bin/php -q
<?php
/* Copyright 10/30/2006 freelock computing
*
* chkdomains.php
* basic site monitoring script
* sends an email when status is different than previous running of the script
***
* installation instructions
* create a /var/www/conf directory to store this
* /var/www/conf/satus contains the cache result
* this must be writable by the user running the job
* add a URL to the $site array for each site to check
* set $mailto to appropriate email addresses, comma seporated
* schedual the cron job (php command line package required php_cli)
*
* The status file needs to be writiable by the script ( a user accont
* that runs the script)
*
* on your server that is to be checked, create a textfile called "FILENAME" with your success string, this must match exactly, newlines and whitespace matter.
***
* Testing instructions
* delete or change the contents of the domain_uptime.txt
* run the script
* check the contents of the new domain_uptime.txt file
* check your email
*
***
* files,
* on each server, uptime.txt
* on monitoring system
/var/www/conf/chkdomains.php (this file)
* /var/www/conf/status/domain_uptime.txt conists of 1's and 0's seporated by newlines, this is rewritten every run. 1's represent server being up, 0's represent server being down.
*/
# fully qualified URLS of http servers with a trailing slash
$site[] = "http://yoursite1.com/";
$site[] = "http://yoursite2.org/";
$site[] = "http://yoursite3.net/";
# email address(es) seporated by commas
$mailto = "sys-admin@yourdomain.com,optionaladditional@email.com";
#file to check from servers listed above
define('FILENAME', "uptime.txt");
#file contents must match exactly
define('SUCCESS', "success");
#file to store the status of the last run
define('STATUS_FILE', '/var/www/conf/status/domain_uptime.txt'); #
$out = '';
for ($i=0; $i<count($site); $i++)
{
$filename = $site[$i] . FILENAME;
if ($fp=@fopen($filename,'r')){
$err = socket_get_status($fp);
if ($err['timed_out']) {
$txt = "$filename timed out\n";
$status[$i]= 0;
}
else
{
$text = trim(fread($fp, 2048));
$txt = $filename. ': ' . $text . '(' . ($text==SUCCESS) . ")\n";
$status[$i]=1;
}
fclose($fp);
} else {
$txt = "$filename not opened\n";
$status[$i]=0;
}
$out .= $txt;
}
$changed = false;
if ($oldstatus= @file(STATUS_FILE))
{
for ($i=0;$i<count($oldstatus)&&!$changed;$i++)
{
if ($status[$i]!=trim($oldstatus[$i]))
{
$changed = true;
}
}
}
else
{
$changed = true;
}
if ($changed)
{
$fp = fopen(STATUS_FILE, 'w');
for ($i=0;$i<count($status); $i++)
{
fwrite($fp, $status[$i] . "\n");
}
fclose($fp);
mail( $mailto , 'Domain Uptime Report: Status Change', $out);
}
?>