作者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