I have recently downloaded vnstat
to monitor my Internet bandwidth usage. I have a limited data plan but my ISP provides unmetered bandwidth during off-peak hours (2:00 AM to 8:00 AM). So, I would like vnstat to report the data usage categorizing on-peak usage and off-peak usage separately.
I have gone through man vnstat
but couldn't find any help in this regard. Would it be possible to to get two different stats for on-peak and off-peak hours using vnstat
? If not, what alternatives are available to me to achieve the same?
AFAIK no bandwidth monitoring tool is better than
vnstat
. Unfortunately it does't support this feature. But you can achieve this by using multiple databases forvnstat
.Save the data for on peak hours to
vnstat1
and for off-peak tovnstat2
. Make a daemon script that will switch them based on time of the day using anif
condition in an infinite loop; if not between 02-08 hours usevnstat1
else disablevnstat1
, enablevnstat2
andsleep
.I wrote the following bash script. Save it in a file and make it executable using
sudo chmod x <filename>
. Test it for some time. Finally make the script auto start with OS. Run it as theroot
user. (Just add the path to the script in/etc/rc.local
for it to be executed at boot time).Change
i=eth0
on 5th line to the network interface you want to monitor.To know the usage run
cat /tmp/onpeak
andcat /tmp/offpeak
respectively.Also it's a good idea to reset the data on the default database (
/var/lib/vnstat
), as it will never update if everything in the script goes well.Hopefully this helps a bit.
So I am going to give you a program I wrote, because I was having problems with cron working. I don't know shell scripting unfortunately so this is in python, but it worked for me, and it is pretty simple, but nice. Sorry I didn't comment my code, but I really didn't want to go back through it. I know some parts might not be the best, but it works.
Quick Guide
Open up Idle, copy>paste.
Save as
filename.py
.Open Terminal.
Run it with Python 3 (python3 filename.py`).
Put in the hour you want to schedule for vnstat to run.
Put in the minute you want of that hour.
Put the amount of hours you want it to monitor for.
Put the amount of minutes you want it to monitor for.
Enter the device you are monitoring (I didn't go through and error check, so if you enter something like monkeybutt, it will try and run
vnstat -i monkeybutt
, every 15 seconds, just Ctrl+C out).The program will run through every 15 seconds to see if the time matches, it won't give any messages. It's meant to be run in the background. Once it starts, it will take a shot every 15 seconds of the network, on the device you chose. Using the vnstat command, plus a counter saying how many seconds you have left of monitoring.
After completing the scans it will ask if you want to exit, and give you an option if you want to show a report, otherwise you can just exit. All of the stuff is stored in vnstats database anyways.
Old part of post (may be helpful to some people)
You could get gnome-schedule, then put in your command for example vnstat -h, and set it reoccurring during times you are trying to monitor. Maybe too simple, but hopefully someone else could expand further.
Additional(Edit): I have not used vnstat very much, which is why I was hoping someone could expand on that part, but with the gnome-schedule, you can schedule a command to execute during specific parts of the day. So using vntstat -tr -l (traffic switch, monitor live traffic), would show the traffic while running (which you can schedule in gnome-schedule, but you may have to manually Ctrl+C] out.)
Otherwise I am sure you could pipe this into a file, or use the graphical output program for vnstat if you have that. Graphical output program is
vnstati -i 'filename'
will output it to png file. If you want further commandsvnstat --longhel
p has more commands.(Sample screen of how to put things into gnome-schedule.)
Do you know how to get a 'snapshot' of the bandwidth using vnstat already, or do you need further help on those commands? I can try to work with the program more.