2011-04-07 220 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