Thread: Load Alerts
View Single Post
Old March 23rd, 2006, 3:50 PM   #1 (permalink)
mr_fern
All Ur Base R Belong 2 Us
Excelling Contributor
 
mr_fern's Avatar
 
Joined in Feb 2005
Lives in Vegas & New York
824 posts
Gave thanks: 2
Thanked 6 times
Load Alerts

Well I decided to write myselfs some scripts to keep me informed of any issues with my servers. I figured I'd share them here.

The first is an Apache watchdog. It basically checks to see if Apache is running. If not, it attempts to restart it. It then sends a text message response regarding the success of the operation. I have tested everything but the restarting, so I won't post it yet.

But I did also create a basic Load alert system. It gets the current load of the system, and checks it against a configured value and then sends a text message if the load is higher than the configured value. It optionally also sends the current load value.

PHP Code:
<?php

/***********************************************************************
* Load Doctor v 0.1                                                    *
* (c) 2006 Pierre Fontenelle - http://brooklynrebel.com                *
*                                                                      *
* This program monitors your server's load and will alert you when     *
* the load is higher than a specified value. Alerts are sent via       *
* text messages to ensure prompt awareness.                            *
*                                                                      *
* Dependencies: uptime                                                 *
*                                                                      *
* Currently Supported Cell Companies:                                  *
*                                                                      *
* Sprint PCS (Sprint), Nextel (Nextel), AT&T (Cingular),               *
* Cingular (Cingular), T-Mobile (T-Mobile), Verizon (Verizon)          *
* Note: Parentheses stand for $service value                           *
*                                                                      *
* Valid Phone Number Formats:                                          *
* 1234567890, (123)456-7890, 123-456-7890, (123)456.7890, 123.456.7890 *                                                                       *                                                                      *
***********************************************************************/

// Enter Your Phone Number Here
// Valid Formats: (123)456-7890, 123-456-7890, (123)456.7890, 123.456.7890, 1234567890
$phone_number '123-456-7890';

// Enter Your Service Name Here
// Valids are listed at the top of file
$service 'Sprint';

// Subject
// Subject of Text Message
$subj 'Heartbeat Alert!';

// Max Load
// Set as a float.
// A decent load is CPUs * 1.00;
$load 0.00;

// Load Message: Sent when load is higher than desired value
$load_msg 'Your load has gone above the desired load value. ';

// Include Load:
// Set to 1 to include the current load value. 0 to omit it.
$include_load 1;

// Do Not Edit Below This Line
// Unless You Want To Of Course :)

if (isset($_SERVER['REMOTE_ADDR'])) // Make sure it's run through Command Line
    
die('This program is not intended to be run through a browser');

$uptime exec('uptime'); // get uptime data
$start strpos($uptime'load average:') + strlen('load average: ');
$loads substr($uptime$start);
list(
$current_load$five_minute_load$fifteen_minute_load) = explode(', '$loads);

$txtmsg_address get_message_addr($phone_number,$service); // prepares text message e-mail address

    
if ($current_load $load)
    {
        
$load_msg .= ($include_load) ? 'Current Load: '.$current_load ''//append current load
        
mail($txtmsg_address$subj$load_msg);
    }

function 
get_message_addr($number,$service) {

    
$number preg_replace('/[^0-9]/','',$number); // strip non digits
    
$service strtolower($service);
    
    
$services = array();
    
$services['sprint'] = 'messaging.sprintpcs.com';
    
$services['nextel'] = 'messaging.nextel.com';
    
$services['cingular'] = 'cingularME.com';
    
$services['t-mobile'] = 'tmomail.net';
    
$services['verizon'] = 'vtext.com';
    
    return 
$number     .'@'$services[$service];
}

?>
I had a weird issue occur at one point during testing. It stalled on one instance and shot the load up. It was due to the mail sending, however I think it was a result of the headers I was trying to send (which don't work for the email to sms conversion), so the headers were removed.

The script's intended to be ran as a minute interval cron job

Example Crontab Line:
* * * * * php -q /path/to/load_checker.php

Nothing complex, pretty simple script. If you decide to use it let me know, and let me know how things work out.

And if you have any suggestions, let me hear 'em

You can download a copy from the attached zip (The file header comments don't seem to show in the php code block
__________________
Nobody doing nothing
mr_fern is offline   Reply With Quote