Here is another one that also works on windows. Note that this method is not fast, so be careful in the number of calls to this function.
<?php
function get_server_load() {
if (stristr(PHP_OS, 'win')) {
$wmi = new COM("Winmgmts://");
$server = $wmi->execquery("SELECT LoadPercentage FROM Win32_Processor");
$cpu_num = 0;
$load_total = 0;
foreach($server as $cpu){
$cpu_num++;
$load_total += $cpu->loadpercentage;
}
$load = round($load_total/$cpu_num);
} else {
$sys_load = sys_getloadavg();
$load = $sys_load[0];
}
return (int) $load;
}
?>
sys_getloadavg
(PHP 5 >= 5.1.3)
sys_getloadavg — Récupère la charge moyenne du système
Description
array sys_getloadavg
( void
)
Retourne trois échantillons représentant la charge moyenne du système (le nombre de processus dans le système qui est dans le processus de roulement en attente) depuis les dernières 1, 5 et 15 minutes, respectivement.
Valeurs de retour
Retourne un array avec trois échantillons (dernières 1, 5 et 15 minutes).
Exemples
Exemple #1 Exemple avec sys_getloadavg()
<?php
$load = sys_getloadavg();
if ($load[0] > 80) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
?>
Notes
Note: Cette fonction n'est pas implémentée sous Windows.
rick at rctonline dot nl
24-Jan-2012 12:57
828586 at gmail dot com
12-Jun-2010 07:43
equivalent for windows
<?php
ob_start();
passthru('typeperf -sc 1 "\processor(_total)\% processor time"',$status);
$content = ob_get_contents();
ob_end_clean();
if ($status === 0) {
if (preg_match("/\,\"([0-9]+\.[0-9]+)\"/",$content,$load)) {
if ($load[1] > get_config('busy_error')) {
header('HTTP/1.1 503 Too busy, try again later');
die('Server too busy. Please try again later.');
}
}
}
?>
Anonymous
19-Nov-2009 12:14
To get just current load avg, you can do :
<?php
$output = shell_exec('cat /proc/loadavg');
$loadavg = substr($output,0,strpos($output," "));
?>
scott at corelevel dot com
27-Nov-2006 02:47
I was having a problem with a large script I need to run - was a loop through about 50,000 records and downloading several pictures for a bunch of them, and updating the database.
the problem came as I started getting visitors to my site, the server would get behind, run out of memory, iowait skyrockets, mysql slows down... was a total downhill spiral.
Use this to fix it.
$load = sys_getloadavg();
$sleep=5;
$maxload=2;
if ($load[0] > $maxload) {
sleep($sleep);
echo "Busy server - sleep $sleep seconds<br>";
}
I have to play with the load and the sleep number to find what worked for my script, but now my server does not bog at all.
surfchen at gmail dot com
06-Jul-2006 09:22
the codes below will provide this function for order versions of PHP.
if (!function_exists('sys_getloadavg')) {
function sys_getloadavg()
{
$loadavg_file = '/proc/loadavg';
if (file_exists($loadavg_file)) {
return explode(chr(32),file_get_contents($loadavg_file));
}
return array(0,0,0);
}
}
tom pittlik
03-Mar-2006 02:36
The code below mimics the output of sys_getloadavg(). You may have to tweak the way the substring is captured for different distros.
<?
function sys_getloadavg_hack()
{
$str = substr(strrchr(shell_exec("uptime"),":"),1);
$avs = array_map("trim",explode(",",$str));
return $avs;
}
print_r(sys_getloadavg_hack());
// Array
// (
// [0] => 6.24
// [1] => 4.92
// [2] => 3.99
// )
?>
This function is a neat way of running low priority or non-essential cron jobs on a busy server - if the load is high, don't continue with the task (and try again in a few minutes time).
