According to the document of PHP official website, http://php.net/sys_getloadavg
sys_getloadavg
(PHP 5 >= 5.1.3)
sys_getloadavg — Gets system load average
So, it’s only for PHP >= 5.1.3. In PHP < 5.1.3, we use this function below:
<?php if (!function_exists(‘sys_getloadavg’)) { function sys_getloadavg() { if (@is_readable(‘/proc/loadavg’)) { //Linux if ($fh = @fopen(‘/proc/loadavg’, ‘r’)) { $data = @fread($fh, 6); @fclose($fh); $load_avg = explode(" ", $data); } } else { //Freebsd if ($serverstats = @shell_exec("uptime")) { $str = substr(strrchr($serverstats, ":"), 1); $load_avg = array_map("trim", explode(",", $str)); } } return $load_avg ? $load_avg : array(0, 0, 0); } } ?>
Note: This function is not implemented on Windows platforms.
Example of that function: sys_getloadavg
<? var_dump(sys_getloadavg()); ?>
With Windows platforms, we use this function below to get 1 minute load average:
function win_getloadavg() { $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); return (int)$load; }
To check operation system Windows, you use that code:
if (stristr(PHP_OS, 'win')) { //Your code }
1 Comment
PHP: calculate percentage of CPU load | Free Online Tutorials
(April 9, 2013 - 1:04 am)[…] Read first: Function sys_getloadavg in PHP < 5.1.3 […]