Linux下的PHP实时流量统计程序
时间:2010-09-04 来源:王道-旭忠著
本程序分析/proc/net/ip_conntrack来进行分析实时在线及流量情况,精度不敢说,但是很实用。
做的有点太水了。不过能用就好
#!/usr/local/bin/php -q
<?php
$my_OverIp = array ();
$my_OverBytes = array ();
$k = 0;
$kk = 0;
$my_s=1;
$fd = fopen ( "/proc/net/ip_conntrack", "r" );
while ( ! feof ( $fd ) ) {
$my_FileLineCache = fgets ( $fd );
preg_match ( "/src=[0-9]+.[0-9]+.[0-9]+.[0-9]{1,3}/", $my_FileLineCache, $match_ip );
preg_match ( "/bytes=[0-9]+/", $my_FileLineCache, $match_bytes );
for($i = 0; $i < sizeof ( $my_OverIp ); $i ++) {
if ($my_OverIp [$i] == $match_ip [0]) {
$k = 1;
$kk = $i;
break;
}
}
$my_byte=preg_replace("/bytes=/","",$match_bytes [0]);
if ($k == 0) {
$my_OverIp [] = $match_ip [0];
$my_OverBytes [] = 0;
} else {
if ($my_s % 2 == 0)
{
$my_OverBytes [$kk] += $my_byte;
}
}
$k = 0;
$my_s++;
}
fclose ( $fd );
for($j = 0; $j < sizeof ( $my_OverIp ); $j ++) {
if ($j<>sizeof ( $my_OverIp )-1)
{
print $my_OverIp[$j];
print " bytes=$my_OverBytes[$j]\n";
}
}
?>