php 如何在没有 exec 的情况下获取 CPU 使用率和 RAM 使用率?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4705759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get CPU usage and RAM usage without exec?
提问by Rami Dabain
How does VBulletin get the system information without the use of exec
? Is there any other information I can get about the server without exec? I am interested in:
VBulletin 如何在不使用 的情况下获取系统信息exec
?在没有 exec 的情况下,我还能获得有关服务器的任何其他信息吗?我对感兴趣:
- bandwidth used
- system type
- CPU speed/usage/count
- RAM usage
- 使用的带宽
- 系统类型
- CPU 速度/使用率/计数
- 内存使用
回答by shamittomar
Use PHPSysInfolibrary
使用PHPSysInfo库
phpSysInfo is a open source PHP script that displays information about the host being accessed. It will displays things like:
phpSysInfo 是一个开源 PHP 脚本,用于显示有关正在访问的主机的信息。它将显示如下内容:
- Uptime
- CPU
- Memory
- SCSI, IDE, PCI
- Ethernet
- Floppy
- Video Information
- 正常运行时间
- 中央处理器
- 记忆
- SCSI、IDE、PCI
- 以太网
- 软盘
- 视频信息
It directly parsed parses /proc
and does not use exec
.
它直接解析 parses/proc
并且不使用exec
.
Another way is to use Linfo. It is a very fast cross-platformphp script that describes the host server in extreme detail, giving information such as ram usage, disk space, raid arrays, hardware, network cards, kernel, os, samba/cups/truecrypt status, temps, disks, and much more.
另一种方法是使用Linfo。这是一个非常快速的跨平台php 脚本,它极其详细地描述了主机服务器,提供了诸如 ram 使用情况、磁盘空间、raid 阵列、硬件、网卡、内核、操作系统、samba/cups/truecrypt 状态、临时文件等信息,磁盘等等。
回答by dhaupin
This is what I use on Linux servers. It still uses exec
, but other questions point here as duplicate, and there is no [good] suggestion for those. It shouldwork on every distro, but if it doesn't, try messing with $get_cores + 1
offset.
这是我在 Linux 服务器上使用的。它仍然使用exec
,但其他问题在这里指出重复,并且没有[好的]建议。它应该适用于每个发行版,但如果没有,请尝试使用$get_cores + 1
偏移量。
CPU in percent of cores used (5 min avg):
CPU 使用的内核百分比(平均 5 分钟):
$exec_loads = sys_getloadavg();
$exec_cores = trim(shell_exec("grep -P '^processor' /proc/cpuinfo|wc -l"));
$cpu = round($exec_loads[1]/($exec_cores + 1)*100, 0) . '%';
RAM in percent of total used (realtime):
RAM 占总使用量的百分比(实时):
$exec_free = explode("\n", trim(shell_exec('free')));
$get_mem = preg_split("/[\s]+/", $exec_free[1]);
$mem = round($get_mem[2]/$get_mem[1]*100, 0) . '%';
RAM in GB used (realtime):
已使用的 GB 内存(实时):
$exec_free = explode("\n", trim(shell_exec('free')));
$get_mem = preg_split("/[\s]+/", $exec_free[1]);
$mem = number_format(round($get_mem[2]/1024/1024, 2), 2) . '/' . number_format(round($get_mem[1]/1024/1024, 2), 2);
Here is what's in the $get_mem
array if you need to calc other facets:
$get_mem
如果您需要计算其他方面,这是数组中的内容:
[0]=>row_title [1]=>mem_total [2]=>mem_used [3]=>mem_free [4]=>mem_shared [5]=>mem_buffers [6]=>mem_cached
Bonus, here is how to get the uptime:
奖励,这是获得正常运行时间的方法:
$exec_uptime = preg_split("/[\s]+/", trim(shell_exec('uptime')));
$uptime = $exec_uptime[2] . ' Days';
回答by Stergios Zg.
<?php
function get_server_load()
{
$load=array();
if (stristr(PHP_OS, 'win'))
{
$wmi = new COM("Winmgmts://");
$server = $wmi->execquery("SELECT LoadPercentage FROM Win32_Processor");
$cpu_num = 0;
$load_total = 0;
foreach($server as $cpu)
{
$cpu_num++;
$load_total += $cpu->loadpercentage;
}
$load[]= round($load_total/$cpu_num);
}
else
{
$load = sys_getloadavg();
}
return $load;
}
echo implode(' ',get_server_load());
回答by Saud Iqbal
This is what I use for instant CPU usage without 1 second delay
这是我在没有 1 秒延迟的情况下用于即时 CPU 使用的内容
$cpu = shell_exec('top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*//" | awk \'{print 100 - }\'');
回答by Vishnu T Asok
after searching on forums and trying many methods, best accurate is this:
在论坛上搜索并尝试了多种方法后,最准确的是:
$stat1 = file('/proc/stat');
sleep(1);
$stat2 = file('/proc/stat');
$info1 = explode(" ", preg_replace("!cpu +!", "", $stat1[0]));
$info2 = explode(" ", preg_replace("!cpu +!", "", $stat2[0]));
$dif = array();
$dif['user'] = $info2[0] - $info1[0];
$dif['nice'] = $info2[1] - $info1[1];
$dif['sys'] = $info2[2] - $info1[2];
$dif['idle'] = $info2[3] - $info1[3];
$total = array_sum($dif);
$cpu = array();
foreach($dif as $x=>$y) $cpu[$x] = round($y / $total * 100, 1);
print_r($cpu);