作者xczh (我也要小老師 ><)
看板Web_Design
標題Re: [問題] 使用PHP自動導向
時間Thu Jun 9 02:38:12 2005
function loadavg() {
if (PHP_OS == Linux) {
if ($fp = fopen('/proc/loadavg', 'r')) {
$load = fread($fp, 14);
fclose($fp);
} else {
$load = 'N.A';
}
return $load;
}
這是參考phpsysinfo的
phpsysinfo有針對各種OS設計讀取負荷的函式
BSD是這樣的
function loadavg () {
$s = $this->grab_key('vm.loadavg');
$s = ereg_replace('{ ', '', $s);
$s = ereg_replace(' }', '', $s);
$results = explode(' ', $s); return $results;
}
function grab_key ($key) {
return execute_program('sysctl', "-n $key");
}
execute_program函式主要內容如下
function execute_program ($program, $args = '') {
if ($fp = popen("$program $args", 'r')) {
while (!feof($fp)) {
$buffer .= fgets($fp, 4096);
}
return trim($buffer);
}
}
我覺得可以直接簡化
$fp = fopen('vm.loadavg', 'r');
while (!feof($fp)) {
$s .= fgets($fp, 4096);
}
$s = ereg_replace('{ ', '', $s);
$s = ereg_replace(' }', '', $s);
$results = explode(' ', $s);
return $results;
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.164.224.136
1F:→ xczh:我對BSD不熟 @@..所以BSD跟freeBSD有沒有差不清楚唷218.164.224.136 06/09
2F:推 billyrt:套套看就知道啦:P 140.121.80.100 06/09
3F:推 eye12006:無差 218.169.33.196 06/12