本文主要和大家分享PHP服务器探针的实现代码,文章有点长,希望能帮助到大家。<?phperror_reporting (0); //抑制所有错误信息@header("content-Type: text/html; charset=utf-8"); //语言强制ob_start();
date_default_timezone_set('Asia/Shanghai');//此句用于消除时间差$time_start = microtime_float();/**
*
*/class ServerInfo{
//服务器参数
public $S = array( 'YourIP', //你的IP
'DomainIP', //服务器域名和IP及进程用户名
'Flag', //服务器标识
'OS', //服务器操作系统具体
'Language', //服务器语言
'Name', //服务器主机名
'Email', //服务器管理员邮箱
'WebEngine', //服务器WEB服务引擎
'WebPort', //web服务端口
'WebPath', //web路径
'ProbePath', //本脚本所在路径
'sTime' //服务器时间
); public $sysInfo; //系统信息,windows和linux
public $CPU_Use; public $hd = array( 't', //硬盘总量
'f', //可用
'u', //已用
'PCT', //使用率
); public $NetWork = array( 'NetWorkName', //网卡名称
'NetOut', //出网总量
'NetInput', //入网总量
'OutSpeed', //出网速度
'InputSpeed' //入网速度
); //网卡流量
function __construct(){
$this->S['YourIP'] = @$_SERVER['REMOTE_ADDR']; $domain = $this->OS()?$_SERVER['SERVER_ADDR']:@gethostbyname($_SERVER['SERVER_NAME']); $this->S['DomainIP'] = @get_current_user().' - '.$_SERVER['SERVER_NAME'].'('.$domain.')'; $this->S['Flag'] = empty($this->sysInfo['win_n'])?@php_uname():$this->sysInfo['win_n']; $os = explode(" ", php_uname()); $oskernel = $this->OS()?$os[2]:$os[1]; $this->S['OS'] = $os[0].'内核版本:'.$oskernel; $this->S['Language'] = getenv("HTTP_ACCEPT_LANGUAGE"); $this->S['Name'] = $this->OS()?$os[1]:$os[2]; $this->S['Email'] = $_SERVER['SERVER_ADMIN']; $this->S['WebEngine'] = $_SERVER['SERVER_SOFTWARE']; $this->S['WebPort'] = $_SERVER['SERVER_PORT']; $this->S['WebPath'] = $_SERVER['DOCUMENT_ROOT']?str_replace('\\','/',$_SERVER['DOCUMENT_ROOT']):str_replace('\\','/',dirname(__FILE__)); $this->S['ProbePath'] = str_replace('\\','/',__FILE__)?str_replace('\\','/',__FILE__):$_SERVER['SCRIPT_FILENAME']; $this->S['sTime'] = date('Y-m-d H:i:s'); $this->sysInfo = $this->GetsysInfo(); //var_dump($this->sysInfo);
$CPU1 = $this->GetCPUUse();
sleep(1); $CPU2 = $this->GetCPUUse(); $data = $this->GetCPUPercent($CPU1, $CPU2); $this->CPU_Use =$data['cpu0']['user']."%us, ".$data['cpu0']['sys']."%sy, ".$data['cpu0']['nice']."%ni, ".$data['cpu0']['idle']."%id, ".$data['cpu0']['iowait']."%wa, ".$data['cpu0']['irq']."%irq, ".$data['cpu0']['softirq']."%softirq"; if(!$this->OS()) $this->CPU_Use = '目前只支持Linux系统'; $this->hd = $this->GetDisk(); $this->NetWork = $this->GetNetWork();
} public function OS(){
return DIRECTORY_SEPARATOR=='/'?true:false;
} public function GetsysInfo(){
switch (PHP_OS) { case 'Linux': $sysInfo = $this->sys_linux(); break; case 'FreeBSD': $sysInfo = $this->sys_freebsd(); break; default: # code...
break;
} return $sysInfo;
} public function sys_linux(){ //linux系统探测
$str = @file("/proc/cpuinfo"); //获取CPU信息
if(!$str) return false; $str = implode("", $str);
@preg_match_all("/model\s+name\s{0,}\:+\s{0,}([\w\s\)\@.-]+)([\r\n]+)/s", $str, $model); //CPU 名称
@preg_match_all("/cpu\s+MHz\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/", $str, $mhz); //CPU频率
@preg_match_all("/cache\s+size\s{0,}\:+\s{0,}([\d\.]+\s{0,}[A-Z]+[\r\n]+)/", $str, $cache); //CPU缓存
@preg_match_all("/bogomips\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/", $str, $bogomips); //
if(is_array($model[1])){ $cpunum = count($model[1]); $x1 = $cpunum>1?' ×'.$cpunum:''; $mhz[1][0] = ' | 频率:'.$mhz[1][0]; $cache[1][0] = ' | 二级缓存:'.$cache[1][0]; $bogomips[1][0] = ' | Bogomips:'.$bogomips[1][0]; $res['cpu']['num'] = $cpunum; $res['cpu']['model'][] = $model[1][0].$mhz[1][0].$cache[1][0].$bogomips[1][0].$x1; if(is_array($res['cpu']['model'])) $res['cpu']['model'] = implode("
", $res['cpu']['model']); if(is_array($res['cpu']['mhz'])) $res['cpu']['mhz'] = implode("
", $res['cpu']['mhz']); if(is_array($res['cpu']['cache'])) $res['cpu']['cache'] = implode("
", $res['cpu']['cache']); if(is_array($res['cpu']['bogomips'])) $res['cpu']['bogomips'] = implode("
", $res['cpu']['bogomips']);
} //服务器运行时间
$str = @file("/proc/uptime"); if(!$str) return false; $str = explode(" ", implode("", $str)); $str = trim($str[0]); $min = $str/60; $hours = $min/60; $days = floor($hours/24); $hours = floor($hours-($days*24)); $min = floor($min-($days*60*24)-($hours*60)); $res['uptime'] = $days."天".$hours."小时".$min."分钟"; //内存
$str = @file("/proc/meminfo"); if(!$str) return false; $str = implode("", $str);
preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buf);
preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers); $resmem['memTotal'] = round($buf[1][0]/1024, 2); $resmem['memFree'] = round($buf[2][0]/1024, 2); $resmem['memBuffers'] = round($buffers[1][0]/1024, 2); $resmem['memCached'] = round($buf[3][0]/1024, 2); $resmem['memUsed'] = $resmem['memTotal']-$resmem['memFree']; $resmem['memPercent'] = (floatval($resmem['memTotal'])!=0)?round($resmem['memUsed']/$resmem['memTotal']*100,2):0; $resmem['memRealUsed'] = $resmem['memTotal'] - $resmem['memFree'] - $resmem['memCached'] - $resmem['memBuffers']; //真实内存使用
$resmem['memRealFree'] = $resmem['memTotal'] - $resmem['memRealUsed']; //真实空闲
$resmem['memRealPercent'] = (floatval($resmem['memTotal'])!=0)?round($resmem['memRealUsed']/$resmem['memTotal']*100,2):0; //真实内存使用率
$resmem['memCachedPercent'] = (floatval($resmem['memCached'])!=0)?round($resmem['memCached']/$resmem['memTotal']*100,2):0; //Cached内存使用率
$resmem['swapTotal'] = round($buf[4][0]/1024, 2); $resmem['swapFree'] = round($buf[5][0]/1024, 2); $resmem['swapUsed'] = round($resmem['swapTotal']-$resmem['swapFree'], 2); $resmem['swapPercent'] = (floatval($resmem['swapTotal'])!=0)?round($resmem['swapUsed']/$resmem['swapTotal']*100,2):0; $resmem = $this->formatmem($resmem); //格式化内存显示单位
$res = array_merge($res,$resmem); // LOAD AVG 系统负载
$str = @file("/proc/loadavg"); if (!$str) return false; $str = explode(" ", implode("", $str)); $str = array_chunk($str, 4); $res['loadAvg'] = implode(" ", $str[0]); return $res;
} public function sys_freebsd(){ //freeBSD系统探测
$res['cpu']['num'] = do_command('sysctl','hw.ncpu'); //CPU
$res['cpu']['model'] = do_command('sysctl','hw.model'); $res['loadAvg'] = do_command('sysctl','vm.loadavg'); //Load AVG 系统负载
//uptime
$buf = do_command('sysctl','kern.boottime'); $buf = explode(' ', $buf); $sys_ticks = time()-intval($buf[3]); $min = $sys_ticks/60; $hours = $min/60; $days = floor($hours/24); $hours = floor($hours-($days*24)); $min = floor($min-($days*60*24)-($hours*60)); $res['uptime'] = $days.'天'.$hours.'小时'.$min.'分钟'; //内存
$buf = do_command('sysctl','hw.physmem'); $resmem['memTotal'] = round($buf/1024/1024, 2); $str = do_command('sysctl','vm.vmtotal');
preg_match_all("/\nVirtual Memory[\:\s]*\(Total[\:\s]*([\d]+)K[\,\s]*Active[\:\s]*([\d]+)K\@.-]+)([\r\n]+)/s", $str, $model); //CPU 名称 @preg_match_all("/cpu\s+MHz\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/", $str, $mhz); //CPU频率 @preg_match_all("/cache\s+size\s{0,}\:+\s{0,}([\d\.]+\s{0,}[A-Z]+[\r\n]+)/", $str, $cache); //CPU缓存 @preg_match_all("/bogomips\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/", $str, $bogomips); // if(is_array($model[1])){ $cpunum = count($model[1]); $x1 = $cpunum>1?' ×'.$cpunum:''; $mhz[1][0] = ' | 频率:'.$mhz[1][0]; $cache[1][0] = ' | 二级缓存:'.$cache[1][0]; $bogomips[1][0] = ' | Bogomips:'.$bogomips[1][0]; $res['cpu']['num'] = $cpunum; $res['cpu']['model'][] = $model[1][0].$mhz[1][0].$cache[1][0].$bogomips[1][0].$x1; if(is_array($res['cpu']['model'])) $res['cpu']['model'] = implode("
", $res['cpu']['model']); if(is_array($res['cpu']['mhz'])) $res['cpu']['mhz'] = implode("
", $res['cpu']['mhz']); if(is_array($res['cpu']['cache'])) $res['cpu']['cache'] = implode("
", $res['cpu']['cache']); if(is_array($res['cpu']['bogomips'])) $res['cpu']['bogomips'] = implode("
", $res['cpu']['bogomips']); } //服务器运行时间 $str = @file("/proc/uptime"); if(!$str) return false; $str = explode(" ", implode("", $str)); $str = trim($str[0]); $min = $str/60; $hours = $min/60; $days = floor($hours/24); $hours = floor($hours-($days*24)); $min = floor($min-($days*60*24)-($hours*60)); $res['uptime'] = $days."天".$hours."小时".$min."分钟"; //内存 $str = @file("/proc/meminfo"); if(!$str) return false; $str = implode("", $str); preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?Cached\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buf); preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers); $resmem['memTotal'] = round($buf[1][0]/1024, 2); $resmem['memFree'] = round($buf[2][0]/1024, 2); $resmem['memBuffers'] = round($buffers[1][0]/1024, 2); $resmem['memCached'] = round($buf[3][0]/1024, 2); $resmem['memUsed'] = $resmem['memTotal']-$resmem['memFree']; $resmem['memPercent'] = (floatval($resmem['memTotal'])!=0)?round($resmem['memUsed']/$resmem['memTotal']*100,2):0; $resmem['memRealUsed'] = $resmem['memTotal'] - $resmem['memFree'] - $resmem['memCached'] - $resmem['memBuffers']; //真实内存使用 $resmem['memRealFree'] = $resmem['memTotal'] - $resmem['memRealUsed']; //真实空闲 $resmem['memRealPercent'] = (floatval($resmem['memTotal'])!=0)?round($resmem['memRealUsed']/$resmem['memTotal']*100,2):0; //真实内存使用率 $resmem['memCachedPercent'] = (floatval($resmem['memCached'])!=0)?round($resmem['memCached']/$resmem['memTotal']*100,2):0; //Cached内存使用率 $resmem['swapTotal'] = round($buf[4][0]/1024, 2); $resmem['swapFree'] = round($buf[5][0]/1024, 2); $resmem['swapUsed'] = round($resmem['swapTotal']-$resmem['swapFree'], 2); $resmem['swapPercent'] = (floatval($resmem['swapTotal'])!=0)?round($resmem['swapUsed']/$resmem['swapTotal']*100,2):0; $resmem = $this->formatmem($resmem); //格式化内存显示单位 $res = array_merge($res,$resmem); // LOAD AVG 系统负载 $str = @file("/proc/loadavg"); if (!$str) return false; $str = explode(" ", implode("", $str)); $str = array_chunk($str, 4); $res['loadAvg'] = implode(" ", $str[0]); return $res; } public function sys_freebsd(){ //freeBSD系统探测 $res['cpu']['num'] = do_command('sysctl','hw.ncpu'); //CPU $res['cpu']['model'] = do_command('sysctl','hw.model'); $res['loadAvg'] = do_command('sysctl','vm.loadavg'); //Load AVG 系统负载 //uptime $buf = do_command('sysctl','kern.boottime'); $buf = explode(' ', $buf); $sys_ticks = time()-intval($buf[3]); $min = $sys_ticks/60; $hours = $min/60; $days = floor($hours/24); $hours = floor($hours-($days*24)); $min = floor($min-($days*60*24)-($hours*60)); $res['uptime'] = $days.'天'.$hours.'小时'.$min.'分钟'; //内存 $buf = do_command('sysctl','hw.physmem'); $resmem['memTotal'] = round($buf/1024/1024, 2); $str = do_command('sysctl','vm.vmtotal'); preg_match_all("/\nVirtual Memory[\:\s]*\(Total[\:\s]*([\d]+)K[\,\s]*Active[\:\s]*([\d]+)K\n/i", $str, $buff, PREG_SET_ORDER);
preg_match_all("/\nReal Memory[\:\s]*Total[\s]∗([\d]+)K[\s]∗Active[\s]∗([\d]+)KTotal[\s]∗([\d]+)K[\s]∗Active[\s]∗([\d]+)K\n/i", $str, $buf, PREG_SET_ORDER); $resmem['memRealUsed'] = round($buf[0][2]/1024, 2); $resmem['memCached'] = round($buff[0][2]/1024, 2); $resmem['memUsed'] = round($buf[0][1]/1024, 2)+$resmem['memCached']; $resmem['memFree'] = $resmem['memTotal']-$resmem['memUsed']; $resmem['memPercent'] = (floatval($resmem['memTotal'])!=0)?round($resmem['memUsed']/$resmem['memTotal']*100,2):0; $resmem['memRealPercent'] = (floatval($resmem['memTotal'])!=0)?round($resmem['memRealUsed']/$resmem['memTotal']*100,2):0; $resmem = $this->formatmem($resmem); $res = array_merge($res,$resmem); return $res;
} public function do_command($cName, $args){ //执行系统命令FreeBSD
$cName = empty($cName)?'sysctl':timr($cName); if(empty($args)) return false; $args = '-n '.$args; $buffers = ''; $command = find_command($cName); if(!$command) return false; if($fp = @popen("$command $args", 'r')){ while (!@feof($fp)) { $buffers .= @fgets($fp, 4096);
}
pclos