php监控系统,php 系统监控 | 学步园
系統監控,在我的理解,就是cpu使用率,內存使用,硬盤使用,進程等等情況。
而這些東西,對于很多科班的童鞋來說,應該都很簡單。在命令行中輸入幾個命令就完事了。但是用php如何去做呢?這個問題,我想了很久,最后無意中在網上找到了幾個可以執行外部命令的函數,頓時讓我十分的驚喜。
1.exec().與 shell_exec() 相似,不同之處是它返回輸出的最后一行,并且可選地用命令的完整輸出和錯誤代碼填充數組.exec($command,$array)? $array 為結果
2.shell_exec???? 等同于``(單撇號)? 執行命令,返回結果為字符串。
3.passthru()???? 直接將結果輸出到瀏覽器上 ,不返回任何值。
4.system()???????? 直接將結果輸出到瀏覽器上,執行成功返回true,失敗返回false
知道了這幾個函數,那就很簡單了,基本的思路就是取到shell命令的結果,再用正則取到我們想要的值。
header("Content-type: text/html; charset=utf-8");
//CPU使用率
$str = shell_exec('more /proc/stat');
$pattern = "/(cpu[0-9]?)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)[\s]+([0-9]+)/";
preg_match_all($pattern, $str, $out);
echo "CPU使用率:".'
';
echo "共有".(count($out[1])-1)."個CPU,每個CPU利用率如下:
";
for($n=1;$n
{
echo $out[1][$n]."=".number_format((100*($out[1][$n]+$out[2][$n]+$out[3][$n])/($out[4][$n]+$out[5][$n]+$out[6][$n]+$out[7][$n])),4,'.','')."%
";
}
echo "***************************************************************".'
';
//內存使用情況
$str = shell_exec('more /proc/meminfo');
$pattern = "/(.+):\s*([0-9]+)/";
preg_match_all($pattern, $str, $out);
$mem=getRealSize($out[2][0]);
$used=getRealSize($out[2][1]);
$useable=getRealSize(($out[2][0]-$out[2][1]));
echo "內存使用情況:".'
';
echo "物理內存總量:".$mem."
";
echo "已使用的內存:".$useable."
";
echo "可使用內存:".$used."
";
echo "***************************************************************".'
';
//計算內存大小
function getRealSize($size)
{
$mb = 1024;???????? // MB
$gb = 1024 * $mb;?? // GB
$tb = 1024 * $gb;?? // TB
if($size < $mb){
return $size." KB";
}else if($size < $gb){
return round($size/$mb,2)." MB";
}else if($size < $tb){
return round($size/$gb,2)." GB";
}else{
return round($size/$tb,2)." TB";
}
}
//硬盤使用情況
$str = shell_exec('df -h');
#var_dump($str);
$arr = preg_split('/[,\s]+/', trim($str));
$arr = array_slice($arr,7);
#var_dump($arr);
$arr = array_chunk($arr,6);
#var_dump($arr);
echo "硬盤各個分區使用情況如下:";
#var_dump($arr);
?>
| 文件系統 | 容量 | 已用 | 可用 | 已用百分比 | 掛載點 |
foreach($arr as $v){
echo "
";for($i=0;$i
echo "
".$v[$i]."";}
echo "
";}
?>
echo "***************************************************************".'
';
//指定進程運行個數
echo "我們假設指定進程為httpd".'
';
$http = shell_exec("ps ax|grep 'httpd' -c");
#shell_exec("ps axu|grep 'httpd'|wc -l");
if($http>2){
echo "Httpd進程的運行個數為:".($http-2).'
';
//這里減去2的意思,是因為瀏覽器運行的時候,開啟了一個,shell命令執行的時候,開啟了一個。額,我是這么理解的,不知道對不對
}else{
echo "Httpd進程的運行個數為:0
";
}
echo "***************************************************************".'
';
?>
//Mysql連接數
$mysql_conn = shell_exec("/usr/local/lamp/mysql/bin/mysqladmin -uroot -phexinroot processlist");
$str= str_replace(array("-","+"),"",$mysql_conn);
$arr=explode('|',$str);
//格式如下:
/*array(19) { [0]=> string(1) " " [1]=> string(4) " Id " [2]=> string(6) " User " [3]=> string(11) " Host "
[4]=> string(4) " db " [5]=> string(9) " Command " [6]=> string(6) " Time " [7]=> string(7) " State "
[8]=> string(18) " Info " [9]=> string(2) " " [10]=> string(4) " 89 " [11]=> string(6) " root "
[12]=> string(11) " localhost " [13]=> string(4) " " [14]=> string(9) " Query " [15]=> string(6) " 0 "
[16]=> string(7) " " [17]=> string(18) " show processlist " [18]=> string(2) " " }
var_dump($arr);
*/
//經計算得到下式、
if(count($arr)>10){
$num=ceil((count($arr)-10)/9);
}else{
$num=0;
}
echo "當前Mysql的連接數為:".$num;
?>
總結
以上是生活随笔為你收集整理的php监控系统,php 系统监控 | 学步园的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 发现鼠标无法使用发现鼠标无法使用怎么办
- 下一篇: 以宏碁掠夺者 GM7000 2TB 为例