Get average server load

This function should work on Unix as on windows

function get_server_load() {
	$os = strtolower(PHP_OS);
	if(strpos($os, "win") === false) {
		 if(file_exists("/proc/loadavg")) {
			  $load = file_get_contents("/proc/loadavg");
			  $load = explode(' ', $load);
			  return $load[0];
		 }elseif(function_exists("shell_exec")) {
			  $load = explode(' ', `uptime`);
			  return $load[count($load)-1];
		 }
	}elseif(class_exists("COM")) {
		$wmi = new COM("WinMgmts:\\\\.");
		$cpus = $wmi->ExecQuery("Select * from Win32_Processor");
 
		$cpuload = 0;
		$i = 0;
		foreach($cpus as $cpu){
			$cpuload += $cpu->LoadPercentage;
			$i++;
		}
		$cpuload = round($cpuload / $i, 2);
		return "$cpuload";
	}
}

Recommended posts:


Tags : more-28

This entry was posted on Monday, January 1st, 2007 at 5:04 pm and is filed under Blog, Solutions. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


 Top