monitor your network bandwidth in Linux with your own script

monitor your network bandwidth in Linux with your own script

·

3 min read

In this blog post, we will discuss how to monitor your network bandwidth usage in Linux using a custom script. Network monitoring is an essential task for any system administrator, as it allows them to track the performance of their network in real-time.

By monitoring their network bandwidth, they can identify potential issues like bottlenecking or high latency and take corrective action quickly.

We will go through a step-by-step process to create our own custom network monitoring script in Linux and show you how to use it in practice. Stay tuned, because by the end of this blog post you will be able to monitor your network like a pro!

The amount of data sent (uploaded) & received (downloaded) can be found out
using the following bash script.


netu() {
    # [net]work [u]sage: check network usage stats

    net_device=$(ip route | awk '/via/ {print $5}')
    TRANSMITTED=$(ifconfig "$net_device" | awk '/TX packets/ {print $6$7}')
    RECEIVED=$(ifconfig "$net_device" | awk '/RX packets/ {print $6$7}')

    printf "%s\n" "TRANSMITTED : $TRANSMITTED"
    printf "%s\n" "RECEIVED  : $RECEIVED"
}
  • Only works per session, i.e stats are gathered once you power up your PC (or login) and are lost when you shutdown.

  • Good to have if you have limited data availability & want to monitor your data usage.

Let's just review on what utilities we used here.

ip

Our first step is to find your networking interface in Linux (i.e the default networking interface) and its name.

default via 192.168.42.129 dev enp0s20u4u1 proto dhcp metric 100 
169.254.0.0/16 dev enp0s20u4u1 scope link metric 1000 
192.168.42.0/24 dev enp0s20u4u1 proto kernel scope link src 192.168.42.149 metric 100

The first line of output lists our default network interface. The interface name will be different in your case.

In this case, it is enp0s20u4u1.

ifconfig

This is a good old tool used by network professionals to configure a network interface. ifconfig $net_device will display the status of our default net device.

$ ifconfig enp0s20u4u1
enp0s20u4u1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.42.149  netmask 255.255.255.0  broadcast 192.168.42.255
        inet6 fe80::d464:9bdb:2b16:b27  prefixlen 64  scopeid 0x20<link>
        inet6 2405:204:322e:53d:18ef:6496:fbb7:9309  prefixlen 64  scopeid 0x0<global>
        inet6 2405:204:322e:53d:93c3:cff8:8680:78bb  prefixlen 64  scopeid 0x0<global>
        ether 96:a3:91:8e:68:de  txqueuelen 1000  (Ethernet)
  -->   RX packets 19334  bytes 14292555 (14.2 MB)
        RX errors 0  dropped 0  overruns 0  frame 0
  -->   TX packets 16514  bytes 3008589 (3.0 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

You see the RX and TX packets? Yup that's our stats. Now we will use awk to extract those packet infos.

awk

AWK is a pattern scanning language, generally used for data manipulation needs.

Now, this is not a tutorial about awk but let's just understand our use-case here. A simple awk statement might look like this.

awk '/pattern/ { action }'

So in our case, we want to look for pattern which matches TX packets.

$ ifconfig enp0s20u4u1 |  awk '/TX packets/'
        TX packets 18554  bytes 3411366 (3.4 MB)

Meh, this doesn't look good. We want that human-readable format 3.4MB. One thing which excites me is that we can access/read each individual field (or word or record) in that line. Each word can then be printed using print $<field-no> (an action)

awk fields when using ifconfig command in linux

Now we just print both the $6 and $7 fields.

$ ifconfig enp0s20u4u1 |  awk '/TX packets/ { print $6$7 }'
(5.7MB)