1. 程式人生 > >php 獲得linux 機器的性能

php 獲得linux 機器的性能

exec fun htm get IE lin function -c CP

<?php
$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 "共同擁有".count($out[1])."個CPU,每一個CPU利用率例如以下:\n"; for($n=0;$n<count($out[1]);$n++) { echo $out[1][$n]."=".(100*($out[1][$n]+$out[2][$n]+$out[3][$n])/($out[4][$n]+$out[5][$n]+$out[6][$n]+$out[7][$n]))."%\n"; } $str = shell_exec(‘more /proc/meminfo‘); $pattern = "/(.+):\s*([0-9]+)/"; preg_match_all($pattern, $str, $out); echo "物理內存總量:".$out[2][0]."\n"; echo "已使用的內存:".$out[2][1]."\n"; echo "-----------------------------------------\n"; echo "內存使用率:".(100*($out[2][0]-$out[2][1])/$out[2][0])."%\n"; $str = shell_exec(‘more /proc/net/dev‘); $pattern = "/(eth[0-9]+):\s*([0-9]+)\s+([0-9]+)\s+([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 "共同擁有".count($out[1])."個網絡接口,每一個網絡接口利用率例如以下:\n"; for($n=0;$n<count($out[1]);$n++) { echo $out[1][$n].":收到 ".$out[3][$n]." 個數據包,發送 ".$out[11][$n]." 個數據包\n"; } function get_cpu_usg() { $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); $total=0; for($n=0;$n<count($out[1]);$n++) { $usg=(100*($out[1][$n]+$out[2][$n]+$out[3][$n])/($out[4][$n]+$out[5][$n]+$out[6][$n]+$out[7][$n])); $total+=$usg; } return $total; } function get_mem_usg() { $str = shell_exec(‘more /proc/meminfo‘); $pattern = "/(.+):\s*([0-9]+)/"; preg_match_all($pattern, $str, $out); $memusg= (100*($out[2][0]-$out[2][1])/$out[2][0]) ; return $memusg; }

輸出形式例如以下

共同擁有11個CPU,每一個CPU利用率例如以下:
cpu=0.46448573378952%
cpu0=0.31392223749401%
cpu1=0.53497469529473%
cpu2=0.48665815912626%
cpu3=0.44933676279833%
cpu4=0.39351618273537%
cpu5=0.25942202031553%
cpu6=0.72395782907821%
cpu7=0.15144390649732%
cpu8=0.10644291691583%
cpu9=0.12204804936289%
物理內存總量:32776832
已使用的內存:240852
-----------------------------------------

內存使用率:99.265176085352%
共同擁有4個網絡接口,每一個網絡接口利用率例如以下:
eth0:收到 307077767 個數據包,發送 303024103 個數據包
eth1:收到 240252949 個數據包。發送 119448221 個數據包
eth2:收到 0 個數據包。發送 0 個數據包
eth3:收到 0 個數據包,發送 0 個數據包

php 獲得linux 機器的性能