<?php
    
    
// Max execute time
    
ini_set('max_execution_time'0);

    
/* I would keep the servers.php file as an include as it is used elsewhere if you choose to have the status information displayed. It's contents are similar to the following:
        
    $servers = Array();
    $servers[] = Array(
        'url' => 'http://00.00.00.00/status.php', // Full URL to the status.php file
        'id' => 'S1', // Short server ID
        'name' => 'Server 1', // Full Server Name
        'description' => 'Main Server', // Description of the server
        'alert_level' => 2 // Monitoring level: 0 = email level0 contacts only, 1 = email all contacts, 2 = email & text all contacts
    );
    */
    
include_once('servers.php');
    
    
$contacts = Array(
        
"level0_email" => Array(
            
"admin@some-company.com" // email address(s) of the first point of contact
        
);
        
"level1_email" => Array(
            
"someone-else@gmail.com"// additional email addresses
        
);
        
"sms" => Array(
            
"0123456789" // mobile number or possibly mobile email to send alerts to
        
);
    );

    foreach(
$servers as $server) {
        echo 
"Looking at server: " $server['name'] . "<br />";
        
$cached_status = @file_get_contents('status/server-' $server['id'] .'.txt');
        
$cached_status unserialize($cached_status);
        
        
$timer_start microtime(true);
        
$current_status file_get_contents($server['url']);
        
$timer_end microtime(true);
        
        
$new_status = Array();
        
$new_status['status'] = 'offline';
        if(
preg_match("/^apache=1;/"$current_status)) {
            
$new_status['status'] = 'online';
            
$status_parts explode(";"$current_status);
            foreach(
$status_parts as $stat) {
                if(!empty(
$stat)) {
                    list(
$stat_name$stat_value) = explode("="$stat);
                    
$new_status[$stat_name] = $stat_value;
                }
            }
            
$new_status['updated'] = time();
            
$new_status['time_taken'] = ($timer_end $timer_start);
            
            if(
                (isset(
$new_status['memory']) && $new_status['memory'] > 35// Memory over 35%
                
|| (isset($new_status['cpu2']) && $new_status['cpu2'] > 2// CPU Greater than 2 over past 5 minutes
                
|| ($new_status['time_taken'] > 0.2// Greater than 2 seconds execution
            
) {
                
$new_status['status'] = 'busy';
                echo 
"&nbsp;&nbsp;&nbsp;- This server is busy<br />";
            }
            
            if(
$new_status['mysql'] == '0') {
                
$new_status['status'] = 'offline';
                echo 
"&nbsp;&nbsp;&nbsp;- This servers mysql service is offline<br />";
            }
        } else {
            echo 
"&nbsp;&nbsp;&nbsp;- This servers apache or network services are offline<br />";
        }
        
        
// Update cache file - these are files stored in a "servers" folder (folder must be writable)
        
if($handle fopen('servers/server-' $server['id'] .'.txt''w')) {
            
$status serialize($new_status);
            
fwrite($handle$status);
            
fclose($handle);
        }
        
        if(
$new_status['status'] == 'offline') {
            echo 
"This server is <b>offline</b>";
            if(
$cached_status['status'] == 'offline') {
                echo 
"&nbsp;&nbsp;&nbsp;- Server has been offline for at least 4 minutes&hellip;<br />";
            } else {
                echo 
"&nbsp;&nbsp;&nbsp;- Server has been offline for no more than 3 minutes&hellip;<br />";
            }
            
// Emails
            
if($server['alert_level'] > && $cached_status['status'] == 'offline') {
                
$emails_array array_merge($contacts['level0_email'], $contacts['level1_email']);
            } else {
                
$emails_array $contacts['level0_email'];
            }
            
$offline_message "server Offline: " $server['name'] . "<br />Status Information:<br />";
            foreach(
$new_status as $status_key => $status_value) {
                
$offline_message .= "$status_key = $status_value<br />";
            }
            foreach(
$emails_array as $mail) {
                if(
mail($mail"Server Offline"$offline_message,
                    
"From: Server Status <nobody@localhost>\n".
                    
"X-Mailer: PHP/" phpversion() ."\n".
                    
"MIME-Version: 1.0\n".
                    
"Content-Transfer-Encoding: 7bit\n".
                    
"Content-Type: text/html; charset=ISO-8859-1\n\n")) {
                        echo 
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- Sent Notification Email to $mail<br />";
                    } else {
                        echo 
"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- <b>Failed</b> to send Notification Email to $mail<br />";
                    }
            }
            
// SMS
            
if($cached_status['status'] == 'offline' && $server['alert_level'] > 1) {
                foreach(
$contacts['sms'] as $mobile) {
                    
                    
/* The script you place here can depend on who your mobile provider is or if you choose to use a 3rd party system to send the messages */
                    
                
}
            }
        }
        echo 
"<br />";
    }

    echo 
"<br />-- All Servers Checked --";

?>