1

I want to retrieve the daily internet usage from my Ubuntu machine in Json format using Vnstat. To fetch the daily usage in terminal I use following command:

vnstat -d -i wlp2s0

the output will be:

wlp2s0  /  daily

         day         rx      |     tx      |    total    |   avg. rate
     ------------------------+-------------+-------------+---------------
               ,ۋ6�        60 KiB |      27 KiB |      87 KiB |    0.01 kbit/s
               ,ۋ6�    333.00 MiB |  170.16 MiB |  503.16 MiB |   47.71 kbit/s
               ,ۋ6�    626.23 MiB |   39.64 MiB |  665.87 MiB |   63.13 kbit/s
               ,ۋ6�    172.47 MiB |  177.32 MiB |  349.79 MiB |   33.16 kbit/s
               ,ۋ6�     11.88 MiB |    1.66 MiB |   13.54 MiB |    1.28 kbit/s
               ,ۋ6�         0 KiB |       0 KiB |       0 KiB |    0.00 kbit/s
               ,ۋ6�    380.47 MiB |   21.22 MiB |  401.69 MiB |   38.09 kbit/s
               ,ۋ6�    173.32 MiB |   14.71 MiB |  188.03 MiB |   17.83 kbit/s
               ,ۋ6�         0 KiB |       0 KiB |       0 KiB |    0.00 kbit/s
               ,ۋ6�         0 KiB |       0 KiB |       0 KiB |    0.00 kbit/s
               ,ۋ6�     17.49 MiB |    4.33 MiB |   21.82 MiB |    2.07 kbit/s
               ,ۋ6�        70 KiB |      73 KiB |     143 KiB |    0.01 kbit/s
               ,ۋ6�     15.12 MiB |    1.95 MiB |   17.07 MiB |    1.62 kbit/s
               ,ۋ6�     18.45 MiB |    5.86 MiB |   24.31 MiB |    3.55 kbit/s
     ------------------------+-------------+-------------+---------------
     estimated        27 MiB |       7 MiB |      34 MiB |

So how to retrieve Only the total, rx and tx value that is estimated 27 MiB | 7 MiB | 34 MiB |from the above output in Json format in the form:

{"daily_usage":{"rx":27,"tx":7,"total":34}}

Actually I am trying to pass this json format to python script later Thaks in advance!!

muru
  • 207,228
Sjn73
  • 13
  • 1
  • 4

3 Answers3

2

vnstat has options to output in machine-readable formats. From man vnstat:

--json mode
  Show database content for selected interface or  all  interfaces
  in  json format. All traffic values in the output are in KiB. An
  optional mode parameter can be used for limiting the  output  to
  only  selected  information.   Everything  is  shown by default.
  Setting mode to 'h' will output only hours, 'd' days, 'm' months
  and 't' the top 10.

--xml mode
  Show database content for selected interface or  all  interfaces
  in  xml  format. All traffic values in the output are in KiB. An
  optional mode parameter can be used for limiting the  output  to
  only  selected  information.   Everything  is  shown by default.
  Setting mode to 'h' will output only hours, 'd' days, 'm' months
  and 't' the top 10.

Just do vnstat -i wlp2s0 --json d and parse it in Python to get whichever field you need. The -d is not needed, and will be ignored, since the --json option takes the mode argument.

muru
  • 207,228
1

@Sjn73, so, @muru has the right idea.

Only thing that I wanted to (but can't, yet) comment with is that you can simply write: vnstat --json d

That will switch the mode mentioned in the documentation to daily only. Note that this is an input to the --json flag, not the same thing as the -d flag.

Everything  is  shown by default.
  Setting mode to 'h' will output only hours, 'd' days, 'm' months
  and 't' the top 10.
David Foerster
  • 36,890
  • 56
  • 97
  • 151
0
#!/bin/bash

#get the last line
IN=$(vnstat -d | (tail -n1))
#remove estimated
INR=${IN//estimated}
#convert to array
arrOUT=(${INR//|/ })

#format the output
OUTPUT="{\"daily_usage\":{\"rx\": ${arrOUT[0]}, \"tx\": ${arrOUT[2]}, \"total\": ${arrOUT[4]} }"
OUTPUT2="{\"daily_usage\":{\"rx\": ${arrOUT[0]} ${arrOUT[1]}, \"tx\": ${arrOUT[2]} ${arrOUT[3]}, \"total\": ${arrOUT[4]} ${arrOUT[5]} }"

#pick one
echo $OUTPUT
echo $OUTPUT2
  • save into your_script.sh
  • change permissions on the file to make it executable
  • run as bash your_script.sh
derHugo
  • 3,376
  • 5
  • 34
  • 52