Get average server load

This function should work on Unix as on windows

  1.  
  2. function get_server_load() {
  3.         $os = strtolower(PHP_OS);
  4.         if(strpos($os, "win") === false) {
  5.                  if(file_exists("/proc/loadavg")) {
  6.                           $load = file_get_contents("/proc/loadavg");
  7.                           $load = explode(' ', $load);
  8.                           return $load[0];
  9.                  }elseif(function_exists("shell_exec")) {
  10.                           $load = explode(' ', `uptime`);
  11.                           return $load[count($load)-1];
  12.                  }
  13.         }elseif(class_exists("COM")) {
  14.                 $wmi = new COM("WinMgmts:\\\\.");
  15.                 $cpus = $wmi->ExecQuery("Select * from Win32_Processor");
  16.                
  17.                 $cpuload = 0;
  18.                 $i = 0;
  19.                 foreach($cpus as $cpu){
  20.                         $cpuload += $cpu->LoadPercentage;
  21.                         $i++;
  22.                 }
  23.                 $cpuload = round($cpuload / $i, 2);
  24.                 return "$cpuload";
  25.         }
  26. }
  27.  

Tags :

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.

 

Leave a Reply


 Top