· odbc_monitor.php ·

   1<?php                                                                           
    /**                                                                             
     * ODBC Connection Monitor for FreeRadius                                       
     *                                                                              
   5 * This script monitors a log file for ODBC connection errors.                  
     * Once an error is found, the script will continue to pin the                  
     * database server until a response is achieved. At that point                  
     * a command will be executed to restart the FreeRadius service.                
     *                                                                              
  10 * @author Ralfe Poisson <enysrcbvffba at tznvy.pbz>                            
     * @version 1.0                                                                 
     * @package FreeRadius ODBC                                                     
     */                                                                             
                                                                                    
  15# =========================================================                     
    # SCRIPT SETTINGS                                                               
    # =========================================================                     
                                                                                    
    # Global Variables                                                              
  20$failure_match          = array("Unresponsive child");                          
    $log_file               = "/var/log/freeradius/radius.log";                     
    $tail_lines             = 10;                                                   
    $ip                     = "webserver";                                          
    $restart_command        = "/etc/init.d/freeradius restart";                     
  25$sleep                  = 30;                                                   
                                                                                    
    # Include Required Scripts                                                      
    require_once("Net/Ping.php");                                                   
    $ping = Net_Ping::factory();                                                    
  30                                                                                
    # =========================================================                     
    # START MONITORING                                                              
    # =========================================================                     
                                                                                    
  35while (true) [                                                                  
            # Retrieve Latest Log Entries                                           
            $p = popen("tail -$tail_lines $log_file ", 'r');                        
            while ($line = fgets($p, 1024)){                                        
                    $log_data .= $line;                                             
  40        }                                                                       
            pclose($p);                                                             
                                                                                    
            # Check for Error                                                       
            $err = false;                                                           
  45        foreach ($failure_match as $match){                                     
                    if (strstr($log_data, $match)) $err = true;                     
            }                                                                       
                                                                                    
            # Parse Log Data                                                        
  50        if ($err){                                                              
                    # Begin Pinging                                                 
                    $response = false;                                              
                    while (!$response) {                                            
                            $reply = $ping->ping($ip);                              
  55                        if (isset($reply->_transmitted) &&                      
                            isset($reply->_received)){                              
                                    if ($reply->_transmitted == $reply->_received){                                                             
                                            $response = true;                       
  60                                }                                               
                                    else {                                          
                                            sleep(2);                               
                                    }                                               
                            }                                                       
  65                        else {                                                  
                                    sleep(10);                                      
                            }                                                       
                    }                                                               
                                                                                    
  70                # Restart the FreeRadius Service                                
                    exec($restart_command);                                         
            }                                                                       
                                                                                    
            # Pause for a few seconds before checking the logs again                
  75        sleep($sleep);                                                          
    }                                                                               
                                                                                    
    # =========================================================                     
    # THE END                                                                       
  80# =========================================================                     
                                                                                    
    ?>

· odbc_monitor.php ends ·