Get network traffic statistic (in bytes) from Linux kernel

Linux kernel provides access to many system statistics. Therefore, sometimes it is not necessary to install additional utilities to view it. We can write our own simple and fast scripts, or make a large command as an alias.

What internet interface we are using

Interface statistic directories are located in the following way (may differ in names):

ls /sys/class/net/
...
enp4s0  enp5s0  lo

Choose the interface to which ip address is assigned, in my case it’s enp4s0 (this is the interface that communicates with Internet):

ip addr
...
2: enp4s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:1f:d0:27:bd:22 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.104/24 brd 192.168.0.255 scope global enp4s
....

The amount of traffic since the system start

The kernel counts traffic since start. There are also packet statistic files. They are located in the same directories.

Here is the file that stores the amount of bytes transferred to the network (don’t forget to replace interface name with yours):

cat /sys/class/net/enp4s0/statistics/tx_bytes
...
22355421

And this file stores the amount of bytes received from the network (don’t forget to replace interface name with yours):

cat /sys/class/net/enp4s0/statistics/rx_bytes
...
1501873001

The value in bytes is not very comfortable. Here is one big command that outputs this information in kilobytes (don’t forget to replace interface with yours):

echo -e "$(expr $(< /sys/class/net/enp4s0/statistics/rx_bytes) / 1024)\tkB\tRECEIVED\n$(expr $(< /sys/class/net/enp4s0/statistics/tx_bytes) / 1024)\tkB\tTRANSFERRED"
...
1489171	kB	RECEIVED
22017	kB	TRANSFERRED

uptime -p
...
up 5 hours, 45 minutes

The amount of traffic per second

Here we will need to create variables that will store previous values and subtract it from current traffic. Then we can get traffic for a certain time, for example, per second.

The easiest way is to write a script than a command. Create a file and open it for editing:

nano traffic.sh

Copy the following lines inside the file and replace interface name with yours:

#!/bin/bash

INTERFACE_NAME='enp4s0'

while true; do
        RX1=$(< /sys/class/net/${INTERFACE_NAME}/statistics/rx_bytes)
        TX1=$(< /sys/class/net/${INTERFACE_NAME}/statistics/tx_bytes)
        sleep 1s
        RX2=$(< /sys/class/net/${INTERFACE_NAME}/statistics/rx_bytes)
        TX2=$(< /sys/class/net/${INTERFACE_NAME}/statistics/tx_bytes)
        TX_B=$(expr "$TX2" - "$TX1")
        RX_B=$(expr "$RX2" - "$RX1")
        TX_kB=$(expr "$TX_B" / 1024)
        RX_kB=$(expr "$RX_B" / 1024)
        echo "${INTERFACE_NAME}: ${TX_kB} kB transferred, ${RX_kB} kB received"
done

Make the script executable:

chmod +x ./traffic.sh

Then run it. Here I listen to the radio, therefore I have only input traffic:

./traffic.sh
...
enp4s0: 0 kB transferred, 61 kB received
enp4s0: 0 kB transferred, 70 kB received
enp4s0: 0 kB transferred, 61 kB received
enp4s0: 0 kB transferred, 50 kB received
enp4s0: 0 kB transferred, 55 kB received
enp4s0: 0 kB transferred, 60 kB received
enp4s0: 0 kB transferred, 52 kB received
enp4s0: 0 kB transferred, 44 kB received
^C

Leave a Reply

Your email address will not be published.