How to Monitor Monthly Bandwidth Usage in Linux

0
388

As a server owner, it’s important to keep track of your network usage over time. Many hosting providers will charge for bandwidth and transmitted data, so you’ll want to keep tabs on your month-to-month usage.

Install vnstat

There are plenty of bandwidth monitoring tools out there—most real-time monitoring tools like htop and glances will show Rx (received) and Tx (transmitted) out. However, it’s much more useful to look at daily and monthly averages, and to do that, you’ll need a tool that can keep logs over time.

Of course, if you’re hosting your servers on a big cloud provider like AWS or GCP, they will probably have built-in log collection tools like AWS CloudWatch and GCP Cloud Monitoring. For a generic Linux solution though, you’ll want to install vnstat.

vnstat monitors all network interfaces, and keeps logs on how much traffic your servers are handling, which can be used to present monthly, daily, and hourly averages of traffic. It also has the option of outputting to a PNG for a better looking graph.

vnstat is available from most main package managers. For Debian-based systems like Ubuntu, that would be:

sudo apt install vnstat

You’ll also want to install vnstati for image output:

sudo apt install vnstati

If it isn’t available on your package manager, you can download it from source, and use make to build it for your system.

vnstat will immediately start collecting data, but it will take a while for enough data to be collected to actually present anything. Come back to it in a few hours once it’s collected some data, and run vnstat to view its output:

vnstat

This shows received GiB (RX), and transmitted GiB (TX), as well as a total and an estimate based on prior usage if the logs are incomplete. Keep in mind that this is in Gibibytes, not Gigabytes, though the difference isn’t as much as the much smaller Gigabits.

If you want more detailed output, you can output hourly:

vnstat -h

To output an image summary, you can use the following command (-s for summary), replacing eth0 with whatever network device you want to view:

vnstati -s -i eth0 -o ~/network-log.png

You can also view hourly output in the same fashion.

vnstati -h -i eth0 -o ~/network-log.png

If you’d like to do more in-depth analysis, or send these logs off somewhere else, you can output all of vnstat‘s logs with the –json flag.