ActiveState Code

Recipe 576605: Count up transfer for 3 mobile broadband on a mac


The 3 (a UK mobile carrier) broadband dongle is handy, but its mac support is really awful. In particular, the interface software doesn't provide any straightforward way to see your total data transfer to date. This can lead to nasty surprises if you're using a pay-as-you-go plan.

This executable scans /var/log/ppp.log and totals up your data transfers over your dongle. It can optionally take a date as a command-line argument, in which case it only totals up usage after the given date.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/python

doc="""
Usage examples:

phone_data Jan  2 2009 08:42:13
phone_data
"""

from dateutil import parser
from dateutil.parser import parse
import sys

if len(sys.argv) > 1:
    if sys.argv[1] in ['-h', '--help']:
        print doc
        sys.exit()
    start_date = parse(' '.join(sys.argv[1:]))
else:
    start_date = None

f=file('/var/log/ppp.log')
sent = []
recv = []

in_phoneblock=False
for line in f:
    if line.find('Dialing: ATD*99***1#') > 0:
        if start_date is not None:
            this_date = parse(line.split(' : ')[0])
            if this_date > start_date:
                in_phoneblock = True
        else:
            in_phoneblock = True
    if in_phoneblock and line.find('Sent') > 0:
        print line,
        info=line.split(':')[3].split(' ')
        sent.append(int(info[2]))
        recv.append(int(info[5]))
        in_phoneblock = False
print '\nTOTALS: Sent: %i mb, received: %i mb, total: %i mb.\n'%(sum(sent)/1.e6, sum(recv)/1.e6, (sum(sent)+sum(recv))/1.e6)

Sign in to comment