How to add interface to vnstat to database for monitoring

See all GNU/Linux related FAQ
vnStat is a network traffic monitor for Linux and Unix-like systems. It logs network traffic at different intervals and doesn’t require root permissions to run. The implementation involves two commands: vnstat for querying traffic information and vnstatd for data retrieval and storage. Let’s see how to add a network interface to vnstat after installation for monitoring purposes.

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux or Unix terminal
Category Network Utilities
OS compatibility BSD Linux macOS Unix
Est. reading time 2 minutes

Adding a new network interface to vnstat to database for monitoring

  1. Open the terminal application.
  2. For the remote server, log in using the ssh command
  3. Use the tcpdump -D on Unix or ip link show on Linux to list network interfaces.
  4. To add a new network interface named wg0 type: sudo vnstat -i wg0 --add
  5. Restart the vnstat service, run: sudo systemctl restart vnstat.service OR sudo service vnstat restart, depending upon your Linux/Unix version.
  6. Wait for some time to collect the data.
  7. Now, the vnstat -i wg0 command can monitor the data.
Adding a new network interface (NIC) to vnstat database on Linux FreeBSD and Unix

Click to enlarge

Viewing stats for network interface

Type command:
$ vnstat -i {interface}
$ vnstat -i wg0


Database updated: 2024-05-26 05:39:20
   wg0 since 2024-05-26
          rx:  7.73 MiB      tx:  190.58 MiB      total:  198.30 MiB
                     rx      |     tx      |    total    |   avg. rate
       2024-05      7.73 MiB |  190.58 MiB |  198.30 MiB |      762 bit/s
     estimated      7.66 MiB |  232.44 MiB |  240.11 MiB |
                     rx      |     tx      |    total    |   avg. rate
         today      7.73 MiB |  190.58 MiB |  198.30 MiB |   81.70 kbit/s
     estimated     32.82 MiB |  809.53 MiB |  842.35 MiB |

You can see outputs in json or xml format for automation purpose or third party commands:
$ vnstat -i wg0
$ vnstat -i wg0 --json
$ vnstat -i wg0 --xml

XML outputs

<vnstat version="2.6" xmlversion="2">
 <interface name="wg0">
    <fiveminute id="27016"><date><year>2024</year><month>05</month><day>26</day></date><time><hour>05</hour><minute>25</minute></time><rx>751356</rx><tx>503868</tx></fiveminute>
    <fiveminute id="27015"><date><year>2024</year><month>05</month><day>26</day></date><time><hour>05</hour><minute>30</minute></time><rx>217500</rx><tx>219608</tx></fiveminute>
    <fiveminute id="27018"><date><year>2024</year><month>05</month><day>26</day></date><time><hour>05</hour><minute>35</minute></time><rx>7144884</rx><tx>199122280</tx></fiveminute>
    <fiveminute id="27020"><date><year>2024</year><month>05</month><day>26</day></date><time><hour>05</hour><minute>40</minute></time><rx>205084</rx><tx>228832</tx></fiveminute>
    <hour id="2253"><date><year>2024</year><month>05</month><day>26</day></date><time><hour>05</hour><minute>00</minute></time><rx>8318824</rx><tx>200074588</tx></hour>
    <day id="96"><date><year>2024</year><month>05</month><day>26</day></date><rx>8318824</rx><tx>200074588</tx></day>
    <month id="5"><date><year>2024</year><month>05</month></date><rx>8318824</rx><tx>200074588</tx></month>
    <year id="2"><date><year>2024</year><month>01</month><day>01</day></date><time><hour>00</hour><minute>00</minute></time><rx>8318824</rx><tx>200074588</tx></year>
    <top id="66"><date><year>2024</year><month>05</month><day>26</day></date><rx>8318824</rx><tx>200074588</tx></top>

Json outputs


You can use the jq command or sttr command to convert json to yaml:
$ vnstat -i wg0 --json | sttr json-yaml > outputs.yaml
You can fine tune outputs and query it as follows:
$ vnstat -i wg0 -5 #show 5 minutes
$ vnstat -i wg0 -h #show hours
$ vnstat -i wg0 -hg #show hours graph
$ vnstat -i wg0 -d #show days
$ vnstat -i wg0 -m #show months
$ vnstat -i wg0 -y #show years
$ vnstat -i wg0 -t #show top days

Important files for your vnstat service

All data is for each interface is by default stored in the /var/lib/vnstat/ database directory using sqlite. The default config file is /etc/vnstat.conf and can be edited to set default interface and other settings:
$ sudo vim /etc/vnstat.conf
On Linux with systemd as init you can view log files for errors or troubleshooting when adding or deleting interfaces. For example:
$ sudo journalctl -u vnstat.service
Otherwise for Unix and other systems try the /var/log/vnstat.log file:
$ sudo tail -f /var/log/vnstat.log

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

0 comments… add one

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.