我运行一个php服务器,我想开发一个php脚本,当用户调用它时,它会从服务器回显网络使用情况,处理器负载,打开进程等。这可能吗?
答案 0 :(得分:2)
您可以使用可在其中运行sys命令的exec function,并将输出作为字符串返回(您可以在其中解析)。
一个例子:
<?php
$output = array();
exec("ps" , $output);
var_dump($output);
?>
输出:
array(7) {
[0]=>
string(28) " PID TTY TIME CMD"
[1]=>
string(30) "12986 ttys000 0:00.24 -bash"
[2]=>
string(28) "13033 ttys000 0:01.06 irb"
[3]=>
string(28) "13054 ttys000 0:01.38 irb"
[4]=>
string(40) "14975 ttys000 0:00.06 php -f test.php"
[5]=>
string(30) "14010 ttys005 0:00.11 -bash"
[6]=>
string(31) "14367 ttys005 0:00.07 python"
}
答案 1 :(得分:0)
这是我在周末从/proc/
文件系统中读取的一些东西 - 这可能比通过exec()
分叉的重量轻,尽管它也可能不太便携。
function getMem() {
$mem = '';
if($r = @file_get_contents('/proc/meminfo')) {
$dat = array();
foreach(explode("\n", $r) as $line) {
if(preg_match('/^([^:]+):[ ]+([0-9]+.*)$/', $line, $matches))
$dat[$matches[1]] = $matches[2];
}
$free = (int)((@$dat['MemFree'] + @$dat['Cached']) / 1024);
$mem = $free . 'MB / ' . (int)(@$dat['MemTotal'] / 1024) . 'MB';
}
return $mem;
}
function getLoad() {
$load = null;
if($r = @file_get_contents('/proc/loadavg')) {
$l = explode(' ', $r);
$load = implode(' ', array($l[0], $l[1], $l[2]));
}
return $load;
}