As a server owner, it’s important to track your network usage over time. Many web hosts charge for bandwidth and data transmitted, so you’ll want to keep tabs on your monthly usage.
There are many tools for bandwidth monitoring – most real-time monitoring tools like htop and looks will show Rx (received) and Tx (transmitted). However, looking at daily and monthly averages is much more useful, and in order to do that, you’ll need a tool that can keep logs over time.
Of course, if you host your servers on a large cloud provider like AWS or GCP, they will likely have built-in log collection tools like AWS CloudWatch and GCP Cloud Monitoring. However, for a generic Linux solution, you will want to install vnstat.
vnstat monitors all network interfaces and keeps logs of the traffic managed by your servers, which can be used to present monthly, daily, and hourly traffic averages. It also has the option to output in PNG format for better graphics.
vnstat is available from most major package managers. For Debian based systems like Ubuntu this would be:
sudo apt install vnstat
You’ll also want to install vnstati for the image output:
sudo apt install vnstati
If it is not available in your package manager, you can download it from source, and use make to create it for your system.
vnstat will immediately start collecting data, but it will take some time for enough data to be collected to present anything. Get back to it in a few hours once it has collected some data, and run vnstat to view its output:
This shows GiB received (RX) and GiB transmitted (TX), as well as a total and estimate based on past usage if the logs are incomplete. Keep in mind that it is in gibibytes, not Gigabytes, although the difference is not as much as the much smaller Gigabits.
If you want a more detailed output, you can produce every hour:
To generate an image digest, you can use the following command (-s for the digest), replacing eth0 with the network device you want to display:
vnstati -s -i eth0 -o ~ / network-log.png
You can also view the hourly output in the same way.
vnstati -h -i eth0 -o ~ / network-log.png
If you want to do a deeper analysis or send these logs somewhere else, you can generate all the vnstat logs with the –json flag.