如何获取 CPU 和内存使用情况

发布于 2024-12-06 07:13:00 字数 88 浏览 0 评论 0原文

我想知道 php 中的内存和 CPU 使用情况,因为我使用 cronejobs 有时 CPU 过载,所以在这种情况下我不想启动更多进程,我只想跳过这个 cron。

I want to know the memory and CPU usage in php, because I'm using cronejobs sometimes the CPU is overloaded so in this case I don't wan to start more process, I just want to skip this cron.

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

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

发布评论

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

评论(4

染柒℉ 2024-12-13 07:13:00

我认为更好的方法是获取平均负载,因为它不仅取决于CPU,还取决于HDD速度。

这是一个文档: http://php.net/manual/en/function.sys-getloadavg.php

I think better way is get load avarage, because it depends not only on CPU, but on HDD speed also.

Here is a doc: http://php.net/manual/en/function.sys-getloadavg.php

仲春光 2024-12-13 07:13:00

看看这个库 http://phpsysinfo.sourceforge.net/

演示:http://phpsysinfo.sourceforge.net/phpsysinfo/index.php?disp=dynamic

编辑:这是摘自斯蒂芬的评论。我把它放在这里只是为了让人们有足够的机会阅读。

请注意:这个库不太灵活。我找不到办法
只返回 CPU 或 RAM,它返回所有内容,硬件列表,
网络统计、高清使用情况、操作系统信息,几乎所有您能想到的信息
的。这大约需要 4 秒才能完成,因此可能不是
如果您想要使用 ajax 不断更新值或
类似的东西。

Take a look at this library http://phpsysinfo.sourceforge.net/

Demo: http://phpsysinfo.sourceforge.net/phpsysinfo/index.php?disp=dynamic

Edit: This was taken from Stephen's comment. I put it here just so that it gets enough exposure for people to read.

Just a note: this library isn't too snappy. I couldn't find a way to
just return CPU or just ram, it returns EVERYTHING, hardware list,
network stats, hd usage, os info, pretty much everything you can think
of. This takes it about 4 seconds to complete so it may not be the
best option if you want a constantly updating value using ajax or
something of the sort.

橙味迷妹 2024-12-13 07:13:00

如何获取内存和 CPU 使用情况PHP

完美的简单例子。

--
内存百分比 % ($mem[2]/1000 表示已用内存,以 MB 为单位)

function get_server_memory_usage(){

$free = shell_exec('free');
$free = (string)trim($free);
$free_arr = explode("\n", $free);
$mem = explode(" ", $free_arr[1]);
$mem = array_filter($mem);
$mem = array_merge($mem);
$memory_usage = $mem[2]/$mem[1]*100;

return $memory_usage;
}

-- CPU 百分比

function get_server_cpu_usage(){

$load = sys_getloadavg();
return $load[0];

}

How to get Memory and CPU usage in PHP

Perfect simple examples.

--
Memory in procentage % ($mem[2]/1000 for used memory in MB)

function get_server_memory_usage(){

$free = shell_exec('free');
$free = (string)trim($free);
$free_arr = explode("\n", $free);
$mem = explode(" ", $free_arr[1]);
$mem = array_filter($mem);
$mem = array_merge($mem);
$memory_usage = $mem[2]/$mem[1]*100;

return $memory_usage;
}

-- CPU in procentage %

function get_server_cpu_usage(){

$load = sys_getloadavg();
return $load[0];

}
恍梦境° 2024-12-13 07:13:00

我最近在获取 CPU 负载方面遇到了一些问题,我想分享它。

这是我的解决方案,可以帮助我:

我的情况:

我使用 Zend Framework 1.2 来构建监控应用程序,我想获取 cpu 负载并将其显示在页面上。经过一些研究,我发现我可以使用 COM 对象并使用 wmi 查询 Win 操作系统,所以我将这些代码放入我的 init 函数中:

    /* Initialize action controller here */
    $this->wmi = new COM('winmgmts:{impersonationLevel=impersonate}//./root/cimv2');

    if (!is_object($this->wmi)) {
        throw new GetInfoException('This needs access to WMI. Please enable DCOM in php.ini and allow the current user to access the WMI DCOM object.');
    }
  • 你可以在任何你想要的地方使用它,因为 Zend 结构,我在 init 函数中使用了它。

我添加了一个操作和一个函数来使用该 wmi 并通过调用该函数随时获取 cpu 负载。

public function cpuloadAction()
{
    echo json_encode($this->getLoad());
    exit();
}

private function getLoad() {

    // Time?
    if (!empty($this->settings['timer']))
        $t = new LinfoTimerStart('Load Averages');

    $load = array();
    foreach ($this->wmi->ExecQuery("SELECT LoadPercentage FROM Win32_Processor") as $cpu) {
        $load[] = $cpu->LoadPercentage;
    }
    //return round(array_sum($load) / count($load), 2) . "%";
    return (int)round(array_sum($load) / count($load), 2);
}
  • 因为我将这些代码放入函数中的实时数据否则您可以将其编写在单个非面向对象的 PHP 文件中。

希望有帮助。

I recently have some issue to get CPU load and i feel like sharing it .

Here was my solution witch help me out :

My situation :

I've use Zend Framework 1.2 to build monitoring application and i want to get cpu load and show it on the page . after doing some research i found out i could use COM Object and query the Win OS with wmi so i put these code to my init function :

    /* Initialize action controller here */
    $this->wmi = new COM('winmgmts:{impersonationLevel=impersonate}//./root/cimv2');

    if (!is_object($this->wmi)) {
        throw new GetInfoException('This needs access to WMI. Please enable DCOM in php.ini and allow the current user to access the WMI DCOM object.');
    }
  • You could use it anywhere you want , i have use it in the init function beacuse of Zend structure .

And i added an Action and an function to use that wmi and get cpu load any time i want by calling that function .

public function cpuloadAction()
{
    echo json_encode($this->getLoad());
    exit();
}

private function getLoad() {

    // Time?
    if (!empty($this->settings['timer']))
        $t = new LinfoTimerStart('Load Averages');

    $load = array();
    foreach ($this->wmi->ExecQuery("SELECT LoadPercentage FROM Win32_Processor") as $cpu) {
        $load[] = $cpu->LoadPercentage;
    }
    //return round(array_sum($load) / count($load), 2) . "%";
    return (int)round(array_sum($load) / count($load), 2);
}
  • beacuse i what real time data i put these code in a function Otherwise you could write it in single non Object Oriented PHP file.

Hope it help .

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