Subject: Re: Bandwidth throttling once limit reached
To: None <netbsd-help@netbsd.org>
From: None <netbsd99@sudog.com>
List: netbsd-help
Date: 12/16/2002 10:45:01
On Monday 16 December 2002 05:00, J. Piers Hearn wrote:
> After further messing about I've ended up using netstat to dump out my data 
> usage, seeing as ipf doesn't seem to want to give my byte counts, only 
> packet counts. And I'm running a cron job to compare between old and new 
> totals to generate a running total.
> 
> So just as I get the point of looking at configuring altq, what happens? My 
> ISP decides to offer, and I quote: "The big addition is that we are now 
> offering a Flat-Rate option to existing plans, where once you hit either 
> the Peak or the Off-Peak Limit during your billing period then you will be 
> shaped to modem speeds until the beginning of the next billing period."
> Argh!!!
> Oh well, I think I'd prefer to track it myself anyway, it also lets me keep 
> a much closer eye on how much I'm using anyway...
> :-)
> Piers

Just for posterity, rrdtool is *really* cool for monitoring bandwidth and what 
your network connection is doing. Count rules in ipfilter are neat. Here's a 
quick example from my ipf.conf:

# To see the counters, use "ipfstat -aio"
count in on fxp1 from any to any
count out on fxp1 from any to any
count in on fxp0 from any to any
count out on fxp0 from any to any

... and then I use a small stupid Perl script and RRD's included Perl bindings 
to retrieve the values and plug them into an rrd database (see 
pkgsrc/databases/rrdtool):

#!/usr/pkg/bin/perl -w 

use RRDs;
$|=1;


if ( ! -f "fxp0.rrd" ) {
        print "Apparently fxp0.rrd doesn't exist..";
        if (!(RRDs::create 
("fxp0.rrd","--start=now","--step=5","DS:inbytes:COUNTER:3:U:U","DS:outbytes:COUNTER:3:U:U","RRA:AVERAGE:0.5:3:3075840"))) 
{
                print "RRDs::create failed..?\n";
                exit 1;
        } else {
                print "RRDs::create successful!\n";
        }
}

if ( ! -f "fxp1.rrd" ) {
        print "Apparently fxp1.rrd doesn't exist..";
        if (!(RRDs::create 
("fxp1.rrd","--start=now","--step=5","DS:inbytes:COUNTER:3:U:U","DS:outbytes:COUNTER:3:U:U","RRA:AVERAGE:0.5:3:3075840"))) 
{
                print "RRDs::create failed..?\n";
                exit 1;
        } else {
                print "RRDs::create successful!\n";
        }
}

$fxp0info=RRDs::info "fxp0.rrd";

print "Properties of fxp0.rrd:\n";
print "last updated: ", $$fxp0info{"last_update"}, "\n";

$outfxp1=""; $outfxp0=""; $infxp0=""; $infxp1="";
$er="";

while (1) {
        open $h, "/usr/sbin/ipfstat -aio |" or die "Unable to open pipe!\n";
        while (<$h>) {
                if (m/(\d+) count out on fxp1 from any to any/) {
                        $outfxp1=$1;
                } elsif (m/(\d+) count out on fxp0 from any to any/) {
                        $outfxp0=$1;
                } elsif (m/(\d+) count in on fxp1 from any to any/) {
                        $infxp1=$1;
                } elsif (m/(\d+) count in on fxp0 from any to any/) {
                        $infxp0=$1;
                }
        }
        close $h;

        $mytime=time;
        print "current time: ", time, " ";
        print $outfxp0." ".$outfxp1." ".$infxp0." ".$infxp1."\n";

        RRDs::update ("fxp0.rrd", "$mytime:$infxp0:$outfxp0");
        $er=RRDs::error;
        if ($er) {
                print "Error updating fxp0.rrd: $er\n";
                exit 1;
        }
        RRDs::update ("fxp1.rrd", "$mytime:$infxp1:$outfxp1");
        $er=RRDs::error;
        if ($er) {
                print "Error updating fxp1.rrd: $er\n";
                exit 1;
        }

        sleep 3;
}

exit 1;

... after that, I use the rrdtool's graphing capabilities (also in a tiny perl 
script) and end up with some very impressive-looking graphs. I like them 
better than what mrtg does, and they get regenerated *much* more quickly. 
Nice and fast. :-)

You mentioned that you were looking to be able to reset the values again--you 
can do this with ipfilter no problemo. Plus ipfilter "feels" more accurate, 
to me, than trying to use netstat and counting packets or fiddling around 
with options.

ttyl