/** * Get server load ... if linux, returns all three averages, if windows, returns * average load for all CPU cores * * @return string|null */ public static function getServerLoad() { if (function_exists('sys_getloadavg')) { $a = sys_getloadavg(); foreach ($a as $k => $v) { $a[$k] = round($v, 2); } return implode(', ', $a); } else { $os = strtolower(PHP_OS); if (strpos($os, 'win') === false) { if (@file_exists('/proc/loadavg') && @is_readable('/proc/loadavg')) { $load = file_get_contents('/proc/loadavg'); $load = explode(' ', $load); return implode(',', $load); } else { if (function_exists('shell_exec')) { $load = @shell_exec('uptime'); $load = split('load average' . (PHP_OS == 'Darwin' ? 's' : '') . ':', $load); return implode(',', $load); //return $load[count($load)-1]; } else { return null; } } } else { if (class_exists('COM')) { $wmi = new COM("WinMgmts:\\\\."); $cpus = $wmi->InstancesOf('Win32_Processor'); $cpuload = 0; $i = 0; while ($cpu = $cpus->Next()) { $cpuload += $cpu->LoadPercentage; $i++; } $cpuload = round($cpuload / $i, 2); return $cpuload . '%'; } } } return null; }
function get_server_load($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); return $load[0]; } elseif(function_exists("shell_exec")) { $load = explode(' ', `uptime`); return $load[count($load)-1]; } else { return false; } } elseif($windows) { if(class_exists("COM")) { $wmi = new COM("WinMgmts:\\\\."); $cpus = $wmi->InstancesOf("Win32_Processor"); $cpuload = 0; $i = 0; while ($cpu = $cpus->Next()) { $cpuload += $cpu->LoadPercentage; $i++; } $cpuload = round($cpuload / $i, 2); return "$cpuload%"; } else { return false; } } }
function get_server_load($windows = 0) { $serverload = "n/a"; if (!($serverload = getCache("pool_load"))) { $numberOfCores = 8; $avgLoad = 0; $os = strtolower(PHP_OS); if (strpos($os, "win") === false) { if (file_exists("http://" . $poolname . "/loadavg.html")) { $load = file_get_contents("http://" . $poolname . "/loadavg.html"); $load = explode(' ', $load); $avgLoad = $load[0]; } elseif (function_exists("shell_exec")) { $load = explode(' ', `uptime`); $avgLoad = $load[count($load) - 1]; } //This may need to be adjusted depending on your system. This is assuming a dual core setup. if ($avgLoad > 1.9 * $numberOfCores) { $serverload = "critical"; } else { if ($avgLoad > 1.5 * $numberOfCores) { $serverload = "high"; } else { if ($avgLoad > 0.5 * $numberOfCores) { $serverload = "mid"; } else { if ($avgLoad > 0 * $numberOfCores) { $serverload = "low"; } } } } } elseif ($windows) { if (class_exists("COM")) { $wmi = new COM("WinMgmts:\\\\."); $cpus = $wmi->InstancesOf("Win32_Processor"); $cpuload = 0; $i = 0; while ($cpu = $cpus->Next()) { $cpuload += $cpu->LoadPercentage; $i++; } $cpuload = round($cpuload / $i, 2); $avgLoad = $cpuload; } if ($avgLoad > 90) { $serverload = "critical"; } else { if ($avgLoad > 66) { $serverload = "high"; } else { if ($avgLoad > 33) { $serverload = "mid"; } else { if ($avgLoad > 0) { $serverload = "low"; } } } } } if ($serverload != "n/a") { } setCache("pool_load", $serverload, 60); } return $serverload; }
function get_server_load($windows = 0) { if (class_exists("COM")) { $wmi = new COM("WinMgmts:\\\\."); $cpus = $wmi->InstancesOf("Win32_Processor"); $i = 1; // Use the while loop on PHP 4 and foreach on PHP 5 //while ($cpu = $cpus->Next()) { foreach ($cpus as $cpu) { $cpu_stats = 0; $cpu_stats += $cpu->LoadPercentage; $i++; } return round($cpu_stats / 2); // remove /2 for single processor systems } }
<?php $wmi = new COM("Winmgmts://"); $cpus = $wmi->InstancesOf("Win32_Processor"); $os = $wmi->InstancesOf("Win32_OperatingSystem"); foreach ($os as $os) { $os = "{$os->Caption} {$os->CSDVersion}"; } $os = preg_replace("/(Microsoft|\\(R\\)|, \\w+ Edition)/", "", $os); $os = str_replace("Service Pack ", "SP", $os); $system = $wmi->InstancesOf("Win32_ComputerSystem"); $ram = $wmi->InstancesOf("Win32_LogicalMemoryConfiguration"); foreach ($ram as $ram) { $ramtotal = $ram->TotalPhysicalMemory * 1024; } $ram = $wmi->InstancesOf("Win32_PerfRawData_PerfOS_Memory"); foreach ($ram as $ram) { } $ramused = $ramtotal - $ram->AvailableBytes; $ramused = mksize($ramused); $ramtotal = mksize($ramtotal); $uptime = $wmi->InstancesOf("Win32_PerfFormattedData_PerfOS_System"); foreach ($uptime as $uptime) { } $uptime = mkprettytime2($uptime->SystemUpTime); foreach ($cpus as $cpu) { $cpus1[] = $cpu->LoadPercentage; $totalusage += $cpu->LoadPercentage; } $cpucount = count($cpus1); $totalusage = round($totalusage / $cpucount, 2);