vergoh / vnstat

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

-l --json does not output to the stdout #206

Closed marchellodev closed 2 years ago

marchellodev commented 2 years ago

Hey!

I wanted to pipe the output of vnstat -i "wlp0s20f3" -l --json to the json decoder and then to the xorg bar to display the current Up / Down speeds of the network. Running this command in the terminal does output json lines, but piping that does not work. It seems like the json strings are not written in stdout, nor in stderr.

This is how i modified the command for piping to work, script -q -c "vnstat -i "wlp0s20f3" -l --json" (source)

It is probably not a bug, but I am still not sure why vnstat -i "wlp0s20f3" -l --json does not write to the stdout.

Thanks for such an amazing piece of software!

vergoh commented 2 years ago

vnstat -l --json >/tmp/json_test shows no output at all which proves that the command is writing to stdout (as the implementation doesn't have any checks for pipes being active). However, this other command I had running a the same time in another terminal shows the issue if you look at the file size:

$ while true ; do echo $(date ; ls -l /tmp/json_test) ; sleep 10 ; done
Sat Jul 10 00:45:26 EEST 2021 -rw-r--r-- 1 vergo users 0 Jul 10 00:45 /tmp/json_test
Sat Jul 10 00:45:36 EEST 2021 -rw-r--r-- 1 vergo users 0 Jul 10 00:45 /tmp/json_test
Sat Jul 10 00:45:46 EEST 2021 -rw-r--r-- 1 vergo users 4096 Jul 10 00:45 /tmp/json_test
Sat Jul 10 00:45:56 EEST 2021 -rw-r--r-- 1 vergo users 4096 Jul 10 00:45 /tmp/json_test
Sat Jul 10 00:46:06 EEST 2021 -rw-r--r-- 1 vergo users 4096 Jul 10 00:45 /tmp/json_test
Sat Jul 10 00:46:16 EEST 2021 -rw-r--r-- 1 vergo users 8192 Jul 10 00:46 /tmp/json_test
Sat Jul 10 00:46:26 EEST 2021 -rw-r--r-- 1 vergo users 8192 Jul 10 00:46 /tmp/json_test
Sat Jul 10 00:46:36 EEST 2021 -rw-r--r-- 1 vergo users 8192 Jul 10 00:46 /tmp/json_test
Sat Jul 10 00:46:46 EEST 2021 -rw-r--r-- 1 vergo users 12288 Jul 10 00:46 /tmp/json_test
Sat Jul 10 00:46:56 EEST 2021 -rw-r--r-- 1 vergo users 12288 Jul 10 00:46 /tmp/json_test
Sat Jul 10 00:47:06 EEST 2021 -rw-r--r-- 1 vergo users 12288 Jul 10 00:46 /tmp/json_test
Sat Jul 10 00:47:16 EEST 2021 -rw-r--r-- 1 vergo users 16384 Jul 10 00:47 /tmp/json_test
Sat Jul 10 00:47:26 EEST 2021 -rw-r--r-- 1 vergo users 16384 Jul 10 00:47 /tmp/json_test
Sat Jul 10 00:47:36 EEST 2021 -rw-r--r-- 1 vergo users 20480 Jul 10 00:47 /tmp/json_test
Sat Jul 10 00:47:46 EEST 2021 -rw-r--r-- 1 vergo users 20480 Jul 10 00:47 /tmp/json_test
Sat Jul 10 00:47:56 EEST 2021 -rw-r--r-- 1 vergo users 20480 Jul 10 00:47 /tmp/json_test
Sat Jul 10 00:48:06 EEST 2021 -rw-r--r-- 1 vergo users 24576 Jul 10 00:48 /tmp/json_test
Sat Jul 10 00:48:16 EEST 2021 -rw-r--r-- 1 vergo users 24576 Jul 10 00:48 /tmp/json_test
^C

The output isn't constant even with a 10 second poll interval (while the command is actually generating output every 2 seconds) because stdout is buffered. This isn't exactly a bug but anyway something that should be rather easy to improve as it should be enough to have vnStat flush stdout after every write when that (and few other similar) mode is being used.