Monitoring Bandwidth Usage In Linux: A Comprehensive Guide

how to monitor your internet bandwidth usage in linux

Monitoring your internet bandwidth usage is an essential step in understanding your connection and can help you identify and troubleshoot any connectivity problems. Linux offers a variety of tools for monitoring network traffic and bandwidth usage, which can be categorised into two types: Command-Line Interface (CLI) tools and Graphical User Interface (GUI) tools.

CLI tools are excellent for Linux servers without a graphical interface, allowing users to quickly get an overview of network bandwidth metrics directly in the terminal shell. Examples of CLI tools include iftop, nload, NetHogs, bmon, vnStat, and bwm-ng.

On the other hand, GUI tools present network data visually, often in graphs and charts, making it simpler for users with limited technical experience to monitor network bandwidth and performance. Examples of GUI tools include Nagios Core, Zabbix, Cacti, Site 24x7, and Monitorix.

By utilising these tools, you can maintain a stable network, pinpoint the causes of slowdowns, and quickly fix any malfunctions or issues.

Characteristics Values
Software vnstat, iftop, nethogs, nload, bmon, iperf, netperf, CBM, Nagios, Zabbix, Cacti, Site 24x7, ManageEngine, SolarWinds, Monitorix, Observium, EtherApe, Prometheus, Grafana, Fast, speedtest-cli, IPTraf, Darkstat
Installation Command sudo apt install vnstat, iftop, nethogs, nload, bmon, iperf, netperf, CBM, sudo snap install fast, sudo apt install speedtest-cli
Launch Command vnstat, iftop, nethogs, nload, bmon, iperf -s, netperf, CBM, Nagios, Zabbix, Cacti, Site 24x7, ManageEngine, SolarWinds, Monitorix, Observium, EtherApe, Prometheus, Grafana, fast, speedtest-cli, IPTraf, Darkstat
Interface CLI, GUI
Data Storage Stored data persists after a system reboot
Data Presentation Data is presented in the form of graphs, charts, and tables
Alerting Alerts can be set up when the bandwidth usage exceeds a certain limit
Compatibility Linux, Windows, FreeBSD, Cisco, HP, Dell
Pricing Free, Paid

shundigital

Using the vnstat software

VnStat is a fully-featured, command-line-based program to monitor Linux network traffic and bandwidth utilisation in real-time. It has the advantage of logging network traffic and bandwidth usage statistics for later analysis, and these logs can be viewed even after a system reboot.

Installation

You can install vnStat using the package manager. The command will differ depending on your Linux distribution:

$ sudo yum install sysstat # On Older CentOS/RHEL & Fedora

$ sudo dnf install sysstat # On CentOS/RHEL/Fedora/Rocky Linux & AlmaLinux

$ sudo apt-get install sysstat # On Debian/Ubuntu & Mint

$ sudo pacman -S sysstat # On Arch Linux

Running the Service

Once installed, you need to run the vnStat service so it can collect and store network usage statistics. You can use the `systemctl` command to enable and run the service:

$ systemctl enable --now vnstat

$ systemctl status vnstat

Viewing Statistics in the Console

To see the bandwidth usage statistics in the console, run the `vnstat` command. This will display the received, transmitted, total, and estimated traffic for each network interface. You can specify a particular interface with the `-i` parameter followed by the interface name.

To see the statistics on a daily basis, use the `-d` and `-i` parameters:

Vnstat -d -i eth0

Generating an Image

To visualise the bandwidth usage data in a picture, use the `vnstati` command. The parameters are similar to the `vnstat` command. To specify the output file, use the `-o` parameter followed by the filename.

To generate a monthly bandwidth usage image for the `eth0` interface, run:

$ vnstati -m -i eth0 -o monthly.png

You can also generate a summary with the `-s`, `-hs`, or `-vs` parameters. The `-s` parameter generates a basic summary of the last days' and months' bandwidth usage. The `-hs` and `-vs` parameters add the transfer rate of the last 24 hours and generate a horizontal or vertical image, respectively.

To generate a vertical summary with the `eth0` interface statistics, run:

$ vnstati -vs -i eth0 -o summary.png

Alerting When Bandwidth Usage Reaches a Limit

The vnStat functionality includes an `--alert` parameter that can be used to notify you when the bandwidth usage exceeds a certain limit. This can be used in a script that takes action when a limit is exceeded, such as sending an email alert. This script can then be configured as a daily cron job to automate the process of checking bandwidth usage.

shundigital

Viewing the statistics in the console

To view the statistics in the console, you can use the vnstat command. This will display the received, transmitted, total, and estimated traffic for each network interface.

To see the bandwidth usage, run the following command:

$ vnstat

This will display the received, transmitted, total, and estimated traffic for each network interface for the last two months. You can also view the information by the hour, day, month, and year by using the parameters -h, -d, -m, and -y, respectively.

If you have more than one network interface, you can specify it with the -i parameter followed by the interface name. This way, you can print the information only from the interface connected to the internet.

For example, to see the statistics on a daily basis for the eth0 interface, you can use the following command:

Vnstat -d -i eth0

This will print the eth0 interface usage day by day, including the received and transmitted data, as well as the total and average rate.

The vnstat command also allows you to set alerts when the bandwidth usage reaches a certain limit. You can use the --alert parameter to specify the reception, transmission, or total traffic, the output mode, the return code mode, and the limit in MiB, GiB, or TiB.

For example, to alert when the transmitted bandwidth usage exceeds 1 TiB, you can use the following command:

$ vnstat -i eth0 --alert 0 3 monthly tx 1 TiB

This will return a code of 1 if the limit is exceeded and 0 otherwise.

shundigital

Generating an image

The vnstat software is a great tool to monitor internet bandwidth and generate an image of the usage. It comes with a daemon that collects network usage and stores it, so the data persists after a system reboot.

To generate an image of the bandwidth usage, we can use the vnstati command. This command is similar to the vnstat command, which displays the bandwidth usage in the console.

To generate an image, we need to specify the output file using the -o parameter followed by the filename. We can also use parameters such as -m to view the usage on a monthly basis, -i to specify the network interface, and -s to generate a summary.

For example, to generate a monthly summary of the bandwidth usage for the eth0 interface and save it as an image called "monthly.png", we can use the following command:

$ vnstati -m -i eth0 -o monthly.png

This will create an image with similar content to the console output, including the received, transmitted, total, and estimated traffic for the specified interface.

In addition to generating an image, we can also use the vnstat software to alert us when the bandwidth usage exceeds a certain limit. This can be done using the --alert parameter, which takes several arguments such as reception, transmission, or total bandwidth usage.

shundigital

Alerting when the bandwidth usage reaches a limit

The vnstat software offers a useful --alert parameter that can be used to notify you when your bandwidth usage exceeds a certain limit. This parameter requires six arguments:

  • Reception, transmission, or total
  • The limit in KB, MB, or GB
  • The time interval (hourly, daily, monthly, or yearly)
  • The output mode (0 to suppress output, 1 to print a warning, or 2 to print an error)
  • The exit code (0 if the limit is not exceeded, 1 if it is, or 2 if an error occurs)
  • The network interface (eth0, wlan0, etc.)

For example, to alert when the transmitted bandwidth usage exceeds 1 TB in a month, you can use the following command:

Bash

Vnstat --alert 0 3 monthly tx 1TB

In this command, we use the values 0 and 3 for the output and exit code modes, respectively. This means that vnstat won't print any output, and the exit code will be 1 if the limit is exceeded and 0 otherwise.

You can incorporate this command into a script that performs an action when the limit is exceeded. For instance, you can send an email alert to yourself. Then, you can set up this script as a cron job to run daily, automating the process of checking your bandwidth usage.

Here's an example bash script that alerts when the transmitted bandwidth usage exceeds 1 TB:

Bash

#!/bin/bash

If ! vnstat -i eth0 --alert 0 3 monthly tx 1TB; then

The eth0 interface exceeded the 1 TB transmitted limit

Send an email alert

Fi

In this script, we use the `!` character to check if the vnstat command returns any code other than 0, indicating that the limit has been exceeded. Inside the `if` body, you can add the action you want to take when the bandwidth usage is too high, such as sending an email.

Another option for alerting when your bandwidth usage reaches a limit is to use a monitoring tool like Nagios. Nagios is a popular open-source monitoring tool that can alert you when your bandwidth usage is too high or too low. You can configure thresholds for warning and critical levels, and Nagios will send you alerts when these thresholds are crossed.

shundigital

Using the /proc/net/dev file

The /proc/net/dev file contains the network status, including the total bandwidth received and transmitted for each device. This file can be used to monitor internet bandwidth usage by reading the total bytes received and transmitted for each interface. While this method does not provide historical data or track traffic by process ID, it offers an alternative to installing additional software.

Monitoring Bandwidth Usage Since the Last Boot

The /proc/net/dev file records the total bandwidth usage since the last boot, which can be accessed using the cat command:

Bash

$ cat /proc/net/dev

Inter-| Receive | Transmit

Face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed

Lo: 2939975 515 0 0 0 0 0 0 2939975 515 0 0 0 0 0 0

Eth0: 2571860916 2242041 0 33 0 0 0 3365 136015011 1263791 0 0 0 0 0 0

Tap0: 12929438 10506 0 0 0 0 0 0 442513 4761 0 0 0 0 0 0

In the output above, the total received bytes are in the 2nd column, and the total transmitted bytes are in the 10th column. By ignoring the lo and tap0 interfaces and focusing on the eth0 interface, we can calculate the total received and transmitted bandwidth since the last system boot.

To process this file and monitor the bandwidth usage, we can use awk to calculate the received, transmitted, and total bandwidth usage in MiB:

Bash

$ awk '/^\s*eth0:/ { RX=$2/1024/1024 TX=$10/1024/1024 TOTAL=RX+TX print "RX:", RX, "MiB\nTX:", TX, "MiB\nTotal:", TOTAL, "MiB" }' /proc/net/dev

RX: 2453.29 MiB

TX: 129.875 MiB

Total: 2583.16 MiB

Writing a Log with Bandwidth Usage

To save the internet bandwidth usage over time and avoid losing information during system reboots, we can create a script that logs the bandwidth usage to a CSV file. This script will generate a new log file inside the /var/log folder for each month.

Bash

#!/bin/bash

LOG=/var/log/bandwidth_usage_$(date +%Y%m).csv

Echo -n "$(date --rfc-3339=date)," >> $LOG

Awk '/^\s*eth0:/ { RX=$2/1024/1024 TX=$10/1024/1024 TOTAL=RX+TX print RX "," TX "," TOTAL }' /proc/net/dev >> $LOG

The above script writes a new line to the CSV file containing the current date, followed by the received, transmitted, and total bandwidth usage in MiB. By running this script daily, we can log the bandwidth usage and calculate the monthly usage by subtracting the first and last rows of the file.

Frequently asked questions

You can use built-in Linux tools such as iftop and nethogs, or you can use native Linux tools like iptraf or custom scripts that read from the /proc/net/dev file.

You can use command-line (CLI) or graphical user interface (GUI) tools. CLI tools are excellent for Linux servers without a GUI, while GUI tools are better for users who want a visual representation of network data.

Popular CLI tools include iftop, nload, NetHogs, bmon, vnStat, and CBM.

Popular GUI tools include Nagios Core, Zabbix, Cacti, Site 24x7's NetFlow Analyzer, and Monitorix.

Monitoring network bandwidth usage can help you troubleshoot network issues, maintain network stability, identify high bandwidth usage sources, detect unusual activity, manage costs, and balance resource usage.

Written by
Reviewed by
Share this post
Print
Did this article help you?

Leave a comment