2011-04-07 219 views
4

你如何計算PHP/Apache的服務器負載?我知道在vBulletin論壇上顯示的服務器負載類似於0.03 0.01 0.04,但對於一般的joe來說這是不能理解的。所以我想到了一個1-100百分位的比例。我想顯示從DIV讀取的服務器負載視覺:如何計算PHP中的服務器負載?

$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> 

但是,我不知道如何檢測服務器負載。是否有可能將此服務器負載轉換爲百分比規模?我不知道從哪裏開始。願有人能幫助我嗎?

謝謝。

+3

的原因,這三個值存在是一個百分比指標並沒有真正告訴你什麼。 – 2011-04-07 23:08:17

回答

12

我有一個很老的功能還是應該做的伎倆:

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; 
} 

這將返回處理器負載。您還可以使用memory_get_usagememory_get_peak_usage來加載內存。

如果您無法處理基於此...嘆息的發現百分比,只需發佈​​,我們將一起嘗試。

3
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; 
} 
1

如果 「高管」 被激活

/** 
* Return the current server load 
* It needs "exec" activated 
* 
* e.g. 
* 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]; 
} 
8

這個函數在PHP可能做到這一點伎倆:sys_getloadavg()

分別返回表示平均系統負載(系統運行隊列中的進程數)在過去的1,5和15分鐘內的三個樣本。

-1
function _loadavg() 
{ 
    if(class_exists("COM")) 
    { 
     $wmi = new COM("WinMgmts:\\\\."); 
     $cpus = $wmi->InstancesOf("Win32_Processor"); 

     foreach ($cpus as $cpu) 
     { 
     return $cpu->LoadPercentage; 
     } 
    } 
} 
0

它很容易在LAMP環境如果有適當的權限,

print_r(sys_getloadavg()); 

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

數組值分別是過去1,5和15分鐘的平均負載。

0

這裏是一個Windows/Linux的兼容PHP的服務器負載功能:

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; 
} 
1
<?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

雖然此代碼片段可能會解決問題,但[包括解釋](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers)確實有助於提高帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 - [來自評論](https://stackoverflow.com/review/low-quality-posts/12011689) – Ferrybig 2016-04-14 07:49:45

+0

賞識!謝謝你的指導,一定要記住 – SagarPPanchal 2016-04-14 09:19:49