Being the keeper of several sites, I’ve written a few scripts to aid in monitoring these sites. The monitors generate an email for whatever particular event, and usually run periodically from a cron job. This PHP Error Log Monitoring script scans the site for any error logs, then emails the list of discovered logs to whomever for handling.
The basic premise is to scan the directory structure for the existence of an error_log anywhere, rename it so we will not discover it again, then email the discovery report. We need to set a few parameters:
global $params;
$params=array
('BaseLogName'=>'error_log'
,'BaseScanPath'=>'/public_html/' //always include trailing '/'
,'EMailToAddr'=>'admin@site.com'
);
Now, we initiate a recursive scan of the BaseScanPath:
global $Logs; $Logs=array(); //list of discovered files iScan($params['BaseScanPath']); //start here
The iScan function will get a list of the files in the given path. The list is then scanned to discover more directories to check, or to find the BaseLogName:
protected function iScan($aHere)
{
global $params,$Logs;
$d=dir($aHere);
while (false !== ($entry = $d->read())) {
if ($entry=="." or $entry=="..") continue;
$f=$aHere.$entry;
if (is_dir($f)) iScan($f.'/');
else if ($entry==$params['BaseLogName']) $Logs[]=$f;
}//while
$d->close();
}
When the ‘start here’ call to iScan returns, $Logs has a list of discovered error logs, if any. If there were none, we’re done, otherwise rename with the current timestamp and generate an email:
if (0==count($Logs)) exit; //nothing found
$ts="-".date('Ymd-His').'.txt';
$msg="Error log(s) found at site.com:\r\n\r\n";
foreach($Logs as $f) {
rename($f,$f.$ts);
$msg.=' '.$f."\r\n";
}
$msg.="\r\n\r\nLogs renamed to {$params['BaseLogName']}$ts\r\n";
mail($params['EMailToAddr'],'Error Logs',$msg);
All that is left is to add it to the cron job. I call the script once an hour, so if any new errors are happening, I’ll know fairly quickly.
I’ve given you all the essentials in the above code. My own final script puts the process into a class structure, and has a few additional features, like setting a custom subject for the email, setting the ‘from’ email address, automatically identifying the scanned site, and the option to attach the error logs. I’ve installed the script on all the sites I monitor with great success!

