Andrew Mercer
on this page

What it is

vnstat keeps a small database of how many bytes each interface has transferred per hour, day, and month, without capturing packets. It answers "how much data did this box use this month?" with almost no overhead.

Install and check

sudo dnf install vnstat            # or apt-get install vnstat
sudo systemctl enable --now vnstat
vnstat -i eth0                      # summary for one interface
vnstat -h                           # hourly
vnstat -d                           # daily
vnstat -m                           # monthly
vnstat -l                           # live traffic

Data only starts accumulating after the daemon has been running for a while.

Emailed reports from cron

# crontab -e
@daily   vnstat -d 2>&1 | mail -s "vnstat daily report"   root
@weekly  vnstat -w 2>&1 | mail -s "vnstat weekly report"  root
@monthly vnstat -m 2>&1 | mail -s "vnstat monthly report" root

Cron needs a working local mail setup for these to arrive. See cron.

Graphs on a web page

vnstati renders PNG images from the database. Generate them hourly and serve the directory with any web server.

sudo mkdir -p /var/www/html/vnstat
for opt in s h d m t; do
  vnstati -$opt -i eth0 -o /var/www/html/vnstat/eth0-$opt.png
done
Flag Image
-s summary
-h hourly
-d daily
-m monthly
-t top 10 days

Crontab entry to refresh them:

@hourly for o in s h d m t; do vnstati -$o -i eth0 -o /var/www/html/vnstat/eth0-$o.png; done

An index.html with five <img> tags pointing at those files completes it. Keep the page on a trusted network or behind authentication: it reveals usage patterns.