[olug] Bandwidth Metering

Sam Tetherow tetherow at shwisp.net
Mon Jun 5 23:28:07 UTC 2006


This may not be the most elegant solution, but it's worked for me. 
You'll want to rotate the log files every once in a while or date them 
by setting $IPACCT_DIR to something meaningful like YYYYMM or YYYYMMDD.

Hope it helps.

For each IP to be monitored:

iptables -A IPACCT -i $EXT -d IPADDR
iptables -A IPACCT -o $EXT -s IPADDR

Where EXT is the external interface and IPADDR is the ip address to be 
monitored

Every 5 minutes run:
#!/usr/bin/perl
# This is a simple script that runs every 5 minutes and records how many 
bytes have
# been sent and received foreach IP
# The output is appended to $IPACCT_DIR/$IPADDRESS

$IPACCT_DIR="/var/log/ipacct";

open(STREAM, '/sbin/iptables -L IPACCT -n -x -v -Z |');
$ts=time();
while(<STREAM>) {
if 
(/^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s*$/) {
# Didn't use all the values, but you might want them.
my ($pkt, $bytes, $proto, $opt, $in, $out, $source, $destination)=($1, 
$2, $3, $4, $5, $6, $7, $8);
if ($source eq "0.0.0.0/0") {
$downstream{$destination}=$bytes;
}
if ($destination eq "0.0.0.0/0") {
$upstream{$source}=$bytes;
}
}
}
foreach $ip (keys %downstream) {
# print "$ip\t$ts\t$downstream{$ip}/$upstream{$ip}\n";
open(LOG, ">>$IPACCT_DIR/$ip");
print LOG "$ts\t$downstream{$ip}\t$upstream{$ip}\n";
close(LOG);
}


Charles Bird wrote:
> anyone know of a good method of measuring bandwidth usage per IP within a given time frame?
>
> Wondering who has done this and if there are recommendations from whomever.
> I am assuming this would be a lil linux router task. 
> I am not looking to shape or limit, just monitor usage per IP.
> Thx in advance.
>
>
>   


-- 
   Sam Tetherow
   Sandhills Wireless




More information about the OLUG mailing list