<?php
/*==================================================================*/
/* 创建时间: 2010-01-28 */
/* 最后修改时间:2011-03-31 */
/* 网站:http://www.duyaofei.com/ */
/*==================================================================*/
error_reporting(0); //抑制所有错误信息
@header("content-Type: text/html; charset=utf-8"); //语言强制
ob_start();
$version = "v2011.03.31";
define('HTTP_HOST', preg_replace('~^www\.~i', '', $_SERVER['HTTP_HOST']));
$time_start = microtime_float();
function memory_usage() {
$memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2).'MB';
return $memory;
}
// 计时
function microtime_float() {
$mtime = microtime();
$mtime = explode(' ', $mtime);
return $mtime[1] + $mtime[0];
}
function valid_email($str) {
return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
}
//检测PHP设置参数
function show($varName) {
switch($result = get_cfg_var($varName)) {
case 0:
return '<font color="red">不支持</font>';
break;
case 1:
return '支持';
break;
default:
return $result;
break;
}
}
//保留服务器性能测试结果
$valInt = isset($_POST['pInt']) ? $_POST['pInt'] : "未测试";
$valFloat = isset($_POST['pFloat']) ? $_POST['pFloat'] : "未测试";
$valIo = isset($_POST['pIo']) ? $_POST['pIo'] : "未测试";
if ($_GET['act'] == "phpinfo") {
phpinfo();
exit();
} elseif($_POST['act'] == "整型测试") {
$valInt = test_int();
} elseif($_POST['act'] == "浮点测试") {
$valFloat = test_float();
} elseif($_POST['act'] == "IO测试") {
$valIo = test_io();
}
//MySQL检测
if ($_POST['act'] == 'MySQL检测') {
$host = isset($_POST['host']) ? trim($_POST['host']) : '';
$port = isset($_POST['port']) ? (int) $_POST['port'] : '';
$login = isset($_POST['login']) ? trim($_POST['login']) : '';
$password = isset($_POST['password']) ? trim($_POST['password']) : '';
$host = preg_match('~[^a-z0-9\-\.]+~i', $host) ? '' : $host;
$port = intval($port) ? intval($port) : '';
$login = preg_match('~[^a-z0-9\_\-]+~i', $login) ? '' : htmlspecialchars($login);
$password = is_string($password) ? htmlspecialchars($password) : '';
} elseif ($_POST['act'] == '函数检测') {
$funRe = "函数".$_POST['funName']."支持状况检测结果:".isfun($_POST['funName']);
} elseif ($_POST['act'] == '邮件检测') {
$mailRe = "邮件发送检测结果:发送";
$mailRe .= (false !== @mail($_POST["mailAdd"], "http://".$_SERVER['SERVER_NAME'].($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']), "This is a test mail!")) ? "完成":"失败";
}
// 检测函数支持
function isfun($funName = '') {
if (!$funName || trim($funName) == '' || preg_match('~[^a-z0-9\_]+~i', $funName, $tmp)) return '错误';
return (false !== function_exists($funName)) ? '支持' : '<font color="red">不支持</font>';
}
//整数运算能力测试
function test_int() {
$timeStart = gettimeofday();
for($i = 0; $i < 3000000; $i++) {
$t = 1+1;
}
$timeEnd = gettimeofday();
$time = ($timeEnd["usec"]-$timeStart["usec"])/1000000+$timeEnd["sec"]-$timeStart["sec"];
$time = round($time, 3)."秒";
return $time;
}
//浮点运算能力测试
function test_float() {
//得到圆周率值
$t = pi();
$timeStart = gettimeofday();
for($i = 0; $i < 3000000; $i++) {
//开平方
sqrt($t);
}
$timeEnd = gettimeofday();
$time = ($timeEnd["usec"]-$timeStart["usec"])/1000000+$timeEnd["sec"]-$timeStart["sec"];
$time = round($time, 3)."秒";
return $time;
}
//IO能力测试
function test_io() {
$fp = @fopen(PHPSELF, "r");
$timeStart = gettimeofday();
for($i = 0; $i < 10000; $i++) {
@fread($fp, 10240);
@rewind($fp);
}
$timeEnd = gettimeofday();
@fclose($fp);
$time = ($timeEnd["usec"]-$timeStart["usec"])/1000000+$timeEnd["sec"]-$timeStart["sec"];
$time = round($time, 3)."秒";
return($time);
}
// 根据不同系统取得CPU相关信息
switch(PHP_OS) {
case "Linux":
$sysReShow = (false !== ($sysInfo = sys_linux()))?"show":"none";
break;
case "FreeBSD":
$sysReShow = (false !== ($sysInfo = sys_freebsd()))?"show":"none";
break;
case "WINNT":
$sysReShow = (false !== ($sysInfo = sys_windows()))?"show":"none";
break;
default:
break;
}
//linux系统探测
function sys_linux()
{
// CPU
if (false === ($str = @file("/proc/cpuinfo"))) return false;
$str = implode("", $str);
@preg_match_all("/model\s+name\s{0,}\:+\s{0,}([\w\s\)\(\@.-]+)([\r\n]+)/s", $str, $model);
@preg_match_all("/cpu\s+MHz\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/", $str, $mhz);
@preg_match_all("/cache\s+size\s{0,}\:+\s{0,}([\d\.]+\s{0,}[A-Z]+[\r\n]+)/", $str, $cache);
@preg_match_all("/bogomips\s{0,}\:+\s{0,}([\d\.]+)[\r\n]+/", $str, $bogomips);
if (false !== is_array($model[1]))
{
$res['cpu']['num'] = sizeof($model[1]);
for($i = 0; $i < $res['cpu']['num']; $i++)
{
$res['cpu']['model'][] = $model[1][$i];
$res['cpu']['mhz'][] = $mhz[1][$i];
$res['cpu']['cache'][] = $cache[1][$i];
$res['cpu']['bogomips'][] = $bogomips[1][$i];
}
if (false !== is_array($res['cpu']['model'])) $res['cpu']['model'] = implode("<br />", $res['cpu']['model']);
if (false !== is_array($res['cpu']['mhz'])) $res['cpu']['mhz'] = implode("<br />", $res['cpu']['mhz']);
if (false !== is_array($res['cpu']['cache'])) $res['cpu']['cache'] = implode("<br />", $res['cpu']['cache']);
if (false !== is_array($res['cpu']['bogomips'])) $res['cpu']['bogomips'] = implode("<br />", $res['cpu']['bogomips']);
}
// NETWORK
// UPTIME
if (false === ($str = @file("/proc/uptime"))) 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));
if ($days !== 0) $res['uptime'] = $days."天";
if ($hours !== 0) $res['uptime'] .= $hours."小时";
$res['uptime'] .= $min."分钟";
// MEMORY
if (false === ($str = @file("/proc/meminfo"))) 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);
$res['memTotal'] = round($buf[1][0]/1024, 2);
$res['memFree'] = round($buf[2][0]/1024, 2);
$res['memCached'] = round($buf[3][0]/1024, 2);
$res['memUsed'] = ($res['memTotal']-$res['memFree']);
$res['memPercent'] = (floatval($res['memTotal'])!=0)?round($res['memUsed']/$res['memTotal']*100,2):0;
$res['memRealUsed'] = ($res['memTotal'] - $res['memFree'] - $res['memCached']);
$res['memRealPercent'] = (floatval($res['memTotal'])!=0)?round($res['memRealUsed']/$res['memTotal']*100,2):0;
$res['swapTotal'] = round($buf[4][0]/1024, 2);
$res['swapFree'] = round($buf[5][0]/1024, 2);
$res['swapUsed'] = ($res['swapTotal']-$res['swapFree']);
$res['swapPercent'] = (floatval($res['swapTotal'])!=0)?round($res['swapUsed']/$res['swapTotal']*100,2):0;
// LOAD AVG
if (false === ($str = @file("/proc/loadavg"))) return false;
$str = explode(" ", implode("", $str));
$str = array_chunk($str, 4);
$res['loadAvg'] = implode(" ", $str[0]);
return $res;
}
//FreeBSD系统探测
function sys_freebsd() {
//CPU
if (false === ($res['cpu']['num'] = get_key("hw.ncpu"))) return false;
$res['cpu']['model']