Re: [問題] 使用PHP自動導向
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
→
218.164.224.136 06/09, , 1F
218.164.224.136 06/09, 1F
推
140.121.80.100 06/09, , 2F
140.121.80.100 06/09, 2F
推
218.169.33.196 06/12, , 3F
218.169.33.196 06/12, 3F
Web_Design 近期熱門文章
PTT數位生活區 即時熱門文章