vnstat: print current network activity to a file

1

How can i print the output of vnstat -l to a file? what i've tried:

vnstat -l &> file

but it just hangsup

teslasimus

Posted 2013-03-21T12:27:23.553

Reputation: 443

Answers

5

Remove the l option. From the vnstat man page:

   -l, --live mode
          Display current transfer rate for the selected interface in real time until interrupted. Statis‐
          tics  will be shown after interruption if the runtime was more than 10 seconds. An optional mode
          parameter can be used to select between the displaying of packets per second (mode 0) and trans‐
          fer  counters  (mode  1) during execution.  --style can also be used to affect the layout of the
          output.

So, the -l makes vnstat display output in "live mode", constantly updating, that's why you can't capture the output. If you want an easily parseable format use

vnstat --dumpdb

To just save the standard output do

vnstat > log.txt

If what you want is the current upload and download rate, vnstat is not the right tool for the job. Try sar from the sysstat package (see man sar for more info):

sudo apt-get install sysstat 
sudo sar -n DEV 1 1

To extract the current* download and upload rate for the interface wlan0 from sar's output do:

sudo sar -n DEV 1 1 | grep wlan0 | tail -n 1 | gawk '{print "Down: "$5,"Up: "$6}'

* What does "current" mean? sar (and any other method) will take some milliseconds to generate output.

terdon

Posted 2013-03-21T12:27:23.553

Reputation: 45 216

but i actually need the total current network activity. this will print stats of today, yesterday, this month etc – teslasimus – 2013-03-21T14:14:16.490

What do you mean? What is "current" network activity? You mean the upload/download rate? – terdon – 2013-03-21T14:17:38.210

yes. upload/download... – teslasimus – 2013-03-21T14:33:00.610

See updated answer, vnstat is not the right tool for the job. – terdon – 2013-03-21T14:36:07.730

0

I don't know if you want to print just final summary to file from some period you are going to monitor network activity, but you can cheat it this way:

vnstat -l -i <device> >> dump.txt

The only issue is that vnstat will write output to file every second so it is not the most convenient way. For continuous monitoring with log for every second or so terdon answered your question.

Michal Gonda

Posted 2013-03-21T12:27:23.553

Reputation: 1

Ok, but be aware of the problem that vnstat overwrites the status line again and again using backspaces so in the output you will se only the last status. You can replace the sequences of backspaces and expand the statuses to individual lines using for example: sed -re $'s/\b[\b ]+\b/\\n/g' dump.txt – pabouk – 2014-11-20T14:53:02.917

Tnak's for a good trick ;) – Michal Gonda – 2014-11-22T13:29:01.530