Persist data of VNstat

Is there a way to persist the data of VNstat so that it survives a device reboot?
Save it to the flash instead of RAM, at least periodically, or save to an attached USB-stick?
I tried backing up the data file and copy it back after a reboot, but that seemed not to work either.

The vnstat initscript sadly does not provide any infrastructure for that (you can do it, by modifying the initscript significantly - the wiki has some old guidance for that), but luci-app-nlbwmon does have the necessary knobs to enable value persistency (you still need to reconfigure it, but at least the basic infrastructure is there).

Can you elaborate luci-app-nlbwmon or at least provide me links on how to do it?

It's a different bandwith statistic package - the basics (non-persistent data storage by default) are simply opkg update && opkg install luci-app-nlbwmon.

Ah ok, found it. It doesn't seem to show the traffic per day. It just shows the overall traffic (but also the traffic per lan-device, which is also nice).

What i am looking for is traffic per day (maybe other resolutions), exactly like vnstat does but not loosing all the data by a simple reboot.
Can nlbwmon do that?

I only care about a rough idea of my monthly usage myself, but you may have luck by playing with the accounting intervals (advanced settings). nlbwmon is far from perfect, but good enough for me to have replaced vnstat (which I would have preferred to keep, but the data persistency question drove me into looking for alternatives).

Ah, i see. It persists the data peridoically. That can also be exported and evaluated by external tools.
Imho to complicated for my use case.

You can set the location of the vnstat database in /etc/vnstat.conf. Just edit it, so it points to a persistent directory:

# location of the database directory
DatabaseDir "[your directory]"

1 Like

Wow, blast from the past. There was a quite extensive discussion in a forum topic from 2017 (which I heavily participated in, I didn't even remember that I wrote the backup script on the wiki page ... I must be getting old).

I must admit that some time after, VNstat "fell out of use" on my router, and I don't even remember why. I seem to remember switching to nlbwmon sometime after, and then I even stopped using that, I think it misbehaved at some point and I never picked it up afterwards.

Truth be told, from time to time I am inclined to pick up the topic of bandwidth stats. I would be rather interested in what's the best option, "state of the art", now in 2020. Is it still nlbwmon, which seemed to be the consensus in late 2017?

1 Like

This might deserve its own topic, but I'm only using nlbwmon because it comes with db persistency without having to rewrite the initscript. While it's 'pretty' nlbwmon has two weaknesses:

  • a tendency to run amok (causing 100% CPU load on one core), this isn't reproducible, but happens from time to time.
  • missing IPv6 traffic (probably a race condition on boot, after restarting nlbwmon later it does cover IPv6 as well), this happens always.

Functionality wise, both vnstat and nlbwmon provide the data I'm looking for - just that I don't have to mess with nlbwmon to keep its data persistent.

I moved the database location in the config file to /mnt/sda1/vnstat (usb stick) and it seems to work. Have rebooted and still have the data saved.

nlbwmon seems interesting too. Especially because it shows the traffic per device, but i still havent found out how i get an overview on the data. Can it show how much traffic was generated on each day of a month and so on? (without exporting the data and create an own table calculation sheet)

1 Like

I don't believe you can break nlbwmon down by single days, nlbwmon seems to be purely accumulative within the given time period. Someone CMIIW.

I didn't investigate further but what the export looked like, it is at least possible do calculate that above the export.

As i said, VNstat already calculates and shows exactly what i am interested into. Seeing that i had xTB traffic across the last 3 month and 7 days and not being able to compare it to previous intervalls is pointless for me.

What I mean is that nlbwmon does not collect data "per day" and accumulate when asked for statistics, but rather accumulates data, invariably, "per accounting interval." The accounting period can be customized:

... but it cannot be changed after the fact. I suspect it is possible to set this accounting period to "1 day", change the number of retained intervals to more-than-the-default 10, and accumulate/compare days after the fact. But that is already a workaround, neither nlbw nor the LuCI interface would do this for you.

... would not happen unless you deliberately set it this way. The default interval is a month, starting on the 1st of each month.

What it comes down to is that nlbwmon and vnstat are both restricted to the two dimensions they chose to save data for and break down in display: nlbwmon does hosts and protocols, vnstat does days and hosts. There is also darkstat, which I suppose does pretty much the same as vnstat but in more of a real-time approach. And you could go even all-out and use something like InnoDB and Grafana (externally). I'm really not advocating for one over any other, I really have no dog in this race. If vnstat works for you, it works for you. :slight_smile:

Is it still working? Is it a working solution?

Yes. Been working just fine for me


Did you just point to the new location and restarted vnstat and it created the new database? Or did u move the old database from /var ?



Change this section to point to your persistent storage:

# location of the database directory
DatabaseDir "/your mnt point/vnstat”

And save changes.

Then restart the service:

/etc/init.d/vnstat restart

I copied the database, but i guess it will create a new one if you don't.

Still works for me.

great thanks, i tried it without copying and seems to work