-
-
/* - @系统パラメータ探索LINUX
- @Edit bbs.it-home.org
- @date 2013/5/8
- */
- function sys_linux()
- {
- // CPU
- if (false === ($str = @file(“/proc/cpuinfo”))) return false;
- $str = implode(“”, $str);
- @preg_match_all(“/モデル+名前{0,}:+s{0,}([ws)(.]+)[rn]+/”, $str, $model);
- //@preg_match_all(”/cpus+MHzs{0 ,}:+s{0,}([d.]+)[rn]+/”, $str, $mhz);
- @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']['detail'][] = “类型:”.$model[1][$i].” 存在:”.$cache[1][$i];
- }
- if (false !== is_array($res['cpu']['detail'])) $res['cpu']['detail' ] = implode(”
- ”, $res['cpu']['detail']);
- }
// UPTIME
- if (false === ($str = @file) (“/proc/uptime”))) return false;
- $str =explode(" ", implode("", $str));
- $str = トリム($str[0]);
- $min = $ str / 60;
- $hours = $min / 60;
- $days = フロア($時間 / 24);
- $時間 = フロア($時間 – ($days * 24));
- $min = フロア($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'])*90,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'])*90,2):0;
// LOAD AVG
- if (false === ($str = @file(“/proc/loadavg”) )) false を返します;
- $str =explode(" ", implode("", $str));
- $str = array_chunk($str, 3);
- $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']['detail'] = get_key(“hw.model” );
//LOAD AVG
- if (false === ($res['loadAvg'] = get_key(“vm.loadavg”))) return false;
- $res[' loadAvg'] = str_replace("{", "", $res['loadAvg']);
- $res['loadAvg'] = str_replace("}", "", $res['loadAvg']);< ;/p>
//UPTIME
- if (false === ($buf = get_key(“kern.boottime”))) return false;
- $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));
- if ($days !== 0) $ res['uptime'] = $days.”天”;
- if ($hours !== 0) $res['uptime'] .= $hours.”小時間”;
- $res['uptime'] .= $min.”分钟”;
//MEMORY
- if (false === ($buf = get_key(“hw.physmem”))) return false;
- $res['memTotal '] =round($buf/1024/1024, 2);
- $buf =explode(“n”, do_command(“vmstat”, “”));
- $buf =explode(” “, トリム($buf[ 2]));
$res['memFree'] =round($buf[5]/1024, 2);
- $res['memused'] = ($res[' memTotal']-$res['memFree']);
- $res['memPercent'] = (floatval($res['memTotal'])!=0)?round(($res['memused']/$ res['memTotal'])*90,2):0;
$buf =explode(“n”, do_command(“swapinfo”, “-k”));
- $buf = $buf[1];
- preg_match_all(“/([0-9]+)s+([0-9]+)s+([0-9]+)/”, $buf, $bufArr);
- $ res['swapTotal'] =round($bufArr[1][0]/1024, 2);
- $res['swapused'] =round($bufArr[2][0]/1024, 2);
- $ res['swapFree'] =round($bufArr[3][0]/1024, 2);
- $res['swapPercent'] = (floatval($res['swapTotal'])!=0)?round( ($res['swapused']/$res['swapTotal'])*90,2):0;
return $res;
- }
/*
- パラメータ値を取得 FreeBSD
- */
- function get_key($keyName)
- {
- return do_command('sysctl', “-n $keyName”);
- } p>
/*
- 実行可能ファイルの場所を決定する FreeBSD
- */
- function find_command($commandName)
- {
- $path = array('/bin', '/sbin', '/usr/ bin', '/usr/sbin', '/usr/local/bin', '/usr/local/sbin');
- foreach($path as $p)
- {
- if (@is_executable("$p/ $commandName" )) return “$p/$commandName”;
- }
- return false;
- }
/*
- システムコマンド FreeBSD を実行
- */
- function do_command($commandName, $ args)
- {
- $buffer = "";
- if (false === ($command = find_command($commandName))) return false;
- if ($fp = @popen("$command $args", 'r '))
- {
- while (!@feof($fp))
- {
- $buffer .= @fgets($fp, 4096);
- }
- return trim($buffer);
- }
- return false;
- }
- ?> ;
-
コードをコピー
手順:
メモリは、memory_get_usage() 関数で処理できます。
Windows では、cmd コマンドの実行結果のみを読み取ることができます。
例えば:
-
- $a = system('mem');
- echo $a."
- ";
- //または
- $a = system('mem');
- echo $a. "
- ";
- ?>
コードをコピー
|