SkillPilotAI

vnstat

Live tools show this second's traffic, but cannot tell how much data a server used yesterday or last month. vnstat keeps a long-term database of 5-minute, hourly, daily, monthly and yearly traffic totals per interface, with almost no load, plus JSON output and quota alerts.

189.1 Why is it used?

  • Answer "how much data did eth0 use today / this month / this year?".
  • Track a monthly data quota and alert before or when it is crossed.
  • Feed dashboards with --json, --xml or --oneline.
  • No packet capture: it reads kernel rx/tx byte counters, so it can run for years on a busy server.
  • Install: apt/dnf install vnstat + systemctl enable --now vnstat (Linux), brew install vnstat (macOS).

189.2 How it works

vnstatd, the database and the CLI
  1. 1.Client 192.168.209.20 sends traffic to the iperf3 server 192.168.209.10 over eth0

  2. 2.Kernel counts every byte in eth0's rx/tx counters

  3. 3.vnstatd reads the counters every 5 s (UpdateInterval)

  4. 4.Once a minute it saves the difference to vnstat.db (SQLite, /var/lib/vnstat)

  5. 5.The vnstat command only queries the database

  6. 6.Reports show buckets from 5 minutes up to a year

Lab config uses 5 s update and 1 min save so data shows up quickly

189.3 Lab: start the daemon

Terminal
grep -v "^#" /etc/vnstat.confvnstatvnstatd -n > /tmp/vnstatd.log 2>&1 &cat /tmp/vnstatd.log
Output
$ vnstatd -n > /tmp/vnstatd.log 2>&1 &$ cat /tmp/vnstatd.logNo interfaces found in database, adding available interfaces...Interface "eth0" added with 10000 Mbit bandwidth limit.-> 1 new interface found.Info: vnStat daemon 2.12 (pid:68 uid:0 gid:0, SQLite 3.45.3)Info: Monitoring (1): eth0 (10000 Mbit)
  • Before the daemon ran, vnstat could not open the database: the daemon creates it, the command only reads it.
  • A fresh database gives No data until the first save; after one minute vnstat -s showed 0 B with estimate --.

189.4 Traffic reports

Terminal
iperf3 -c 192.168.209.10 -t 30 -b 20Miperf3 -c 192.168.209.10 -t 20 -b 20M -Rvnstat
Output
$ vnstatDatabase updated: 2026-11-09 11:16:00    eth0 since 2026-11-09           rx:  47.88 MiB      tx:  71.77 MiB      total:  119.65 MiB    monthly                     rx      |     tx      |    total    |   avg. rate     ------------------------+-------------+-------------+---------------       2026-11     47.88 MiB |   71.77 MiB |  119.65 MiB |    4.78 Mbit/s     ------------------------+-------------+-------------+---------------     estimated    414.23 GiB |  620.97 GiB |    1.01 TiB |    daily                     rx      |     tx      |    total    |   avg. rate     ------------------------+-------------+-------------+---------------         today     47.88 MiB |   71.77 MiB |  119.65 MiB |    4.78 Mbit/s     ------------------------+-------------+-------------+---------------     estimated    101.99 MiB |  152.89 MiB |  254.88 MiB |
Terminal
vnstat -5 -i eth0vnstat -h -i eth0vnstat -d -i eth0vnstat -hg -i eth0vnstat -m -i eth0vnstat -y -i eth0vnstat -t -i eth0vnstat --95th -i eth0vnstat -s
Output
$ vnstat -d -i eth0 eth0  /  daily           day        rx      |     tx      |    total    |   avg. rate     ------------------------+-------------+-------------+---------------     2026-11-09    47.88 MiB |   71.77 MiB |  119.65 MiB |    4.78 Mbit/s     ------------------------+-------------+-------------+---------------      estimated    10.25 GiB |   15.37 GiB |   25.62 GiB |
OptionReportDefault entries
(none) / vnstat eth0Summary of the default / named interface-
-5 [N]5-minute buckets24
-h [N] / -hgHourly / last 24 hours as a bar graph (r = rx, t = tx)24
-d [N]Daily30
-m [N] / -y [N]Monthly / yearly12 (monthly)
-t [N]Top days10
--95th95th percentile and rates-
-sShort: month and today in one line each-
-q [a|s]Force query mode (multi-interface short / full)-
  • N limits rows (-d 7 = last 7 days); N = 0 shows all stored entries.
  • -5 showed two buckets: upload in the first, download in the second; -hg filled only the 11 o'clock column.
  • Estimates on a few minutes of data are unrealistic (1.01 TiB for the month); they help only with many days of data.
  • --95th warned: 5-minute data is kept 48 hours but a month needs 744, so coverage was 0.1%.

189.5 Range and length of a list

Terminal
vnstat -5 -b today -i eth0vnstat -d -b 2026-11-09 -e 2026-11-09vnstat -d -b 2024-06-01 -e 2024-06-30vnstat -5 -e "2026-11-09 11:17" --limit 3
Output
$ vnstat -5 -e "2026-11-09 11:17" --limit 3 -i eth0 eth0  /  5 minute          time        rx      |     tx      |    total    |   avg. rate     ------------------------+-------------+-------------+---------------     2026-11-09         11:10      1.93 MiB |   71.74 MiB |   73.67 MiB |    2.06 Mbit/s         11:15     45.95 MiB |   33.39 KiB |   45.98 MiB |    3.21 Mbit/s     ------------------------+-------------+-------------+---------------      sum of 2     47.88 MiB |   71.77 MiB |  119.65 MiB |
  • -b begin, -e end (date or "date time", or today), --limit N rows.
  • A range with no data prints No daily data available; with -e a sum line replaces the estimate.

189.6 Live monitoring

Terminal
vnstat -tr 5 -i eth0vnstat -l -i eth0vnstat -l 1 -i eth0
Output
$ vnstat -l 1 -i eth0Monitoring eth0...    (press CTRL-C to stop)        rx:   18.26 kbit/s   4.46 KiB      tx:   20.48 Mbit/s   4.88 MiB       rx:   15.36 kbit/s   8.21 KiB      tx:   19.96 Mbit/s   9.64 MiB       rx:    3.98 kbit/s   9.18 KiB      tx:    1.58 Mbit/s   10.02 MiB
  • -tr [s]: sample for s seconds (default 5); tx came out at 20 Mbit during iperf3.
  • -l: rate every second until Ctrl+C, then a max/average/min table; -l 0 packets/s mode; -l 1 adds transfer counters.
  • Both accept --json (-tr 10 --json gives a result without countdown).

189.7 Interfaces and database management

Terminal
vnstat --iflistvnstat --dbiflistvnstat --add -i lovnstat -i eth0+lo -dvnstat --remove -i lovnstat --remove -i lo --force
Output
$ vnstat --remove -i loWarning:The current option would remove all data about interface "lo" from the database. Add --force in order to really do that.$ vnstat --remove -i lo --forceInterface "lo" removed from database.The interface will no longer be monitored. Use --add if monitoring the interface is again needed.$ vnstat --dbiflistInterfaces in database: eth0
OptionMeaning
-i IF / -i if1+if2One interface (or alias) / merged report
--iflist [0|1|2]System interfaces: verbose / one per line / count
--dbiflist [0|1|2]Interfaces in the database
--add / --remove (--force)Create / delete a database entry
--rename NEW / --setalias ARename in the DB / display alias
--dbdir DIR / --config FILEUse another database directory / config file
--showconfigPrint settings in config format (non-default lines without ;)

189.8 Output format and style

Terminal
vnstat --oneline b -i eth0vnstat --json d -i eth0vnstat --xml d -i eth0vnstat -d --style 1 -i eth0vnstat -d -i eth0 --rateunit 0
Output
$ vnstat -d -i eth0 --rateunit 0          day        rx      |     tx      |    total    |   avg. rate     ------------------------+-------------+-------------+---------------     2026-11-09    47.88 MiB |   71.77 MiB |  119.65 MiB |   453.80 KiB/s     ------------------------+-------------+-------------+---------------      estimated     7.97 GiB |   11.95 GiB |   19.93 GiB |
  • --oneline: 15 fields separated by ;, b = all in bytes (perfect for scripts).
  • --json / --xml modes: s summary, f 5-min, h hourly, d daily, m monthly, y yearly, t top days, p 95th; add N to limit (--json d 7).
  • --style 0 narrow, 1 bar column (% rx, : tx), 2 bar + avg rate summary, 3 avg rate per row, 4 no control characters.
  • --rateunit 0 bytes / 1 bits; --locale LOC.

189.9 Alerts

Terminal
vnstat -i eth0 --alert 1 3 d total 1 MiBecho "exit code: $?"
Output
$ vnstat -i eth0 --alert 1 3 d total 1 MiB; echo "exit code: $?"   eth0 at 2026-11-09 11:18 for day 2026-11-09                           Alert limit exceeded!           daily |       total      |   percentage   |   avg. rate     ----------+------------------+----------------+--------------          used |       119.65 MiB |       11965.3% |   3.04 Mbit/s         limit |         1.00 MiB |                |      97 bit/s        excess |       118.65 MiB |                |     ----------+------------------+----------------+--------------     estimated |        16.31 GiB |       >100000%, +16.30 GiBexit code: 1
ArgumentValues
output0 never print, 1 always, 2 when estimate > limit, 3 when limit exceeded
exit0 always 0, 1 always 1, 2 estimate > limit -> 1, 3 exceeded -> 1 else 0
typeh d m y, or p / 95 / 95% (95th percentile rate)
conditionrx, tx, total, rx_estimate, tx_estimate, total_estimate
limit + unitB KiB MiB GiB TiB ... or KB MB GB ...; 95th needs a rate unit (bit/s ...)
  • Before traffic the same alert returned exit 0; with a 1 TiB limit exit is 0 and a bar shows what remains.
  • --alert 0 3 prints nothing but still exits 1 on breach (ideal for cron); exit mode 0 always returns 0.

189.10 Help and debug

Terminal
vnstat --helpvnstat --longhelpvnstat --versionvnstat -D -i eth0 -s
Output
$ vnstat -D -i eth0 -sDebug enabled, vnstat 2.12, SQLite 3.45.3Config file: /etc/vnstat.conf  c: Interface "eth0"   -> "Interface": "eth0"  c: DatabaseDir "/var/lib/vnstat"   -> "DatabaseDir": "/var/lib/vnstat"  i: UpdateInterval 5   -> "UpdateInterval": 5  i: PollInterval 2   -> "PollInterval": 2

189.11 Common mistakes

  • "Failed to open database" right after install: vnstatd is not running; systemctl enable --now vnstat.
  • Thinking vnstat captures packets and loads the CPU: it only reads counters.
  • Trusting estimates or --95th on a database only minutes old.
  • Using --remove without reading the warning: --force deletes all history of that interface.

189.12 Interview tips

  • Monthly quota: enable vnstat, check vnstat -i eth0 -m, cron vnstat -i eth0 --alert 0 3 m total 1 TiB (exit 1 = crossed, send a mail), export --json m / --oneline b for dashboards.
  • vnstatd collects, vnstat.db stores, vnstat only queries.
  • Live: iftop/nload; history: vnstat.
  • Resolutions -5 -h -d -m -y -t, ranges -b/-e/--limit.