Zend - The PHP Company




Algorithms

Add Code


Fatal Error Notifier  

Type: application
Added by: kevinkorb
Entered: 19/03/2006
Last modified: 08/12/2006
Rating: - (fewer than 3 votes)
Views: 3745
This runs as a cron job and notifies the administrator of all the php fatal errors that happened in the last specified time. This is a low-overhead solution to fatal error reporting.... some solutions suggested output buffering each page, running a regex, but that has huge overhead. Others say no production code should ever have fatal errors. However, in my business we maintain many different sites each low budget and testing may not always be efficent. Sometimes a change on one file affects our code in more than one place and we forget to look somewhere and fatal errors happen. This has saved our butt a few times.


<?php
/**
 * This class was written because php's custom error handler does NOT
 * catch PHP Fatal Errors.  To me, Fatal Errors are a big deal.  So you set
 * the cron up every 30 min to run this.  It checks the error logs you listed
 * and within the last 30 min if there are any php fatal errors, it parses
 * them out and emails the specified address.
 * 
 * This file instantizes itself at the bottom, so all you have to call is this
 * file in your cron script.
 * 
 * CONFIGURATION....
 * Very Easy, just make sure that your temp location is writable by your
 * webserver, and make sure your $check_min is set to the same time as your
 * cron.  Every 15 min, 30 min etc.
 * 
 * Also this script was wrtten with the logformat: 
 * [Sat Mar 18 16:16:13 2006] [error] PHP Fatal error:
 * 
 * @author Kevin Korb - kevin@pearlmarley.com
 * @version .2
 */
Class phpFatal {

    var 
$check_min "30";
    var 
$log_location "/var/log/httpd/";
    var 
$temp_location "/library/webserver/CronScripts/temp/phpfatal.txt";
    var 
$logs_array = array("qpd_error_log""grmc_error_log""henrycounty_error_log"
                            
"foodguru_error_log""kirby_error_log""meddev_error_log",
                            
"blackdiamond_error_log""espebe_error_log""aroundtown_error_log");
                            
    var 
$email "you@yourdomain.com";
    var 
$error_log;
    var 
$output;
    var 
$total_output;
    var 
$from_email "Fatal Error Reporter <yourserver@yourdomain.com>nr";
    
    function 
phpFatal() {
        foreach (
$this->logs_array AS $log) {
            
$this->error_log $log;
            
$this->grep_log_to_temp();
            
$this->parse_file_for_results();
            if(
$this->output != '') {
                
$this->total_output .= "#############  {$this->error_log}  #############nn";
                
$this->total_output .= $this->output;
            }
        }
        if(
$this->total_output != '') {
            
mail($this->email"Fatal Errors Found"$this->total_output"From: {$this->from_email}");
        }
    }
    
    function 
grep_log_to_temp() {
        
        
system("grep 'PHP Fatal error' {$this->log_location}{$this->error_log} >> {$this->temp_location}");
    }
    
    function 
parse_file_for_results() {
        
$this->output '';
        
$file fopen($this->temp_location 'r');
        
        while (!
feof($file)) {
            
$line fgets($file4096);
            if(
$this->check_timestamp($line) == true) {
                
$this->output .= $line."n";
            }
        }
        
fclose($file);

        @
unlink($this->temp_location);
    }

    function 
check_timestamp($line) {

        
$current_time time();
        
$month_name substr($line,5,3);
        
        
$month $this->month_name_to_number($month_name);
        
        
$day substr($line,9,2);
        
$hour substr($line,12,2);
        
$min substr($line15,2);
        
$sec substr($line18,2);
        
$year substr($line21,4);
        echo 
"year = {$year}n";
        
        
        
$this_timestamp mktime($hour$min$sec$month$day$year);
        
$seconds_difference $this->check_min 60;
        
        if((
$current_time $this_timestamp) <= $seconds_difference) {
            return 
true;
        }
        else {
            return 
false;
        }
    }
        
    function 
month_name_to_number($month) {
        switch (
$month) {
            case 
"Jan": return '1'; break;
            case 
"Feb":    return '2'; break;
            case 
"Mar":    return '3'; break;
            case 
'Apr':    return '4'; break;
            case 
'May':    return '5'; break;
            case 
'Jun':    return '6'; break;
            case 
'Jul':    return '7'; break;
            case 
'Aug':    return '8'; break;
            case 
'Sep': return '9'; break;
            case 
'Oct';    return '10'; break;
            case 
'Nov':    return '10'; break;
            case 
'Dec': return '12'; break;
        }

    }    

}


$phpFatal = new phpFatal;

?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories