php 获得linux 机器的性能

来源:互联网 发布:将centos系统备份 编辑:程序博客网 时间:2024/05/19 06:36
<?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 个数据包

0 0
原创粉丝点击