PHP教學-同時抓取 cpu 記憶體 (linux)

這是另一種範例程式

<?php

function get_used_status()
{
$fp = popen('top -b -n 1 | grep -E "^(Cpu|Mem)"',"r");//获取某一时刻系统cpu和内存使用情况
$rs = "";
while(!feof($fp))
{
$rs .= fread($fp,1024);
}
pclose($fp);
$sys_info = explode("\n",$rs);
//print_r($sys_info);
$cpu_info = explode("  ",$sys_info[0]);
$mem_info = explode(" ",$sys_info[1]);
//print_r($cpu_info);
//print_r($mem_info);
$cpu_usage = substr($cpu_info[1],0,4);
//var_dump($cpu_usage);

$mem_total = trim($mem_info[3],'k');
$mem_used = trim($mem_info[6],'k');
$mem_usage = round(100*intval($mem_used)/intval($mem_total),1).'%';
//var_dump($mem_usage);

$fp = popen("df -lh","r");
$rs = fread($fp,1024);
pclose($fp);
$hd_info = explode("\n",$rs);
//print_r($hd_info);
$hd = explode(" ",$hd_info[1]);
//print_r($hd);
$hd_usage = $hd[21];
return array('cpu'=>$cpu_usage,'mem'=>$mem_usage,'hd'=>$hd_usage);
}

print_r(get_used_status());

?>


留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

使用windows CMD 時間自動校正