Friday, April 11, 2014

IPv6 and IPv4 statistics without netstat

Here's a method to show IPv6 and IPv4 statistics on Linux without the netstat command. I was looking for a method "without netstat", because the very old netstat version 1.42 on Ubuntu (both 12.04 and 14.04) and Debian has a silly overflow bug:

$ netstat -s -6 | grep -i octet
    Ip6InOctets: -1
    Ip6OutOctets: 80610872
    Ip6InMcastOctets: 172080
    Ip6OutMcastOctets: 3448


So "-1" IPv6 bytes ... :-(

IPv6 statistics


Here's the solution with /proc/net/snmp6 (and no netstat needed):

$ cat /proc/net/snmp6 | grep -i octet | head -2 | awk '{ print $NF }' 
8325976854
80610872

To make it a bit more readable:

$ cat /proc/net/snmp6 | grep -i octet | head -2 | awk '{ print int($NF/1048576) " MB" }' 
7940 MB
76 MB

So 7940 MB downstream IPv6 traffic.

IPv4 statistics


The IPv4 statistics are in a different file called ... /proc/net/netstat:

$ cat /proc/net/netstat | grep -i ipext | tail -1 | awk '{ print $8 "\n" $9 }'
3813163404
32346989

And in MB's:

$ cat /proc/net/netstat | grep -i ipext | tail -1 | awk '{ print $8 "\n" $9 }' | awk '{ print int($NF/1048576) " MB" }'
3636 MB
30 MB



To be complete: the overflow for IPv4 traffic:

$ netstat -s | grep -i octet
    InOctets: -481799528
    OutOctets: 32350221
    InMcastOctets: 396

So a negative number of IPv4 bytes ....

Script


You can put this into a five line script:

#!/bin/sh

echo "IPv4 (down resp up):"

cat /proc/net/netstat | grep -i ipext | tail -1 | awk '{ print $8 "\n" $9 }' | awk '{ print int($NF/1048576) " MB" }'

echo "\nIPv6 (down resp up):"

cat /proc/net/snmp6 | grep -i octet | head -2 | awk '{ print int($NF/1048576) " MB" }' 

The result is something like:


$ ./ipv4-vs-ipv6.sh
IPv4 (down resp up):
3636 MB
31 MB

IPv6 (down resp up):
7971 MB
77 MB

You can download the script here


To do: realtime info, just like nload does ....








No comments: