vergoh / vnstat

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

Allow to set fix units output #210

Closed eprodanov closed 2 years ago

eprodanov commented 2 years ago

Hello there!

I would like to ask about option to set fix output units. For example only in Mbit/s or only in kbit/s.

Thank you!

vergoh commented 2 years ago

Could you describe if there's some specific use case in mind for this request or simply put: why would it be nice to have such configuration option? Would this option apply to all outputs or only some of a certain type like the lists? Is this about the textual output, image output or both?

eprodanov commented 2 years ago

Hello there! Thank you very much for your answer!

I am using console version of vnstat for my php monitoring script (network part) and put results in DB. Once in 30 seconds I call script using crone like:

"vnstat -tr 2 -i enp0s25 | grep -iE "(rx|tx)" | awk {\'print $2\'}', $vnstat); //network load: $vnstat[0] - rx, $vnstat[1] - tx"

And, depending of load, I get kbits or Mbits, but I would like to set exactly Mbits and be very happy)

Sorry if my issue is stupid and have a great day =)

vergoh commented 2 years ago

Try this: vnstat -i enp0s25 -tr 2 --json | jq '.rx.bytespersecond,.tx.bytespersecond' Or directly in Mbit/s: vnstat -i enp0s25 -tr 2 --json | jq '.rx.bytespersecond/131072,.tx.bytespersecond/131072'

eprodanov commented 2 years ago

Thank you, but that didn't work for me:

parse error: Invalid numeric literal at line 1, column 9

jq seems does not like something..

vergoh commented 2 years ago

Which version of vnStat do you have? Does it produce json output with -tr 2 --json?

$ vnstat -tr 2 --json
{"jsonversion":"1","vnstatversion":"2.9 beta1","interface":"eth0","sampletime":2,"rx":{"ratestring":"2.25 KB/s","bytespersecond":2304,"packetspersecond":11,"bytes":4609,"packets":22},"tx":{"ratestring":"1.39 KB/s","bytespersecond":1422,"packetspersecond":6,"bytes":2844,"packets":12}}
eprodanov commented 2 years ago

Sorry, I had an 1.15 version, updated to 2.8 and your code works perfect!

vnstat -i enp0s25 -tr 2 --json | jq '.rx.bytespersecond/131072,.tx.bytespersecond/131072' - works perfect for me.

Thank you very much and have a great day!