SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2011-03-16 00:56:54

hardran3
#! Junkie
From: forest town, lake land
Registered: 2011-02-26
Posts: 359

Bandwidth Logging for Mobile Broadband Connections

If anyone has a mobile broadband connection, they probably have a limit. A great tool to measure your daily/weekly/monthly usage is vnstat.

You only need to install the one package.

sudo apt-get install vnstat

During the install it will try to start the daemon, but it will fail. First you have to tell it what interfaces to monitor. Execute something like the following in your terminal, changing the interface to match your mobile connection.

sudo vnstat -u -i ppp0

If you want the totals to follow your billing month and not the actual month you will need to edit /etc/vnstat.conf. Change MonthRotate to whenever your billing date is.

# on which day should months change
MonthRotate 10

Now start the daemon:

sudo service vnstat start

Now to check how much bandwidth you have used you can simply type vnstat into your terminal. benj1 has wrote a python script to make it easy to use this info with conky. Thanks benj1! To use it with conky add something like this to your conky config.
For total usage:

Total:${alignr}${execi 300 vnstat-conky-script.py}

For sent monthly total:

Sent:${alignr}${execi 300 vnstat-conky-script.py -s}

For recieved monthly total:

Recieved:${alignr}${execi 300 vnstat-conky-script.py -r}

Last edited by hardran3 (2011-03-18 01:10:57)

Offline

Be excellent to each other!

#2 2011-03-16 01:14:36

crunchy
#! Junkie
From: Juneau, AK, USA
Registered: 2010-08-19
Posts: 461

Re: Bandwidth Logging for Mobile Broadband Connections

it's quite easy to integrate with your conky simple put this into your .conkyrc

${exec vnstat}

registered Linux user: #533379
registered #! user: #6769
Whenever someone calls me a computer 'nerd' or a 'Unix-based-system'
all I can think is: You just wait. In a couple of years. I'll be your IT. Then where will you be!

Offline

#3 2011-03-16 03:21:54

Hinterland
Member
From: UK
Registered: 2011-03-01
Posts: 14

Re: Bandwidth Logging for Mobile Broadband Connections

EDIT: on 32bit systems you need to update vnstat before it hits 4gb. From man vnstat:

RESTRICTIONS
       Updates  needs  to  be executed at least as often as it is possible for
       the interface to generate enough traffic to wrap the  kernel  interface
       traffic  counter.

You can change the update interval by making a cron job:

gksu gedit /etc/cron.d/vnstat

with this in it:

# 0-55/5 * * * * root if [ -x /usr/bin/vnstat ] && [ `ls /var/lib/vnstat/ | wc -l` -ge 1 ]; then /usr/bin/vnstat -u; fi
0-59 * * * * root if [ -x /usr/bin/vnstat ] && [ `ls /var/lib/vnstat/ | wc -l` -ge 1 ]; then /usr/bin/vnstat -u; fi

The #'d line updates vnstat every 5 mins the other every 1min

To display monthly totals in conky:

Rx (Month):$alignr${execi 300 vnstat -i XXXX -m | grep "`date +"%b '%y"`" | awk '{print $3 $4}'}
Tx (Month):$alignr${execi 300 vnstat -i XXXX -m | grep "`date +"%b '%y"`" | awk '{print $6 $7}'}

replacing XXXX with the interface you are using (eg wlan0)

Last edited by Hinterland (2011-03-16 03:36:26)

Offline

#4 2011-03-16 05:44:15

hardran3
#! Junkie
From: forest town, lake land
Registered: 2011-02-26
Posts: 359

Re: Bandwidth Logging for Mobile Broadband Connections

Hinterland wrote:

EDIT: on 32bit systems you need to update vnstat before it hits 4gb. From man vnstat:

RESTRICTIONS
       Updates  needs  to  be executed at least as often as it is possible for
       the interface to generate enough traffic to wrap the  kernel  interface
       traffic  counter.

You can change the update interval by making a cron job...

The cron job is not necessary as we are using vnstatd. It also fixes the 4GB wrap on 32 bit systems. From the vnstatd man page:

The purpose of vnstatd is to provide a more flexible way for updating vnstat(1) databases than what using cron for updating can provide. The daemon makes possible updating databases more often but at the same time requires less disk access since data can be cached and written only later to disk at a  user  configurable interval. It is also able to track how interfaces come and go without the need of additional scripts that are required with cron based updates.

Thanks for the conky lines smile

Last edited by hardran3 (2011-03-16 05:45:08)

Offline

#5 2011-03-16 07:24:21

Hinterland
Member
From: UK
Registered: 2011-03-01
Posts: 14

Re: Bandwidth Logging for Mobile Broadband Connections

Oops sorry! roll

Thanks for this, binned the cron and swapped to vnstatd smile

Last edited by Hinterland (2011-03-16 07:53:16)

Offline

#6 2011-03-16 20:07:38

benj1
Wiki Wizard
From: Yorkshire, England
Registered: 2009-09-05
Posts: 1,084

Re: Bandwidth Logging for Mobile Broadband Connections

I wrote this script for use with conky.

#!/usr/bin/env python
#
#       vnstat-conky-script.py
#       
#       get monthly download totals from vnstat, for display in conky
#
from subprocess import Popen,PIPE
import sys

def sizeformat(size):
    suffix = ['B','KB','MB','GB','TB']
    count = 0
    while size >= 1024 and count < len(suffix)-1:
        count += 1
        size = size/1024
    return "%.2f%s" % (size, suffix[count])
# get output
output = Popen(["vnstat", "--dumpdb"], stdout= PIPE).communicate()[0]
# get the correct line (first to start with m)
line = (i for i in output.split('\n') if i.startswith('m') ).next()        
#split line out into constituent parts
mon,mon_num,nixtime,rec_mb,tran_mb,rec_kb,tran_kb,inuse=line.split(';')

if len(sys.argv) == 1: #no args return total
    print sizeformat(((float(rec_mb)*1024*1024)+(float(rec_kb)*1024))
        +(float(tran_mb)*1024*1024)+(float(tran_kb)*1024))
elif sys.argv[1] == '-r': #received
    print sizeformat((float(rec_mb)*1024*1024)+(float(rec_kb)*1024))
elif sys.argv[1] == '-s':#sent
    print sizeformat((float(tran_mb)*1024*1024)+(float(tran_kb)*1024))
else: 
    print "'%s' flag not recognised, use -r flag (received), -s flag (sent) or no flag (total)" % sys.argv[1]

When called with the -r flag it returns the received monthly total, when called with the -s flag it returns the sent monthly total and when called without args it returns the overall sent/received monthly total.
All nicely formatted with mb/gb/tb?! roll (its futureproof I suppose).

Last edited by benj1 (2011-03-17 13:49:15)


- - - - - - - - Wiki Pages - - - - - - -
#! install guide           *autostart programs, modify the menu & keybindings
configuring Conky       *installing scripts

Offline

#7 2011-03-16 22:03:39

hardran3
#! Junkie
From: forest town, lake land
Registered: 2011-02-26
Posts: 359

Re: Bandwidth Logging for Mobile Broadband Connections

benj1 wrote:

I wrote this script for use with conky.

Thanks! It works great for the -r and -s options, but when I run it with no arguments I get an error.

~/bin$ vnstat-conky-script.py
Traceback (most recent call last):
  File "/home/ryan/bin/vnstat-conky-script.py", line 24, in <module>
    if sys.argv[1] == '-r': #received
IndexError: list index out of range

I can muddle my way through bash, but have no idea about python.

Offline

#8 2011-03-17 13:38:34

benj1
Wiki Wizard
From: Yorkshire, England
Registered: 2009-09-05
Posts: 1,084

Re: Bandwidth Logging for Mobile Broadband Connections

hardran3 wrote:
benj1 wrote:

I wrote this script for use with conky.

Thanks! It works great for the -r and -s options, but when I run it with no arguments I get an error.

~/bin$ vnstat-conky-script.py
Traceback (most recent call last):
  File "/home/ryan/bin/vnstat-conky-script.py", line 24, in <module>
    if sys.argv[1] == '-r': #received
IndexError: list index out of range

I can muddle my way through bash, but have no idea about python.

youre correct, looking at my conkyrc im using a -t flag, but any argument that isn't -s or -r will do, ill rewrite it to be a bit more obvious.

EDIT
ive updated my previous script so it should work as advertised, no flag returns the overall total, flags that aren't -s or -r return an error.

Last edited by benj1 (2011-03-17 13:51:42)


- - - - - - - - Wiki Pages - - - - - - -
#! install guide           *autostart programs, modify the menu & keybindings
configuring Conky       *installing scripts

Offline

#9 2012-04-17 19:33:10

marquardsen
New Member
Registered: 2012-04-03
Posts: 3

Re: Bandwidth Logging for Mobile Broadband Connections

It's working!!! Thanks a lot guys! cool

Now if I could just stop conky from disappearing from my desktop all the time... hmm

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo