Monday, April 29, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 1793  / 2 Years ago, mon, april 25, 2022, 12:47:43

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?


More From » command-line

 Answers
0

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 for vnstat.



Save the data for on peak hours to vnstat1 and for off-peak to vnstat2. Make a daemon script that will switch them based on time of the day using an if condition in an infinite loop; if not between 02-08 hours use vnstat1 else disable vnstat1, enable vnstat2 and sleep.



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 the root user. (Just add the path to the script in /etc/rc.local for it to be executed at boot time).



#!/bin/bash
# vnstat_switcher.sh

# Select the interface to monitor e.g: eth0, eth1, ppp0
i=eth0

# Location of database used by vnstat
db1='/var/lib/vnstat1' # on-peak
db2='/var/lib/vnstat2' # off-peak

onpeakdb='/tmp/onpeak'
offpeakdb='/tmp/offpeak'

debug=false
iscatdata=true
current=0

# Create database for db1 if it doesn't exist
if ! [ -d "$db1" ]
then
mkdir -p "$db1"
vnstat -i $i --dbdir "$db1" -u
fi

# Create database for db2 if it doesn't exist
if ! [ -d "$db2" ]
then
mkdir -p "$db2"
vnstat -i $i --dbdir "$db2" -u
fi

$debug && echo 1
#vnstat -i $i --disable

while true
do
t=$( date %H )
$debug && t=$( date %S )
if [ "$t" -lt 2 ] || [ "$t" -gt 7 ] # if peak hours
then
$debug && echo 'On-peak hours'
vnstat -i $i --dbdir "$db1" -u
$debug && echo 2
if [ "$iscatdata" ]
then
vnstat -i $i --dbdir "$db2" > "$offpeakdb"
iscatdata=false
fi
vnstat -i $i --dbdir "$db1" > "$onpeakdb"
if [ $current != 1 ]
then
vnstat -i $i --disable --dbdir "$db2"
$debug && echo 3
vnstat -i $i --enable --dbdir "$db1" --sync
$debug && echo 4
current=1
fi
else
$debug && echo 'Off-peak hours'
vnstat -i $i --dbdir "$db2" -u
$debug && echo 5
if [ "$iscatdata" ]
then
vnstat -i $i --dbdir "$db1" > "$onpeakdb"
iscatdata=false
fi
vnstat -i $i --dbdir "$db2" > "$offpeakdb"
if [ $current != 2 ]
then
vnstat -i $i --disable --dbdir "$db1"
$debug && echo 6
vnstat -i $i --enable --dbdir "$db2" --sync
$debug && echo 7
current=2
fi
fi
$debug && sleep 1 || sleep 1m
done


#### Notes
# Ignore this message at first execution
# Interface "lo" is already disabled.
# Interface "lo" is already enabled.

#For debugging use interface 'lo' then
# `ping -s 2222 0.0.0.0`


Change i=eth0 on 5th line to the network interface you want to monitor.



To know the usage run cat /tmp/onpeak and cat /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.


[#26467] Tuesday, April 26, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
exceeeelh

Total Points: 21
Total Questions: 109
Total Answers: 120

Location: Marshall Islands
Member since Wed, Jan 5, 2022
2 Years ago
exceeeelh questions
Sun, Nov 20, 22, 17:08, 1 Year ago
Sat, Jan 1, 22, 08:04, 2 Years ago
Wed, May 12, 21, 05:11, 3 Years ago
;