Obtaining ethernet interface statistics under linux

The power surges and power outages hitting titahi bay in the last week adversely affected my main desktops ethernet card, all network connectivity died and th ethernet port status light became red instead of green.

Swapped out the network switch with a spare, still no network connectivity. Placed back original switch, other machines connected to that switch still had connectivity but not the desktop.

Rebooted the desktop, network connectivity restored but the ethernet port status light is still red instead of green. It is on the motherboard so having to replace it would be expensive. I have a apple USB network adapter lying around somewhere I will hunt out along with the linux drivers for it as I have had that working under linux before as the interface for a triple boot linux/windoze/hackintosh(snow leopard) system so should be able to get that working, plus a few spare USB wireless adapters I have had running under linux before if needed although I am short of USB ports.

Anyway I wanted to check the interface stats to see if the error condition was causing packet re transmissions and discovered the tools are not that useful.

The recommended tool is ethtool but that only provides partial statistics as seen below.


[root@phoenix ~]# ethtool -S enp3s0
NIC statistics:
tx_packets: 9581
rx_packets: 8774
tx_errors: 0
rx_errors: 0
rx_missed: 0
align_errors: 0
tx_single_collisions: 0
tx_multi_collisions: 0
unicast: 8496
broadcast: 55
multicast: 223
tx_aborted: 0
tx_underrun: 0

There are a lot more statistics available, ethtool for linux (for Fedora anyway) as seen above only reads some of them.

All the ethernet statistics available for Linux systems are available in the directory /sys/class/net/xxxx/statistics where xxxx is the interface name. For example for my interface name of enp3s0 we have all these files

[root@phoenix ~]# ls /sys/class/net/enp3s0/statistics
collisions rx_crc_errors rx_frame_errors rx_over_errors tx_carrier_errors tx_fifo_errors
multicast rx_dropped rx_length_errors rx_packets tx_compressed tx_heartbeat_errors
rx_bytes rx_errors rx_missed_errors tx_aborted_errors tx_dropped tx_packets
rx_compressed rx_fifo_errors rx_nohandler tx_bytes tx_errors tx_window_errors

So rather than use ethtool to obtain statistics it is better to use a script to check all the files as below. Obviously for my use I have expanded the script to check a parameter is provided and that the interface exists but the script below works and serves to provide a working example.


#!/bin/bash
ifname="$1"
ls /sys/class/net/${ifname}/statistics | while read fname
do
data=`cat /sys/class/net/${ifname}/statistics/${fname}`
echo "${fname}: ${data}"
done

The script basically returns all stats…

[root@phoenix ~]# ls /sys/class/net/enp3s0/statistics | while read x
do
   data=`cat /sys/class/net/enp3s0/statistics/${x}`
   echo "${x}: ${data}"
done
collisions: 0
multicast: 1506
rx_bytes: 13439448
rx_compressed: 0
rx_crc_errors: 0
rx_dropped: 0
rx_errors: 0
rx_fifo_errors: 0
rx_frame_errors: 0
rx_length_errors: 0
rx_missed_errors: 0
rx_nohandler: 0
rx_over_errors: 0
rx_packets: 29130
tx_aborted_errors: 0
tx_bytes: 4313586
tx_carrier_errors: 0
tx_compressed: 0
tx_dropped: 0
tx_errors: 0
tx_fifo_errors: 0
tx_heartbeat_errors: 0
tx_packets: 31090
tx_window_errors: 0
[root@phoenix ~]#

Anyway, you now know the easiest way to get ethernet statistics in Linux.

Obviously for real-time traffic throughput you still need iftop.

About mark

At work, been working on Tandems for around 30yrs (programming + sysadmin), plus AIX and Solaris sysadmin also thrown in during the last 20yrs; also about 5yrs on MVS (mainly operations and automation but also smp/e work). At home I have been using linux for decades. Programming background is commercially in TAL/COBOL/SCOBOL/C(Tandem); 370 assembler(MVS); C, perl and shell scripting in *nix; and Microsoft Macro Assembler(windows).
This entry was posted in Unix. Bookmark the permalink.