程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php檢測服務器系統信息[可以查CPU]

php檢測服務器系統信息[可以查CPU]

編輯:關於PHP編程

function sys_linux() {
 // CPU
 if (false === ($str = @file("/proc/cpuinfo"))) return false;
 $str = implode("", $str);
 @preg_match_all("/models+names{0,}:+s{0,}([ws)(@.]+)([rn]+)/s", $str, $model);
 @preg_match_all("/caches+sizes{0,}:+s{0,}([d.]+s{0,}[A-Z]+[rn]+)/", $str, $cache);
 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']['cache'][] = $cache[1][$i];
  }
  if (false !== is_array($res['cpu']['model'])) $res['cpu']['model'] = implode("<br />", $res['cpu']['model']);
  if (false !== is_array($res['cpu']['cache'])) $res['cpu']['cache'] = implode("<br />", $res['cpu']['cache']);
 }

 // 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("/MemTotals{0,}:+s{0,}([d.]+).+?MemFrees{0,}:+s{0,}([d.]+).+?SwapTotals{0,}:+s{0,}([d.]+).+?SwapFrees{0,}:+s{0,}([d.]+)/s", $str, $buf);

 $res['memTotal'] = round($buf[1][0]/1024, 2);
 $res['memFree'] = round($buf[2][0]/1024, 2);
 $res['memUsed'] = ($res['memTotal']-$res['memFree']);
 $res['memPercent'] = (floatval($res['memTotal'])!=0)?round($res['memUsed']/$res['memTotal']*100,2):0;

 $res['swapTotal'] = round($buf[3][0]/1024, 2);
 $res['swapFree'] = round($buf[4][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;}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved