我正在尝试做一个循环,根据平均负载和 CPU/内存等显示一些条件。但是,它似乎只为整个循环提取一次使用信息。我希望它在每个循环中更新它:这就是我得到的,非常感谢任何形式的帮助!
<?php
exec("cat /proc/cpuinfo | grep processor | wc -l",$processors);
$totalc=$processors[0];
function sys_getloadavg_hack()
{
$str = substr(strrchr(shell_exec("uptime"),":"),1);
$avs = array_map("trim",explode(",",$str));
return $avs;
}
$load = sys_getloadavg_hack();
function get_memory() {
foreach(file('/proc/meminfo') as $ri)
$m[strtok($ri, ':')] = strtok('');
return 100 - round(($m['MemFree'] + $m['Buffers'] + $m['Cached']) / $m['MemTotal'] * 100);
}
function print_exec(){
$out_arr=array();
$last_line=exec("mpstat",$out_arr);
return $last_line;
}
for ($i=1; $i<=50; $i++)
{
$memory = get_memory();
$cpu = substr(print_exec(),-5);
if($memory>"10" && $cpu>"10" && $load[0]<$totalc) {
echo "Everything is good!";
} else {
}
echo "Memory: $memory || CPU: $cpu || Load AVG: $load[0] || #CPU's: $totalc<br> || #Runtime: $i\n\n";
usleep(800000);
}
?>