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
  • 193,181
  • 53
  • 473
  • 722
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.

Andrew Collett
  • 103
  • 1
  • 7
muru
  • 193,181
  • 53
  • 473
  • 722
  • currently i am using the above command you had mentioned, it is generating json string with all the details even if I mention `-d` option – Sjn73 Oct 24 '17 at 16:18
  • 1
    @Sjn73 "and parse it in Python to get whichever field you need". You have the JSON, you're already going to parse it in Python anyway, so what difference does it make? If you're too lazy, then: `vnstat -d -i wlp2s0 --json | jq -r '.interfaces[0].traffic.total'` – muru Oct 24 '17 at 16:24
  • Thanks for showing the usage of `jq` to fetch the required field – Sjn73 Oct 24 '17 at 16:49
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
  • 35,754
  • 55
  • 92
  • 145
Andrew Collett
  • 103
  • 1
  • 7
  • If you wish to improve muru's answer, you may suggest an edit instead: https://askubuntu.com/posts/968645/edit – pomsky Mar 18 '18 at 21:45
  • Thanks. I have now done that. I made the edit suggestion, but now I see I did it anonymously. If someone can attach it to my account that would be great... – Andrew Collett Mar 20 '18 at 11:35
  • 1
    I rejected the original edit, now you can submit it again using your account – muru Mar 20 '18 at 12:32
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,306
  • 5
  • 30
  • 49