Linuxサーバーステータス情報の取得

function get_used_status(){
$fp = popen('top -b -n 2 | grep -E “^(Cpu|Mem|Tasks)"',"r");//CPUおよびメモリ使用量を取得
$rs = “";
while(!feof($fp)){
$rs .= fread($fp,1024);
}
pclose($fp);
$sys_info = explode(“\n",$rs);
$tast_info = explode(“,",$sys_info[3]);//プロセス
$cpu_info = explode(“,",$sys_info[4]); //CPU利用率
$mem_info = explode(“,",$sys_info[5]); //メモリ利用率
//実行中のプロセスの数
$tast_running = trim(trim($tast_info[1],’running’));
//CPU利用率
$cpu_usage = trim(trim($cpu_info[0],’Cpu(s): '),’%us’); //百分比

//メモリ利用率
$mem_total = trim(trim($mem_info[0],’Mem: '),’k total’);
$mem_used = trim($mem_info[1],’k used’);
$mem_usage = round(100*intval($mem_used)/intval($mem_total),2); //百分比

/*HDD使用率  begin*/
$fp = popen('df -lh | grep -E “^(/)"',"r");
$rs = fread($fp,1024);
pclose($fp);
$rs = preg_replace(“/\s{2,}/",’ ',$rs);
$hd = explode(" “,$rs);
$hd_avail = trim($hd[3],’G’); //HDD容量 G単位
$hd_usage = trim($hd[4],’%’);
//print_r($hd);
/*HDD使用率 end*/

//時間検出
$fp = popen(“date +\"%Y-%m-%d %H:%M\"","r");
$rs = fread($fp,1024);
pclose($fp);
$detection_time = trim($rs);

/*IPアドレスを取得 begin*/
/*
$fp = popen('ifconfig eth0 | grep -E “(inet addr)"',’r’);
$rs = fread($fp,1024);
pclose($fp);
$rs = preg_replace(“/\s{2,}/",’ ',trim($rs));
$rs = explode(" “,$rs);
$ip = trim($rs[1],’addr:’);
*/
/*IPドレスを取得 end*/
/*
$file_name = “/tmp/data.txt"; // 絶対パス: homedata.dat
$file_pointer = fopen($file_name, “a+");
fwrite($file_pointer,$ip);
fclose($file_pointer); // 終了
*/

return array('cpu_usage’=>$cpu_usage,’mem_usage’=>$mem_usage,’hd_avail’=>$hd_avail,’hd_usage’=>$hd_usage,’tast_running’=>$tast_running,’detection_time’=>$detection_time);
}

Source

Posted by arkgame