在php中获取cpu百分比的用法 [英] Get cpu percent usage in php

查看:124
本文介绍了在php中获取cpu百分比的用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在PHP中显示CPU使用率的百分比。如果获取cron中的值,在shell中输出文件>解析PHP或直接在PHP中获取值,这一点不重要。我尝试在互联网上找到许多解决方案,但没有任何有用的。加载平均值我无法显示0-100%的图形栏和函数,我发现百分比输出给我不好的值或仅第一个核心的值。获得每个核心的百分比使用率将是很好的。是否有解决方案?



编辑:



我做临时解决方案,它的效果很好,但不是最好的方法。



cron工作每一分钟运行php脚本哪个exec命令为grep cpu信息从顶并保存到文件,结束脚本等待3秒和循环20次(每3秒更新一次)
php脚本:

 <?php 
for($ i = 0; $ i <= 20; $ i ++){
// cpu load
exec(top -b -n 1 | grep'Cpu(s):'> ; /some/file.cpu);
// ram使用
exec(top -b -n 1 | grep'Mem:'> /some/file.ram);
// wait 3sec
sleep(3);
}
?>

现在从这个文件我可以解析信息。



新问题是如何使守护程序脚本每3秒运行一次这个命令。我认为php脚本和cron的解决方案只是暂时的解决方案,而不是最好的方法。守护进程将会更好。

解决方案

在论坛搜索和尝试许多方法之后,最准确的是:

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

现在统计资料是$ cpu ['user'],$ cpu ['nice'],$ cpu ['sys'],$ cpu ['idle']


I want to show percent CPU usage in PHP. Is not important if get values by cron in shell > output to file > parse in PHP or directly get value in php. I try many solutions found on internet but nothing was useful. With load average I can't display 0-100% graphic bar and functions I found for percentage output give me bad values or only value for first core. It would be nice to get number of percentage usage for every core. Is there solution for this?

EDIT:

I make temporary solution, it works good but it is not best way.

cron job every one minute run php script which exec command for grep cpu info from "top" and save it to file, on end script wait 3 seconds and loop 20-times (way to get update every 3 seconds) php script:

<?php
for($i=0; $i<=20; $i++) {
    //cpu load
    exec("top -b -n 1 | grep 'Cpu(s):' > /some/file.cpu");
    //ram usage
    exec("top -b -n 1 | grep 'Mem:' > /some/file.ram");
    //wait 3sec
    sleep(3);
}
?>

and now from this files I can parse informations.

New question is how to make daemon script to run this commands every 3 seconds. I think solution with php script and cron is only temporary solution and is not best way. daemon will be much better.

解决方案

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

now stats are in $cpu['user'], $cpu['nice'], $cpu['sys'], $cpu['idle']

这篇关于在php中获取cpu百分比的用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆