vergoh / vnstat

vnStat - a network traffic monitor for Linux and BSD
GNU General Public License v2.0
1.41k stars 120 forks source link

allow monthly bandwidth tracking to start on a different day for each interface #174

Open paulo-erichsen opened 3 years ago

paulo-erichsen commented 3 years ago

description

this is a feature request for vnstat to allow keeping track of billing cycle usage for different interfaces that start on different days

can we make the MonthRotate option to be set on a per interface basis?

description for MonthRotate from vnstat.conf(5)

   MonthRotate
          Day of month that months are expected to change. Usually set to 1 but can be set to alternative values for example for tracking monthly billed traffic where  the
          billing  period  doesn't start on the first day. For example, if set to 7, days of February up to and including the 6th will count for January. Changing this op‐
          tion will not cause existing data to be recalculated. Value range: 1..28

use case

say I have different ISPs on different interfaces, or for example one interface could be a cellular connection while the other interface using a different ISP. I'd like to easily be able to get the monthly usage for both interfaces even though their billing cycles are different

example:

vergoh commented 3 years ago

Thanks for the feature request. I think this is something that should be possible to implement. Currently the main limiting factor is the implementation of the configuration file reader which doesn't support per interface configurations on a generic level. There's also the MonthRotateAffectsYears setting in the configuration of which I'm not sure if it too should be then be interface specific or not.

rwb196884 commented 4 months ago

Since this obviously isn't going to happen here's a script to get the total since some time.

begin=$( date -d "2024-06-01" +%s )
mb=$( vnstat -i hp --json | jq -r ".interfaces[0].traffic.fiveminute" | jq ".[] | select( .timestamp >= $begin ) | {\"t\": [.rx, .tx] | add}" | jq '[.t] | add' |
 paste -sd+ | bc | xargs -i echo "scale=0;{}/(1024*1024)" | bc )
rwb196884 commented 4 months ago

Or maybe not

# vnstat -i hp --json d | jq -r ".interfaces[0].traffic.day" | jq ".[] | select( .timestamp >= $begin ) | {\"t\": [.rx, .tx] | add}" | jq '[.t] | add' | past
e -sd+ | bc | xargs -i echo "scale=2;{}/(1024*1024)" | bc
769.40
# vnstat -i hp --json | jq -r ".interfaces[0].traffic.fiveminute" | jq ".[] | select( .timestamp >= $begin ) | {\"t\": [.rx, .tx] | add}" | jq '[.t] | add' | paste -sd+ | bc | xargs -i echo "scale=2;{}/(1024*1024)" | bc
260.49
#