Pages

Friday, July 13, 2012

Measure network traffic with Linux

To measure your network traffic for a specific interface use netstat:

# netstat -i
Kernel Interface table
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0   1500 0   7758072      0      0 0       3305512      0      0      0 BMRU
lo    16436 0     79946      0      0 0         79946      0      0      0 LRU

The first column Iface shows the interface name, the second column the current MTU (in bits). The fourth and the eight column show how many packages were received and how many packages were transceived. In this case you need to multiple each package counter with the MTU for interface eth0 to get the amount of bits that were transmitted:

# echo 7758072*1500 | bc -l
11637108000
# echo 3305512*1500 | bc -l
4958268000

Next divide the results by 8 to get the amount of bytes transmitted:

# echo 7758072*1500/8 | bc -l
1454638500.00000000000000000000
# echo 3305512*1500/8 | bc -l
619783500.00000000000000000000

Finally divide the bytes by 1024 two times to get a more comfortable value in MB:

# echo 7758072*1500/8/1024/1024 | bc -l
1387.25137710571289062500
# echo 3305512*1500/8/1024/1024 | bc -l
591.07160568237304687500

The output above shows that ~1387 MB were received and ~591 MB were transceived - since operating system start! To get a more current value you need to look at netstat again and calculate a difference for the received and transceived packages:

# netstat -i
Kernel Interface table
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0   1500 0   7785987      0      0 0       3315598      0      0      0 BMRU
lo    16436 0     80021      0      0 0         80021      0      0      0 LRU

During the first netstat output and the second netstat output I copied a ~5MB file via scp. Next I will calculate the difference between the received and transceived packages from the first and the second netstat output:

# echo 7785987-7758072 | bc -l
27915
# echo 3315598-3305512 | bc -l
10086

Now 27915 packages were received and 10086 packages were transceived. Now wrap the calculation from above around the difference and here you go:

# echo \(7786052-7758072\)*1500/8/1024/1024 | bc -l
5.00321388244628906250
# echo \(3315702-3305512\)*1500/8/1024/1024 | bc -l
1.82211399078369140625

No comments:

Post a Comment