{"id":1018,"date":"2019-12-26T16:43:07","date_gmt":"2019-12-26T04:43:07","guid":{"rendered":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/?p=1018"},"modified":"2019-12-26T17:36:59","modified_gmt":"2019-12-26T05:36:59","slug":"obtaining-ethernet-interface-statistics-under-linux","status":"publish","type":"post","link":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/?p=1018","title":{"rendered":"Obtaining ethernet interface statistics under linux"},"content":{"rendered":"<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>The recommended tool is <em>ethtool<\/em> but that only provides partial statistics as seen below.<\/p>\n<pre><code>\r\n[root@phoenix ~]# ethtool -S enp3s0\r\nNIC statistics:\r\ntx_packets: 9581\r\nrx_packets: 8774\r\ntx_errors: 0\r\nrx_errors: 0\r\nrx_missed: 0\r\nalign_errors: 0\r\ntx_single_collisions: 0\r\ntx_multi_collisions: 0\r\nunicast: 8496\r\nbroadcast: 55\r\nmulticast: 223\r\ntx_aborted: 0\r\ntx_underrun: 0\r\n<\/code><\/pre>\n<p>There are a lot more statistics available, <em>ethtool<\/em> for linux (for Fedora anyway) as seen above only reads some of them.<\/p>\n<p>All the ethernet statistics available for Linux systems are available in the directory <strong>\/sys\/class\/net\/xxxx\/statistics<\/strong> where <strong>xxxx<\/strong> is the interface name. For example for my interface name of enp3s0 we have all these files<\/p>\n<pre>[root@phoenix ~]# ls \/sys\/class\/net\/enp3s0\/statistics\r\ncollisions rx_crc_errors rx_frame_errors rx_over_errors tx_carrier_errors tx_fifo_errors\r\nmulticast rx_dropped rx_length_errors rx_packets tx_compressed tx_heartbeat_errors\r\nrx_bytes rx_errors rx_missed_errors tx_aborted_errors tx_dropped tx_packets\r\nrx_compressed rx_fifo_errors rx_nohandler tx_bytes tx_errors tx_window_errors\r\n<\/pre>\n<p>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.<\/p>\n<pre><code>\r\n#!\/bin\/bash\r\nifname=\"$1\"\r\nls \/sys\/class\/net\/${ifname}\/statistics | while read fname\r\ndo\r\ndata=`cat \/sys\/class\/net\/${ifname}\/statistics\/${fname}`\r\necho \"${fname}: ${data}\"\r\ndone\r\n<\/code><\/pre>\n<p>The script basically returns all stats&#8230;<\/p>\n<pre>[root@phoenix ~]# ls \/sys\/class\/net\/enp3s0\/statistics | while read x\r\ndo\r\n   data=`cat \/sys\/class\/net\/enp3s0\/statistics\/${x}`\r\n   echo \"${x}: ${data}\"\r\ndone\r\ncollisions: 0\r\nmulticast: 1506\r\nrx_bytes: 13439448\r\nrx_compressed: 0\r\nrx_crc_errors: 0\r\nrx_dropped: 0\r\nrx_errors: 0\r\nrx_fifo_errors: 0\r\nrx_frame_errors: 0\r\nrx_length_errors: 0\r\nrx_missed_errors: 0\r\nrx_nohandler: 0\r\nrx_over_errors: 0\r\nrx_packets: 29130\r\ntx_aborted_errors: 0\r\ntx_bytes: 4313586\r\ntx_carrier_errors: 0\r\ntx_compressed: 0\r\ntx_dropped: 0\r\ntx_errors: 0\r\ntx_fifo_errors: 0\r\ntx_heartbeat_errors: 0\r\ntx_packets: 31090\r\ntx_window_errors: 0\r\n[root@phoenix ~]#\r\n<\/pre>\n<p>Anyway, you now know the easiest way to get ethernet statistics in Linux.<\/p>\n<p>Obviously for real-time traffic throughput you still need <em>iftop<\/em>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/?p=1018\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1018","post","type-post","status-publish","format-standard","hentry","category-my-nux-thoughts-and-notes"],"_links":{"self":[{"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1018","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1018"}],"version-history":[{"count":8,"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1018\/revisions"}],"predecessor-version":[{"id":1026,"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1018\/revisions\/1026"}],"wp:attachment":[{"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1018"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1018"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1018"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}