Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am looking into finding a way to track public bandwidth usage on a per-instance or per elastic IP basis. Amazon does not seem to offer these metrics. You can get total in/out bandwidth through their reporting mechanisms, but this includes private network bandwidth, and is account wide. You can use cloudwatch to gather more in depth metrics, but they also lump public and private bandwidth together. We are looking into rolling our own, but your servers are built with one interface, and any elastic IPs are NAT’d to that interface. Since everything goes through one interface, it is all lumped together.

Does anyone have any suggestions? Have you ever encountered a similar issue? That is a linux server environment with one interface from which you had to determine public bandwidth usage.

share|improve this question

1 Answer 1

One option would be to setup iptables to track information.

iptables -I INPUT -s
iptables -I INPUT ! -s
iptables -I OUTPUT -d
iptables -I OUTPUT ! -d

This will allow for you to see internet/local specifics. -s will match all amazon local ips and ! -s matches everything but amazon local ips (internet).

iptables --list --verbose
Chain INPUT (policy ACCEPT 93 packets, 7602 bytes)
 pkts bytes target     prot opt in     out     source               destination
   85  6930            all  --  any    any    !ip-10-0-0-0.ec2.internal/8  anywhere
    8   672            all  --  any    any     ip-10-0-0-0.ec2.internal/8  anywhere

Chain OUTPUT (policy ACCEPT 78 packets, 9307 bytes)
 pkts bytes target     prot opt in     out     source               destination
   70  8635            all  --  any    any     anywhere            !ip-10-0-0-0.ec2.internal/8
    8   672            all  --  any    any     anywhere             ip-10-0-0-0.ec2.internal/8

You will need to set up something to save iptables counters if you want to keep them beyond a reboot. The commands you need are:

iptables-save -c > filename
iptables-restore -c < filename

Some AMIs will save automatically. You need to modify their init script to use -c or disable it and replace it with your own.

If you want to be able to read the data more easily set up a cron job to run the iptables-save command on a regular basis so you can just parse that file. You could also upload that file to S3 or use scp to transfer it to a central statistics box. iptables-save and iptables-restore must be run as root and -c is required to save stats for individual rules.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.