Title: Simple network dashboard with vnstat | |
Author: Solène | |
Date: 25 November 2021 | |
Tags: openbsd networking nocloud | |
Description: A simple how-to for making an html dashboard with network | |
pictures about bandwidth usage | |
# 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 | |
vnstat project homepage | |
Take a look at Abhinav's Notes for a similar setup with NixOS | |
# 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. | |
```shell commands | |
# 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: | |
```shell script | |
#!/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 sources | |
<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: | |
```crontab code | |
# 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. | |
# Http server | |
Obviously you need to serve /var/www/htdocs/dashboard/ from your http | |
server, I won't cover this step in the article. | |
# 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). |