查看CUP,RAM使用情况

 <?php
/**
 *监控CUP/RAM使用情况
 */
 
//查看CUP使用情况
$mode   = "/(cpu)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)/";
$string = shell_exec("more /proc/stat");
preg_match_all($mode,$string,$arr);
$total1 = $arr[2][0]+$arr[3][0]+$arr[4][0]+$arr[5][0]+$arr[6][0]+$arr[7][0]+$arr[8][0]+$arr[9][0];
$time1  = $arr[2][0]+$arr[3][0]+$arr[4][0]+$arr[6][0]+$arr[7][0]+$arr[8][0]+$arr[9][0];

sleep(1);
$string = shell_exec("more /proc/stat");
preg_match_all($mode,$string,$arr);
$total2 = $arr[2][0]+$arr[3][0]+$arr[4][0]+$arr[5][0]+$arr[6][0]+$arr[7][0]+$arr[8][0]+$arr[9][0];
$time2  = $arr[2][0]+$arr[3][0]+$arr[4][0]+$arr[6][0]+$arr[7][0]+$arr[8][0]+$arr[9][0];
$time  = $time2-$time1;
$total = $total2-$total1;
//echo "CPU amount is: ".$num;
$percent = round($time/$total,2);
$percent = $percent*100;
//echo "CPU Usage is: ".$percent."%";

if($percent > 10){
    echo "CPU使用已超过10%。";
}

//查看RAM使用情况
$str=shell_exec("more /proc/meminfo");
$mode="/(.+):\s*([0-9]+)/";
preg_match_all($mode,$str,$arr);
$pr=round($arr[2][1]/$arr[2][0],2);
$pr=1-$pr;
$pr=$pr*100;
//echo "RAM Usage is: ".$pr."%";

if($pr > 85){
    echo "RAM使用已超过85%。";
}

您可以选择一种方式赞助本站

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: