$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);

}

function GetCoreInformation() {$data = file('/proc/stat');$cores = array();foreach( $data as $line ) {if( preg_match('/^cpu[0-9]/', $line) ){$info = explode(' ', $line);$cores[]=array('user'=>$info[1],'nice'=>$info[2],'sys' => $info[3],'idle'=>$info[4],'iowait'=>$info[5],'irq' => $info[6],'softirq' => $info[7]);}}return $cores;}
function GetCpuPercentages($stat1, $stat2) {if(count($stat1)!==count($stat2)){return;}$cpus=array();for( $i = 0, $l = count($stat1); $i < $l; $i++) { $dif = array(); $dif['user'] = $stat2[$i]['user'] - $stat1[$i]['user'];$dif['nice'] = $stat2[$i]['nice'] - $stat1[$i]['nice']; $dif['sys'] = $stat2[$i]['sys'] - $stat1[$i]['sys'];$dif['idle'] = $stat2[$i]['idle'] - $stat1[$i]['idle'];$dif['iowait'] = $stat2[$i]['iowait'] - $stat1[$i]['iowait'];$dif['irq'] = $stat2[$i]['irq'] - $stat1[$i]['irq'];$dif['softirq'] = $stat2[$i]['softirq'] - $stat1[$i]['softirq'];$total = array_sum($dif);$cpu = array();foreach($dif as $x=>$y) $cpu[$x] = round($y / $total * 100, 2);$cpus['cpu' . $i] = $cpu;}return $cpus;}
$stat1 = GetCoreInformation();sleep(1);$stat2 = GetCoreInformation();$data = GetCpuPercentages($stat1, $stat2);
$cpu_show = $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";
function makeImageUrl($title, $data) {$api='http://api.yahei.net/tz/cpu_show.php?id=';$url.=$data['user'].',';$url.=$data['nice'].',';$url.=$data['sys'].',';$url.=$data['idle'].',';$url.=$data['iowait'];$url.='&chdl=User|Nice|Sys|Idle|Iowait&chdlp=b&chl=';$url.=$data['user'].'%25|';$url.=$data['nice'].'%25|';$url.=$data['sys'].'%25|';$url.=$data['idle'].'%25|';$url.=$data['iowait'].'%25';$url.='&chtt=Core+'.$title;return $api.base64_encode($url);}
if($_GET['act'] == "cpu_percentage"){echo "<center><b><font face='Microsoft YaHei' color='#666666' size='3'>图片加载慢,请耐心等待!</font></b><br /><br />";foreach( $data as $k => $v ) {echo '<img src="' . makeImageUrl( $k, $v ) . '" style="width:360px;height:240px;border: #CCCCCC 1px solid;background: #FFFFFF;margin:5px;padding:5px;" />';}echo "</center>";exit();}



// 根据不同系统取得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].'&nbsp;('.$mhz[1][$i].')';

$res['cpu']['mhz'][] = $mhz[1][$i];

$res['cpu']['cache'][] = $cache[1][$i];

$res['cpu']['bogomips'][] = $bogomips[1][$i];

}*/
if($res['cpu']['num']==1)
$x1 = '';
else
$x1 = ' ×'.$res['cpu']['num'];
$mhz[1][0] = ' | 频率:'.$mhz[1][0];
$cache[1][0] = ' | 二级缓存:'.$cache[1][0];
$bogomips[1][0] = ' | Bogomips:'.$bogomips[1][0];
$res['cpu']['model'][] = $model[1][0].$mhz[1][0].$cache[1][0].$bogomips[1][0].$x1;

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);
preg_match_all("/Buffers\s{0,}\:+\s{0,}([\d\.]+)/s", $str, $buffers);


$res['memTotal'] = round($buf[1][0]/1024, 2);

$res['memFree'] = round($buf[2][0]/1024, 2);

$res['memBuffers'] = round($buffers[1][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['memBuffers']; //真实内存使用

Prev | Next
Pg.: 1 2 3 4 5 6 7 ... 10


Back to home | File page

Subscribe | Register | Login | N