如何在不执行exec的情况下获取CPU使用率和RAM使用率?

发布于 2024-10-11 23:38:45 字数 170 浏览 4 评论 0原文

VBulletin如何在不使用exec的情况下获取系统信息?在没有 exec 的情况下我可以获得有关服务器的其他信息吗?我感兴趣的是:

  • 使用的带宽
  • 系统类型
  • CPU 速度/使用情况/计数
  • RAM 使用情况

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:

  • bandwidth used
  • system type
  • CPU speed/usage/count
  • RAM usage

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

咽泪装欢 2024-10-18 23:38:45

使用PHPSysInfo

phpSysInfo是一个开源PHP脚本,可以显示有关主机的信息已访问。它将显示如下内容:

  • 正常运行时间
  • CPU
  • 内存
  • SCSI、IDE、PCI
  • 以太网
  • 软盘
  • 视频信息

它直接解析 /proc 并且不使用 exec


另一种方法是使用Linfo。这是一个非常快速的跨平台 php 脚本,非常详细地描述了主机服务器,提供了诸如 RAM 使用情况、磁盘空间、raid 阵列、硬件、网卡、内核、操作系统、samba/ 等信息。 cups/truecrypt 状态、临时文件、磁盘等等。

Use PHPSysInfo library

phpSysInfo is a open source PHP script that displays information about the host being accessed. It will displays things like:

  • Uptime
  • CPU
  • Memory
  • SCSI, IDE, PCI
  • Ethernet
  • Floppy
  • Video Information

It directly parsed parses /proc and does not use exec.


Another way is to use Linfo. It is a very fast cross-platform php 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.

初与友歌 2024-10-18 23:38:45

这是我在 Linux 服务器上使用的。它仍然使用 exec,但其他问题指向这里是重复的,并且对于这些问题没有[好的]建议。它应该适用于每个发行版,但如果不能,请尝试使用 $get_cores + 1 偏移量。

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 占总使用百分比(实时):

$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 以 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);

如果您需要计算,这里是 $get_mem 数组中的内容其他方面:

[0]=>row_title [1]=>mem_total [2]=>mem_used [3]=>mem_free [4]=>mem_shared [5]=>mem_buffers [6]=>mem_cached

奖金,以下是获得正常运行时间的方法:

$exec_uptime = preg_split("/[\s]+/", trim(shell_exec('uptime')));
$uptime = $exec_uptime[2] . ' Days';

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 should work on every distro, but if it doesn't, try messing with $get_cores + 1 offset.

CPU in percent of cores used (5 min avg):

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

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

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

[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';
怪我鬧 2024-10-18 23:38:45
<?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());
<?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());
但可醉心 2024-10-18 23:38:45

这就是我用于即时 CPU 使用情况而没有 1 秒延迟的方法

$cpu = shell_exec('top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk \'{print 100 - $1}\'');

This is what I use for instant CPU usage without 1 second delay

$cpu = shell_exec('top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk \'{print 100 - $1}\'');
陌上青苔 2024-10-18 23:38:45

在论坛上搜索并尝试了多种方法后,最准确的是:

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文