Bash script to collect network interface statistics (RX/TX)

Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0

During an Incident I was dealing with an issue that an amount of different *nix machines were receiving / transmitting data over the wrong interface.
Since we didn’t want to check every *nix machine manually to see which interface is used I wrote a simple bash script which collects necessary network interface statistics like received / transmitted bytes for every interface configured over time.

The script writes a log to a separate file with following format:

Wed Jan 21 14:58:13 CET 2015 NIC=eth7 RXbs=2807182787 TXbs=192412352

while NIC = Interface, RXbs = Received bytes per second and TXbs = Transmitted bytes per second.

We executed the script every minute by using the crontab and we collected and analyzed the log by using Splunk.

How the network statistics has been collected?

Linux writes the network statistics to several files which are refreshed in real time by the kernel:

/sys/class/net/$INTERFACE/statistics/rx_bytes: number of bytes received
/sys/class/net/$INTERFACE/statistics/tx_bytes: number of bytes transmitted

SunOS network statistics can be collected by using the following command:

dladm show-dev -p $INTERFACE -s

AIX network statistics can be collected by using the following command:

entstat $INTERFACE

You can download the script using the following link:

Check out the script and feel free to write a feedback into the comments.

Are there better ways to collect network interface statistics?

Leave a Reply

Your email address will not be published. Required fields are marked *