Last updated on November 17th, 2015 at 05:36 am

Simple script using PHP to get system load average. This script can be added to your website header so that whenever the server becomes busy end user will get a message stating that server is overloaded and please try again. This will help the server to process pending request and provide users fast response instead of a complete server crash. This script will check the server CPU load and display the output.

The code will look like this

$load = sys_getloadavg();
if ($load[0] > 30) {
    header('HTTP/1.1 503 Too busy, try again later');
    die("Server too busy. Please try again later. $load[0] is the current load on the server");
}
else
{
;
print_r($load);
$avg = $load[0]+$load[1]+$load[2];
$avg_load = $avg/3;
echo "Server looks good, Average load is $avg_load";
}

You can modify the above code according to your need and then save it as serverload.php. Then use include_once() function in php to load the webpage.

Leave a Reply

Your email address will not be published. Required fields are marked *