<?php class CServerInfo { static public function getCpuData($speed = 0.5) { if (false === ($prevVal = @file("/proc/stat"))) return false; $prevVal = implode($prevVal, PHP_EOL); $prevArr = explode(' ', trim($prevVal)); $prevTotal = $prevArr[2] + $prevArr[3] + $prevArr[4] + $prevArr[5]; $prevIdle = $prevArr[5]; usleep($speed * 1000000); $val = @file("/proc/stat"); $val = implode($val, PHP_EOL); $arr = explode(' ', trim($val)); $total = $arr[2] + $arr[3] + $arr[4] + $arr[5]; $idle = $arr[5]; $intervalTotal = intval($total - $prevTotal); return round(100 * (($intervalTotal - ($idle - $prevIdle)) / $intervalTotal),2); } /** * Cpu相關信息 * @return array|bool */ static public function getCpuInfoData() { 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); $res = array(); if (false !== is_array($model[1])) { $res['cpu']['num'] = sizeof($model[1]); if ($res['cpu']['num'] == 1) $x1 = ''; else $x1 = ' ×' . $res['cpu']['num']; $res['cpu']['mhz'] = $mhz[1][0]; $mhz[1][0] = ' <br /> 頻率:' . $mhz[1][0]; $res['cpu']['cache'] = $cache[1][0]; $cache[1][0] = ' <br /> 二級緩存:' . $cache[1][0]; $res['cpu']['bogomips'] = $bogomips[1][0]; $bogomips[1][0] = ' <br /> 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']); } return $res; } /** * 內存相關數據 * 單位爲B * @return array|bool */ static public function getMenData() { 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 = array(); $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']; //真實內存使用 $res['memRealFree'] = $res['memTotal'] - $res['memRealUsed']; //真實空閒 $res['memRealPercent'] = (floatval($res['memTotal']) != 0) ? round($res['memRealUsed'] / $res['memTotal'] * 100, 2) : 0; //真實內存使用率 $res['memCachedPercent'] = (floatval($res['memCached']) != 0) ? round($res['memCached'] / $res['memTotal'] * 100, 2) : 0; //Cached內存使用率 $res['swapTotal'] = round($buf[4][0] / 1024, 2); $res['swapFree'] = round($buf[5][0] / 1024, 2); $res['swapUsed'] = round($res['swapTotal'] - $res['swapFree'], 2); $res['swapPercent'] = (floatval($res['swapTotal']) != 0) ? round($res['swapUsed'] / $res['swapTotal'] * 100, 2) : 0; //判斷內存若是小於1G,就顯示M,不然顯示G單位 if ($res['memTotal'] < 1024) { $memTotal = $res['memTotal'] . " M"; $mt = $res['memTotal'] . " M"; $mu = $res['memUsed'] . " M"; $mf = $res['memFree'] . " M"; $mc = $res['memCached'] . " M"; //cache化內存 $mb = $res['memBuffers'] . " M"; //緩衝 $st = $res['swapTotal'] . " M"; $su = $res['swapUsed'] . " M"; $sf = $res['swapFree'] . " M"; $swapPercent = $res['swapPercent']; $memRealUsed = $res['memRealUsed'] . " M"; //真實內存使用 $memRealFree = $res['memRealFree'] . " M"; //真實內存空閒 $memRealPercent = $res['memRealPercent']; //真實內存使用比率 $memPercent = $res['memPercent']; //內存總使用率 $memCachedPercent = $res['memCachedPercent']; //cache內存使用率 } else { $memTotal = round($res['memTotal'] / 1024, 3) . " G"; $mt = round($res['memTotal'] / 1024, 3) . " G"; $mu = round($res['memUsed'] / 1024, 3) . " G"; $mf = round($res['memFree'] / 1024, 3) . " G"; $mc = round($res['memCached'] / 1024, 3) . " G"; $mb = round($res['memBuffers'] / 1024, 3) . " G"; $st = round($res['swapTotal'] / 1024, 3) . " G"; $su = round($res['swapUsed'] / 1024, 3) . " G"; $sf = round($res['swapFree'] / 1024, 3) . " G"; $swapPercent = $res['swapPercent']; $memRealUsed = round($res['memRealUsed'] / 1024, 3) . " G"; //真實內存使用 $memRealFree = round($res['memRealFree'] / 1024, 3) . " G"; //真實內存空閒 $memRealPercent = $res['memRealPercent']; //真實內存使用比率 $memPercent = $res['memPercent']; //內存總使用率 $memCachedPercent = $res['memCachedPercent']; //cache內存使用率 } $res['u_memTotal'] = $memTotal; $res['u_memTotal'] = $mt; $res['u_memUsed'] = $mu; $res['u_memFree'] = $mf; $res['u_memCached'] = $mc; //cache化內存 $res['u_memBuffers'] = $mb; //緩衝 $res['u_swapTotal'] = $st; $res['u_swapUsed'] = $su; $res['u_swapFree'] = $sf; $res['u_swapPercent'] = $swapPercent; $res['u_memRealUsed'] = $memRealUsed; //真實內存使用 $res['u_memRealFree'] = $memRealFree; //真實內存空閒 $res['u_memRealPercent'] = $memRealPercent; //真實內存使用比率 $res['u_memPercent'] = $memPercent; //內存總使用率 $res['u_memCachedPercent'] = $memCachedPercent; //cache內存使用率 return $res; } /** * 硬盤狀況 * @return array */ static public function getDiskData() { $iTotal = round(@disk_total_space(".") / (1024 * 1024 * 1024), 3); //總 $iUsableness = round(@disk_free_space(".") / (1024 * 1024 * 1024), 3); //可用 $iImpropriate = $iTotal - $iUsableness; //已用 $iPercent = (floatval($iTotal) != 0) ? round($iImpropriate / $iTotal * 100, 2) : 0; $sDesc = ''; if($iPercent>85){ $sDesc ="<span style='color: #f40'>磁盤空間很少了,建議近期清理一下日誌</span>"; } if($iPercent>95){ $sDesc ="<span style='color: #f40'>磁盤空間快滿了,可能會影響審計系統正常使用,建議當即清理日誌</span>"; } return array( 'iTotal' => $iTotal, 'iUsableness' => $iUsableness, 'iImpropriate' => $iImpropriate, 'iPercent' => $iPercent, 'sDesc' => $sDesc, ); } /** * 網接相關流量信息 */ static public function getInterfaceData() { //mii-tool $strs = @file("/proc/net/dev"); for ($i = 2; $i < count($strs); $i++) { preg_match_all("/([^\s]+):[\s]{0,}(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/", $strs[$i], $info); $NetOutSpeed[$i] = $info[10][0]; //入網實時 $NetInputSpeed[$i] = $info[2][0]; //出網實時 $NetInput[$i] = self::formatsize($info[2][0]); //入網流量 $NetOut[$i] = self::formatsize($info[10][0]); //出網流量 } } /** * 單位自動轉換 * @param $size * @return string */ static public function formatSize($size) { $danwei = array(' B ', ' K ', ' M ', ' G ', ' T '); $allsize = array(); $i = 0; for ($i = 0; $i < 5; $i++) { if (floor($size / pow(1024, $i)) == 0) { break; } } $allsize1 = array(); for ($l = $i - 1; $l >= 0; $l--) { $allsize1[$l] = floor($size / pow(1024, $l)); if (isset($allsize1[$l + 1])) { $allsize[$l] = $allsize1[$l] - $allsize1[$l + 1] * 1024; } } $len = count($allsize); $fsize = ""; for ($j = $len - 1; $j >= 0; $j--) { $fsize = $fsize . $allsize[$j] . $danwei[$j]; } return $fsize; } } ?>