關於流量統計 - BBS
By Jacky
at 2007-04-25T20:16
at 2007-04-25T20:16
Table of Contents
ports 裡有沒有現成的套件可以統計 每小時/每日的流量(size) 單位bits ?
而不是 snmp+mrtg 統計的流量(rate) 單位bps
就像這樣 http://www.dumeter.com/images/screenshots/dumeter/totals.gif
以下是我用笨方法,去讀 mrtg 的 log file ,統計出每小時流量
hour+1
Σ mrtg_rate *Δt
t=hour
請大家參考看看,如果有更好的方法或軟體,請告知!
# /path/to/script.php localhost_1 2007 4 25 12
Output:
2007-04-25 12:00:00 ==> 2007-04-25 13:00:00
Input: 242455800 bits (28.902983665466 MB)
Output: 252297600 bits (30.076217651367 MB)
Total: 494753400 bits (58.979201316833 MB)
#!/usr/local/bin/php
<?php
$logfile = '/home/www/mrtg/'.$_SERVER['argv'][1].'.log';
if ($_SERVER['argc'] == 6)
{
$y = $_SERVER['argv'][2];
$m = $_SERVER['argv'][3];
$d = $_SERVER['argv'][4];
$H = $_SERVER['argv'][5];
}
else
{
$y=date('Y');
$m=date('m');
$d=date('d');
$H=date('H');
}
$hour_time = mktime($H,0,0,$m,$d,$y);
$total_in = 0;
$total_out = 0;
$lines = file($logfile);
$first_line = true;
$pre_time = 0;
$pre_in = 0;
$pre_out = 0;
foreach ($lines as $line)
{
if ($first_line)
{
$first_line = false;
continue;
}
$arr_line = explode (' ', $line);
if (count($arr_line) ==5)
{
$time = $arr_line[0];
$in = $arr_line[1];
$out = $arr_line[2];
if ($pre_time!=0 && $pre_time<=$hour_time) break;
if ($pre_time>$time && $pre_time<=$hour_time+3600)
{
$time_diff = $pre_time - $time;
$total_in += $pre_in * $time_diff;
$total_out += $pre_out * $time_diff;
}
$pre_time = $time;
$pre_in = $in;
$pre_out = $out;
}
}
echo date('Y-m-d H:i:s ==> ', $hour_time) . date("Y-m-d H:i:s \n", $hour_time+3600);
echo "Input: ${total_in} bits (" . $total_in / (8*1024*1024) ." MB)\n";
echo "Output: ${total_out} bits (" . $total_out / (8*1024*1024) ." MB)\n";
$total = $total_in+$total_out;
echo "Total: ${total} bits (" . $total / (8*1024*1024) ." MB)\n";
?>
--
Tags:
BBS
All Comments
Related Posts
可以合併成一行嗎或更簡短
By Olive
at 2007-04-25T01:53
at 2007-04-25T01:53
可以合併成一行嗎或更簡短
By Jacky
at 2007-04-24T11:56
at 2007-04-24T11:56
make有辦法顯示錯誤的地方嗎?
By Thomas
at 2007-04-24T10:12
at 2007-04-24T10:12
Re: BBS+unicode-rxvt+oxim
By Odelette
at 2007-04-24T08:54
at 2007-04-24T08:54
apache error
By Iris
at 2007-04-24T02:41
at 2007-04-24T02:41