Bandwidth monitoring through iptables

Do:

iptables -I INPUT ! -i lo; iptables -I OUTPUT ! -o lo

Then setup a cronjob that runs once a month when your host checks your bandwidth that does:

iptables -R INPUT 1 ! -i lo; iptables -R OUTPUT 1 ! -o lo

to reset it. You could do iptables -Z INPUT instead, but if you ever add any more rules to INPUT it would reset their counters too.

Next setup a cron job that runs regularly (I have it run every hour) that does:

iptables-save -c > /etc/iptables

That saves every table, all the chains in those tables, and all of the rules in those chains with their counters to the file /etc/iptables (feel free to pick a different name, that’s just what I use).

After that put:

iptables-restore -c < /etc/iptables

in your rc.local. This restores those rules and their counters on boot.

Now just do iptables -L -v to see your current bandwidth usage. Look at those rules’ counters, not the chains’ counters.

This entry was posted on Saturday, May 26th, 2007 at 8:33 am and is filed under gnu/linux. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

You must be logged in to post a comment.