Home » Articles » A Simple Server ‘Load Average’ Monitor PHP Script

A Simple Server ‘Load Average’ Monitor PHP Script

icontexto-message-types-alert-orange_256x256-150x150I have always had to SSH into my web server to check load averages. Today, I created a very simple PHP script that can check the load averages and display it on an HTML page. This has been tested to work in Ubuntu Server 12.04 and PHP 5.3 but with a little bit of tweaking, you should be able to make this work in any *nix-based system.

Additionally, the script can send out an alert via email once the server load reaches a certain level in the past 15 minutes. Once you have setup this up on your page, you can go to any browser to access your server’s load averages and uptime. You can also setup a cron job to run this script every 5 minutes so that you will receive email alerts. Thanks to Lewcy from Webhostingtalk.com for providing the initial code. Enjoy!

<?php
// Simple Load Average Monitor for Linux
// April 11, 2013 Deer Creek Enterprise Limited
// http://www.deer-creek.ca
$min_warn_level = 3; // Set to min load average to send alert
$email_recipient = "your@email.com"; // Set to address of alert recipient
$current_reading = @exec('uptime');
preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/", 
 $current_reading, $averages);
$uptime = explode(' up ', $current_reading);
$uptime = explode(',', $uptime[1]);
$uptime = $uptime[0].', ' . $uptime[1];
$data = "Server Load Averages $averages[1], $averages[2], $averages[3]\n";
$data .= "Server Uptime $uptime";
if ($averages[3] > $min_warn_level ) {
 $subject = "Alert: Load average is over $min_warn_level";
 mail($email_recipient, $subject, $data);
}
echo $data;
?>