How do you calculate server load in PHP?

How do you calculate php / apache server load? I know that on the vBulletin forums the server load there loads like 0.03 0.01 0.04 , but this is not entirely clear for regular joe. So I thought about the scale of 1-100 percentiles. I wanted to display the visual load of a server that reads from a DIV:

 $load = $serverLoad*100; <div class=\"serverLoad\"> <div style=\"width: $load%;\"></div>//show visual for server load on a 1-100 % scale </div> <p>Current server load is $load%</p> </div> 

However, I do not know how to determine server load. Is it possible to enable loading of this server in the percentile scale? I don’t know where to start. Can someone please help me?

Thanks.

+4
source share
8 answers

I have a very old function that should still do the trick:

 function getServerLoad($windows = false){ $os=strtolower(PHP_OS); if(strpos($os, 'win') === false){ if(file_exists('/proc/loadavg')){ $load = file_get_contents('/proc/loadavg'); $load = explode(' ', $load, 1); $load = $load[0]; }elseif(function_exists('shell_exec')){ $load = explode(' ', `uptime`); $load = $load[count($load)-1]; }else{ return false; } if(function_exists('shell_exec')) $cpu_count = shell_exec('cat /proc/cpuinfo | grep processor | wc -l'); return array('load'=>$load, 'procs'=>$cpu_count); }elseif($windows){ if(class_exists('COM')){ $wmi=new COM('WinMgmts:\\\\.'); $cpus=$wmi->InstancesOf('Win32_Processor'); $load=0; $cpu_count=0; if(version_compare('4.50.0', PHP_VERSION) == 1){ while($cpu = $cpus->Next()){ $load += $cpu->LoadPercentage; $cpu_count++; } }else{ foreach($cpus as $cpu){ $load += $cpu->LoadPercentage; $cpu_count++; } } return array('load'=>$load, 'procs'=>$cpu_count); } return false; } return false; } 

This returns the processor load. You can also use memory_get_usage and memory_get_peak_usage to load memory.

If you can’t cope with the search for interest based on this ... sigh, just write and we will try together.

+12
source

This function in PHP can do this trick: sys_getloadavg () .

It returns three examples representing the average system load (the number of processes in the queue to start the system) for the last 1, 5, and 15 minutes, respectively.

+8
source
 function get_server_load() { $serverload = array(); // DIRECTORY_SEPARATOR checks if running windows if(DIRECTORY_SEPARATOR != '\\') { if(function_exists("sys_getloadavg")) { // sys_getloadavg() will return an array with [0] being load within the last minute. $serverload = sys_getloadavg(); $serverload[0] = round($serverload[0], 4); } else if(@file_exists("/proc/loadavg") && $load = @file_get_contents("/proc/loadavg")) { $serverload = explode(" ", $load); $serverload[0] = round($serverload[0], 4); } if(!is_numeric($serverload[0])) { if(@ini_get('safe_mode') == 'On') { return "Unknown"; } // Suhosin likes to throw a warning if exec is disabled then die - weird if($func_blacklist = @ini_get('suhosin.executor.func.blacklist')) { if(strpos(",".$func_blacklist.",", 'exec') !== false) { return "Unknown"; } } // PHP disabled functions? if($func_blacklist = @ini_get('disable_functions')) { if(strpos(",".$func_blacklist.",", 'exec') !== false) { return "Unknown"; } } $load = @exec("uptime"); $load = explode("load average: ", $load); $serverload = explode(",", $load[1]); if(!is_array($serverload)) { return "Unknown"; } } } else { return "Unknown"; } $returnload = trim($serverload[0]); return $returnload; } 
+3
source

If exec is activated

 /** * Return the current server load * It needs "exec" activated * * eg * 15:06:37 up 10 days, 5:59, 12 users, load average: 1.40, 1.45, 1.33 * returns * float(1.40) */ public function getServerLoad() { preg_match('#(\d\.\d+),[\d\s\.]+,[\d\s\.]+$#', exec("uptime"), $m); return (float)$m[1]; } 
+1
source
 <?php $load = sys_getloadavg(); if ($load[0] > 0.80) { header('HTTP/1.1 503 Too busy, try again later'); die('Server too busy. Please try again later.'); } ?> 
+1
source

Its quite easy in a LAMP environment if you have the appropriate permissions,

 print_r(sys_getloadavg()); 

OUTPUT: Array ([0] => 0 [1] => 0.01 [2] => 0.05)

Array values ​​represent the average load for the last 1, 5, and 15 minutes, respectively.

0
source

Here is the windows / linux compatible php server download function:

 function get_server_load() { $load = ''; if (stristr(PHP_OS, 'win')) { $cmd = 'wmic cpu get loadpercentage /all'; @exec($cmd, $output); if ($output) { foreach($output as $line) { if ($line && preg_match('/^[0-9]+$/', $line)) { $load = $line; break; } } } } else { $sys_load = sys_getloadavg(); $load = $sys_load[0]; } return $load; } 
0
source
 function _loadavg() { if(class_exists("COM")) { $wmi = new COM("WinMgmts:\\\\."); $cpus = $wmi->InstancesOf("Win32_Processor"); foreach ($cpus as $cpu) { return $cpu->LoadPercentage; } } } 
-1
source

Source: https://habr.com/ru/post/1347181/


All Articles