Last updated on November 7th, 2024 at 09:21 am

This PHP code checks the current load on the server and takes different actions depending on whether the server is under heavy load or not. Let’s break it down step by step:

  • If the server’s 1-minute load is greater than 30, the script sends a 503 HTTP status (Service Unavailable) and terminates the script with a message saying the server is too busy.
  • If the server’s load is not too high, the script calculates and displays the average load for the last 1, 5, and 15 minutes, and indicates that the server is operating normally.

This code is useful for monitoring server load and preventing overload by limiting access when the server is under heavy load, while also providing load statistics for normal operations.

The code

$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 "\nServer looks good, Average load is $avg_load\n";
}

Code Breakdown:

Finally, the script outputs a message: "Server looks good, Average load is [avg_load]", where [avg_load] is the computed average load across the three periods.

Get the current system load: $load = sys_getloadavg(); The function sys_getloadavg() returns an array of three values representing the server’s load averages for the last 1, 5, and 15 minutes. For example, if $load = [5.34, 7.21, 6.98], then:

  • $load[0] is the 1-minute load average (5.34),
  • $load[1] is the 5-minute load average (7.21),
  • $load[2] is the 15-minute load average (6.98).

Check if the load exceeds a threshold:

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");
}

This condition checks if the 1-minute load average ($load[0]) is greater than 30. If it is, the server is considered too busy, and a 503 HTTP status (Service Unavailable) is returned.

The die() function is used to stop further execution of the script and displays the message: "Server too busy. Please try again later. [current load] is the current load on the server". This informs the user or client about the server’s high load and suggests they try again later.

If the server is not too busy, calculate and display the average load:

else {
    print_r($load);
    $avg = $load[0] + $load[1] + $load[2];
    $avg_load = $avg / 3;
    echo "Server looks good, Average load is $avg_load";
}

If the load is not over 30, the script proceeds to this else block.

print_r($load); prints out the full load array (i.e., the 1-minute, 5-minute, and 15-minute averages) for debugging or informational purposes.

The script then calculates the average of the three load values:

$avg = $load[0] + $load[1] + $load[2]; sums the three load averages.

$avg_load = $avg / 3; divides the sum by 3 to get the average load across the three intervals.