About me: My name is Solène Rapenne, pronouns she/her. I like learning and sharing knowledge. Hobbies: '(BSD OpenBSD Qubes OS Lisp cmdline gaming security QubesOS internet-stuff). I love percent and lambda characters. OpenBSD developer solene@. No AI is involved in this blog.

Contact me: solene at dataswamp dot org or @solene@bsd.network (mastodon).

I'm a freelance OpenBSD, FreeBSD, Linux and Qubes OS consultant, this includes DevOps, DevSecOps, technical writing or documentation work. If you enjoy this blog, you can sponsor my open source work financially so I can write this blog and contribute to Free Software as my daily job.

Simple network dashboard with vnstat

Written by Solène, on 25 November 2021.
Tags: #openbsd #networking #nocloud

Comments on Fediverse/Mastodon

1. Introduction §

Hi! If you run a server or a router, you may want to have a nice view of the bandwidth usage and statistics. This is easy and quick to achieve using vnstat software. It will gather data regularly from network interfaces and store it in rrd files, it's very efficient and easy to use, and its companion program vnstati can generate pictures, perfect for easy visualization.

My simple router network dashboard with vnstat
My simple router network dashboard with vnstat

vnstat project homepage

Take a look at Abhinav's Notes for a similar setup with NixOS

2. Setup (on OpenBSD) §

Simply install vnstat and vnstati packages with pkg_add. All the network interfaces will be added to vnstatd databases to be monitored.

# pkg_add vnstat vnstati
# rcctl enable vnstatd
# rcctl start vnstatd
# install -d -o _vnstat /var/www/htdocs/dashboard

Create a script in /var/www/htdocs/dashboard and make it executable:

#!/bin/sh

cd /var/www/htdocs/dashboard/ || exit 1

# last 60 entries of 5 minutes stats
vnstati --fiveminutes 60 -o 5.png

# vertical summary of last two days
# refresh only after 60 minutes
vnstati -c 60 -vs -o vs.png

# daily stats for 14 last days
# refresh only after 60 minutes
vnstati -c 60 --days 14 -o d.png

# monthly stats for last 5 months
# refresh only after 300 minutes
vnstati -c 300 --months 5 -o m.png

and create a simple index.html file to display pictures:

<html>
    <body>
        <div style="display: inline-block;">
                <img src="vs.png" /><br />
                <img src="d.png" /><br />
                <img src="m.png" /><br />
        </div>
        <img src="5.png" /><br />
    </body>
</html>

Add a cron as root to run the script every 10 minutes using _vnstat user:

# add /usr/local/bin to $PATH to avoid issues finding vnstat
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin

*/10  *  *  *  * -ns su -m _vnstat -c "/var/www/htdocs/dashboard/vnstat.sh"

My personal crontab runs only from 8h to 23h because I will never look at my dashboard while I'm sleeping so I don't need to keep it updated, just replace * by 8-23 for the hour field.

3. Http server §

Obviously you need to serve /var/www/htdocs/dashboard/ from your http server, I won't cover this step in the article.

4. Conclusion §

Vnstat is fast, light and easy to use, but yet it produces nice results.

As an extra, you can run the vnstat commands (without the i) and use the raw text output to build an pure text dashboard if you don't want to use pictures (or http).