DEV Community

Bhupesh Varshney 👾
Bhupesh Varshney 👾

Posted on • Originally published at bhupesh-v.github.io on

Monitor network (data) usage in linux

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" "$(tput bold)🔼 TRANSMITTED $(tput sgr0): $TRANSMITTED"
    printf "%s\n" "$(tput bold)🔽 RECEIVED    $(tput sgr0): $RECEIVED"
}

Enter fullscreen mode Exit fullscreen mode
  • 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 default networking device (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
Enter fullscreen mode Exit fullscreen mode

The first line of output lists our default network interface (the string followed by your PC name, mine is dev). The interface name will be different in your case.

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
Enter fullscreen mode Exit fullscreen mode

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 }'
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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


I know the last two rows are f**ked up. Maybe its a bug ?

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

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

Demo 🍲

netu-monitor-network-data-usage

And of course, you can watch this script to get real-time feedback as well.

watch -ct -n0 netu.sh
Enter fullscreen mode Exit fullscreen mode

Homework: Figure out how to make this data persist.

Also, have you ever been a class monitor in your school? I was once in 9th grade :)

Seems interesting?, Subscribe 🚀 to receive more such cool stuff or just connect with me on Twitter.

Top comments (2)

Collapse
 
piyush1104 profile image
Piyush Bansal

Hey, I changed the script for mac os. Hope it helps someone.

netu() {
    # [net]work [u]sage: check network usage stats
    ADDRESS=$(ifconfig en0 | grep inet | grep -v inet6 | cut -d ' ' -f2)
    TRANSMITTED_BYTES=$(netstat -ib -I en0 | grep $ADDRESS | awk '{print $10}')
    TRANSMITTED=$(bc <<< "scale=2; $TRANSMITTED_BYTES/1000000")
    RECEIVED_BYTES=$(netstat -ib -I en0 | grep $ADDRESS | awk '{print $7}')
    RECEIVED=$(bc <<< "scale=2; $RECEIVED_BYTES/1000000")

    printf "%s MB\n" "$(tput bold)🔼 TRANSMITTED $(tput sgr0): $TRANSMITTED"
    printf "%s MB\n" "$(tput bold)🔽 RECEIVED    $(tput sgr0): $RECEIVED"
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
bhupesh profile image
Bhupesh Varshney 👾

Looks 👌