-
-
/* - @系统参数探测 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(“/models names{0,}: s{0,}([ws) (.] )[rn] /”, $str, $model);
- //@preg_match_all(“/cpus MHzs{0,}: s{0,}([d.] )[rn] /” , $str, $mhz);
- @preg_match_all("/캐시 크기{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']);
- }
// 가동 시간
- if (false === ($str = @file(“/proc/uptime”))) return false;
- $str = 폭발(” “, implode(“”, $str));
- $ str = 트림($str[0]);
- $min = $str / 60;
- $hours = $min / 60;
- $days = Floor($hours / 24);
- $시간 = 층($시간 – ($일 * 24));
- $min = 층($분 – ($일 * 60 * 24) – ($시간 * 60));
- if ($ 일 !== 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.] ). }([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;
// AVG 로드
- if (false === ($str = @file(“/proc/loadavg”)) ) false 반환;
- $str =explod(" ", implode("", $str));
- $str = array_chunk($str, 3);
- $res['loadAvg'] = implode(” “, $str[0]);
return $res;
- }
- /*
- 系统参数探测 FreeBSD
- */
- 함수 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']);
//UPTIME
- if (false === ($buf = get_key(“kern.boottime”))) return false;
- $buf = 폭발(' ', $buf);
- $sys_ticks = time() – intval( $buf[3]);
- $min = $sys_ticks / 60;
- $hours = $min / 60;
- $days = Floor($hours / 24);
- $hours = Floor ($시간 – ($일 * 24));
- $min = Floor($min – ($일 * 60 * 24) – ($시간 * 60));
- if ($days !== 0) $res['uptime'] = $days.”하늘”;
- if ($hours !== 0) $res['uptime'] .= $hours.”小时”;
- $res ['uptime'] .= $min.”分钟”;
//MEMORY
- if (false === ($buf = get_key(“hw.physmem”) )) false 반환;
- $res['memTotal'] = round($buf/1024/1024, 2);
- $buf =explod(“n”, do_command(“vmstat”, “”)) ;
- $buf = 폭발(” “, Trim($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 = 폭발(“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”);
- }
/*
- 실행 파일 위치 결정 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."
- "
- ?>
코드 복사
|