3

I have two Linux machines that keep pinging each other. With each ping request they attach a data packet in the response. The frequency of the pings can not be determined.

How do I determine how many GB's of data they transfer to each other via the Network ?

3 Answers 3

7

Assuming you have root access to s Linux machine somewhere on the path (either endpoint, or even a router in between), you can write an iptables rule that matches the application traffic and directs it to a particular chain that does nothing but accept all packets. The kernel maintains a byte and packet counter on each chain. Assuming the packets you want to count go from IP 10.1.2.3 TCP port 42123 to IP 10.7.8.9 TCP port 42789:

iptables -I FORWARD -p tcp -s 10.1.2.3 --sport 42123 -d 10.7.8.9 --dport 42789 -j ACCEPT

Replace FORWARD by OUTPUT on the source host, by INPUT on the destination host. You can read the counters with iptables -nvxL FORWARD 42 where 42 is the number of the rules (the first rule is number 1).

Do this for each direction if you want to count both directions. Read the counters at known time intervals to measure bandwidth usage.

You can gain a little flexibility if you create an intermediate chain.

iptables -N myapp_counter
iptables -A myapp_counter -j ACCEPT
iptables -I FORWARD -p tcp -s 10.1.2.3 --sport 42123 -d 10.7.8.9 --dport 42789 -j myapp_counter
iptables -I FORWARD -p tcp -s 10.7.8.9 --sport 42789 -d 10.1.2.3 --dport 42123 -j myapp_counter

With this setup, the counter for the myapp_counter will include traffic in both directions. Another benefit of going through a chain is that you can atomically read the counter on the chain and set it to 0 with iptables -nvxZL myapp_counter. And you don't need to figure out the rule number, so it's easier to automate the setup.

4
  • It also may be beneficial to zero the counters when you list them (-Z with -L) so you don't have to remember the last counter value and perform a subtraction. Combining -Z with -L will atomically give you the current value and zero the counter.
    – camh
    Nov 29, 2010 at 23:46
  • @camh: Good point, but that only works for a chain. Is it possible to atomically read and set the counter on a rule? Nov 30, 2010 at 0:13
  • According to my iptables(8) man page: iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...] - that is you can specify a rule number and zero just a single rule. I have iptables v1.4.10 installed.
    – camh
    Nov 30, 2010 at 0:25
  • @camh: Ah, that's new in iptables 1.4.6. Before that, -Z only works on a chain, not on an individual rule. Nov 30, 2010 at 1:09
0

you can see statistics with ifconfig.

0

You can check which program is using data with nethogs (http://nethogs.sourceforge.net/).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .