How to use vnStat and iftop

How to use vnStat and iftop to monitor data consumption within a VM

In the generation of cloud computing, virtual machines (VMs) have become very common, while the usage of physical machines has started to gradually decrease. In this post, we will introduce the tools, ‘vnStat’ and ‘iftop,’ which we can use to monitor data consumption in a VM.

First, I am creating a machine with the following resources:
20 GHz CPU
16 GB RAM
20 GB SSD
I am mounting the disk with Ubuntu 18.04 image that is available in CloudSigma’s library.

Ubuntu 18.04 : Pre-installed 64bit with VirtIO drivers, superuser, Python 2.7.15, Pip 18.0, OpenSSL 1.1.0i, Cloud-init and latest updates until 2018-09-30.

Next, I am updating all existing repos and packages on the machine.

Now that our system is up- to- date, I will move towards our tools, ‘vnStat’ and ‘iftop’.

VNSTAT

According to humdi, vnStat is a console-based network traffic monitor for Linux and BSD that keeps a log of network traffic for the selected interface(s). It uses the network interface statistics provided by the kernel as information source. This means that vnStat won’t actually be sniffing any traffic and also ensures light use of system resources.

Some of the features of vnStat are:

  1. Even if the system restarts, the data still persists.
  2. It can monitor multiple interfaces at the same time.
  3. There are several output options, example, day wise, week wise, etc.
  4. Data Retention policies can be configured by the user.
  5. Months can be configured according to the billing cycle.
  6. The tool itself is very light and resource usage is minimal.
  7. CPU usage will be the same regardless of the traffic.
Installation

Use the following command to install vnStat:

It should automatically detect the network interface. In case it doesn’t, I can check the interface with the command:

In my case I can see two interfaces:

I can ignore the loopback interface. The interface that I want to monitor is ‘ens3’. I can update this in vnStat by using the command:

Usage Guide

The usage guide can be seen using the command: vnStat –help

Usage will be described like this:

With the help of these commands, we can get the metrics of data usage.

For monitoring it easily and intuitively, we can install vnStat dashboard. Following are the steps to install the dashboard:

Install the following required dependencies:

After installation of the required dependencies, we will fetch the dashboard from github repository, unzip it and place it in our web server directory.

Next, we need to edit the configuration.

The following lines would need a change. I am marking the required changes in bold.

I can name the interface in order to make it easier to monitor. One can have multiple interfaces to monitor. We can add those here.

After you have made the changes, open <<IP-ADDRESS>>/vnstat

I am able to see the data in an elegant way now.

diagram

network traffic

IFTOP

iftop listens to network traffic on a named interface, or on the first interface it can find which looks like an external interface if none is specified. It displays a table of current bandwidth usage by pairs of hosts. We must run iftop with sufficient permissions to monitor all network traffic on the interface.

Installation

We can install Iftop using the following command:

Usage Guide

The usage guide can be seen using the command: iftop -h

Usage will be described like this:

It needs root permission to work. I am simply using sudo since my user cloudsigma is a sudoer user. I can also use the command ‘sudo su’ and then run all the commands as root user.

The following command would give me a display of all the connections on the interface ‘ens3’ and the data transactions:

A simpler way if the requirement is to capture the data traffic for a particular set of time:

The above command would summarise the data traffic transactions for the coming 30 seconds and after completion of the time, would give a table listing all the data usage and transfer rates.

Though iftop gives live transfer rates, it doesn’t store the data or have a dashboard for us to intuitively visualise and see the patterns. Hence, if we want to see live data transfer rates, we recommend iftop. For data traffic monitoring over a longer period, we recommend vnStat.

We hope you can use this guide to your advantage and apply the basics of how to use vnStat and iftop to monitor data consumption within a VM.