3

Playing around with some low level functions to monitor my system stats.

I would like to get the current network utilization the same way like I can get cpu temp

cat /sys/class/thermal/thermal_zone0/temp

or fan speed

cat /sys/class/hwmon/hwmon6/fan1_input

Looking at

/sys/class/net/my_network_adapter/

I didn't find a way to see the actual bandwidth consumption, rx_bytes just gives the total amount of data downloaded.

Quasímodo
  • 18,625
  • 3
  • 35
  • 72
FuzzyTemper
  • 133
  • 4

2 Answers2

4

To get the rate of B/s, no need of anything but your shell: Simply read rx_bytes file at each second and compare the current value with the value one second before.

rx1=$(cat /sys/class/net/wlp3s0/statistics/rx_bytes)
while sleep 1; do
    rx2=$(cat /sys/class/net/wlp3s0/statistics/rx_bytes)
    printf 'Download rate: %s B/s\n' "$((rx2-rx1))"
    rx1=$rx2
done

Of course, substitute wlp3s0 by the interface you want to monitor.

Quasímodo
  • 18,625
  • 3
  • 35
  • 72
  • thats a nice little trick, will try it, thanks – FuzzyTemper Aug 20 '20 at 10:24
  • 1
    Here's a one-liner thanks to the example above for easier copy/paste into a terminal providing both rx and tx values: `iface=enp2s0; interval_s=3; dir=/sys/class/net/$iface/statistics; rxfile=$dir/rx_bytes; txfile=$dir/tx_bytes; rx1=$(cat $rxfile); tx1=$(cat $txfile); while sleep $interval_s; do rx2=$(cat $rxfile); tx2=$(cat $txfile); rxrate=$(expr $rx2 - $rx1); txrate=$(expr $tx2 - $tx1); echo "rx:$rxrate B/s (total $rx2 B) tx: $txrate B/s (total $tx2 B)"; rx1=$rx2; tx1=$tx2; done`, though I like the readability of Quasimodo's answer better. – jia103 Aug 21 '22 at 18:07
1

The Linux kernel doesn't keep track of the temporal network interface stats - you have to calculate the values by yourself.

There are multiple utilities and applications which do that for you, including Gnome System Monitor if you're a Gnome user, then KDE has KSysGuard/Plasma Network Monitor/KNemo and many others.

Artem S. Tashkinov
  • 26,392
  • 4
  • 33
  • 64
  • ok this means I cannot avoid using one of the "bloated" tools - I really liked the idea of using the lowest level command to keep workload at minimum, similar to the already mentioned methods. – FuzzyTemper Aug 17 '20 at 17:11
  • 1
    Speaking of bloat, there are console utilities to monitor network utilization as well: `iftop`, `ifstat`, `nload`, `bmon`, `iptraf`, `cbm`, `bwm-ng`, `speedometer`, `netload`, `dstat` and many more. – Artem S. Tashkinov Aug 17 '20 at 17:30