Home >Backend Development >PHP Tutorial >php 获得linux 机器的性能

php 获得linux 机器的性能

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-23 13:47:10861browse

<?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 个数据包

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn