Skip to content

Instantly share code, notes, and snippets.

@davidrenne
Created February 24, 2012 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidrenne/1903668 to your computer and use it in GitHub Desktop.
Save davidrenne/1903668 to your computer and use it in GitHub Desktop.
Nice utility to scan real-time all your php fatals and parse errors in production
Edit cron tab to point to this file and run php script every one minute:
*/1 * * * * php /yourpath/phpFatals.php web01
*/1 * * * * php /yourpath/phpFatals.php cron01
*/1 * * * * php /yourpath/phpFatals.php etcetc
Edit the below variables:
<?php
$mainEmail = "asdfasfd@sadfadsfasfd.com";
$clearLogsAt = 52428800;
$logPath = "/var/log/php_error.log";
$date = date("d-M-Y H:i", strtotime("-1 minute"));
$out = "/tmp/fatal_".str_replace(array('-',' ',':'), array('_','_','_'),$date).".out";
system("nice grep '$date' $logPath | grep 'Fatal' > $out");
system("nice grep '$date' $logPath | grep 'PHP Parse error' >> $out");
$results = file_get_contents($out);
echo $results."\n";
if (!empty($results))
{
mail($mainEmail,"PHP Fatals 'n Parse Errors On - ".$argv[1],"PHP Fatals 'n Parse Errors for $date Minute:\n\n".$results);
}
unlink($out);
if (filesize($logPath) > 52428800)
{
system("nice cp $logPath /var/log/".date("d_M_Y_H_i")."_php_error.log");
system("echo '' > $logPath");
mail($mainEmail,"PHP Error Log Cleared at 50MB - ".$argv[1],"PHP Error Log Cleared at 50MB - ".$argv[1].". Historical backup located here: /var/log/".date("d_M_Y_H_i")."_php_error.log");
}
?>
The script has been tested even if the error log is logging like crazy the monitor will clear out the log once it exceeds to keep the I/O lightweight (cause original version spiked server when rogoue error logs happened)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment