Mercurial > ~darius > hgwebdir.cgi > adslstats
comparison adslstats.py @ 1:a795b6cd8b1a
Add command line options.
Update for BS4.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Mon, 18 Mar 2013 23:15:00 +1030 |
parents | 98fe11ea4c82 |
children | 3748cec0e322 f50214bca1ae |
comparison
equal
deleted
inserted
replaced
0:98fe11ea4c82 | 1:a795b6cd8b1a |
---|---|
3 # | 3 # |
4 # Parse ADSL link stats for Billion 7300G & generate RRD archives & graphs | 4 # Parse ADSL link stats for Billion 7300G & generate RRD archives & graphs |
5 # | 5 # |
6 ############################################################################ | 6 ############################################################################ |
7 # | 7 # |
8 # Copyright (C) 2007 Daniel O'Connor. All rights reserved. | 8 # Copyright (C) 2013 Daniel O'Connor. All rights reserved. |
9 # | 9 # |
10 # Redistribution and use in source and binary forms, with or without | 10 # Redistribution and use in source and binary forms, with or without |
11 # modification, are permitted provided that the following conditions | 11 # modification, are permitted provided that the following conditions |
12 # are met: | 12 # are met: |
13 # 1. Redistributions of source code must retain the above copyright | 13 # 1. Redistributions of source code must retain the above copyright |
28 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 28 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
29 # SUCH DAMAGE. | 29 # SUCH DAMAGE. |
30 # | 30 # |
31 ############################################################################ | 31 ############################################################################ |
32 | 32 |
33 import ConfigParser | |
33 import optparse | 34 import optparse |
34 import os | 35 import os |
35 import re | 36 import re |
36 import rrdtool | 37 import rrdtool |
37 import time | 38 import time |
38 import urllib | 39 import urllib |
39 from BeautifulSoup import BeautifulSoup | 40 from bs4 import BeautifulSoup |
41 | |
42 conf = ConfigParser.ConfigParser() | |
43 conf.add_section('global') | |
44 conf.set('global', 'username', 'admin') | |
45 conf.set('global', 'password', 'admin') | |
46 conf.set('global', 'name', 'dsl.dons.net.au') | |
47 | |
48 conflist = ['adslstats.ini'] | |
49 if ('HOME' in os.environ): | |
50 conflist.append(os.path.expanduser('~/.adslstats.ini')) | |
51 conf.read(conflist) | |
40 | 52 |
41 usage = '''%prog [options]''' | 53 usage = '''%prog [options]''' |
42 opts = optparse.OptionParser(usage) | 54 opts = optparse.OptionParser(usage) |
43 opts.add_option('-v', '--verbose', action="store_true", default=False, | 55 opts.add_option('-v', '--verbose', action="store_true", default=False, |
44 help="Enable debug output") | 56 help="Enable debug output") |
45 opts.add_option('-g', '--graph', action="store_true", default=False, | 57 opts.add_option('-g', '--graph', action="store_true", default=False, |
46 help="Generate a graph") | 58 help="Generate a graph") |
47 opts.add_option('-u', '--update', action="store_true", default=False, | 59 opts.add_option('-u', '--update', action="store_true", default=False, |
48 help="Update RRD") | 60 help="Update RRD") |
49 opts.add_option('-a', '--authname', action="store", default="admin", | 61 opts.add_option('-a', '--authname', action="store", default=conf.get('global', 'username'), |
50 help="Username to login to modem") | 62 help="Username to login to modem") |
51 opts.add_option('-p', '--password', action="store", default="admin", | 63 opts.add_option('-p', '--password', action="store", default=conf.get('global', 'password'), |
52 help="Password to login to modem") | 64 help="Password to login to modem") |
53 opts.add_option('-n', '--name', action="store", default="dsl", | 65 opts.add_option('-n', '--name', action="store", default=conf.get('global', 'name'), |
54 help="Hostname of modem") | 66 help="Hostname of modem") |
55 opts.add_option('-b', '--base', action="store", default="/home/darius/projects/adslstats/adslstats", | 67 opts.add_option('-b', '--base', action="store", default="/home/darius/projects/adslstats/adslstats", |
56 help="Base directory for RRD & PNGs") | 68 help="Base directory for RRD & PNGs") |
57 | 69 |
58 (options, args) = opts.parse_args() | 70 (options, args) = opts.parse_args() |
93 for i in statsdict: | 105 for i in statsdict: |
94 assert a[i].td.contents[0].contents[0] == statsdict[i] | 106 assert a[i].td.contents[0].contents[0] == statsdict[i] |
95 | 107 |
96 stats = ADSLStats() | 108 stats = ADSLStats() |
97 | 109 |
110 # Check if the modem is offline | |
111 if a[9].td.findNext('td').contents[0].contents[0].find('N/A') != -1: | |
112 return None | |
98 stats.upstream = cleannum(a[7].td.findNext('td').contents[0].contents[0]) # kbits | 113 stats.upstream = cleannum(a[7].td.findNext('td').contents[0].contents[0]) # kbits |
99 stats.downstream = cleannum(a[8].td.findNext('td').contents[0].contents[0]) # kbits | 114 stats.downstream = cleannum(a[8].td.findNext('td').contents[0].contents[0]) # kbits |
100 stats.nmup = cleannum(a[9].td.findNext('td').contents[0].contents[0]) # dB | 115 stats.nmup = cleannum(a[9].td.findNext('td').contents[0].contents[0]) # dB |
101 stats.nmdown = cleannum(a[10].td.findNext('td').contents[0].contents[0]) # dB | 116 stats.nmdown = cleannum(a[10].td.findNext('td').contents[0].contents[0]) # dB |
102 stats.attenup = cleannum(a[11].td.findNext('td').contents[0].contents[0]) # dB | 117 stats.attenup = cleannum(a[11].td.findNext('td').contents[0].contents[0]) # dB |
139 opener = urllib.FancyURLopener() | 154 opener = urllib.FancyURLopener() |
140 opener.prompt_user_passwd = lambda host, realm: (options.authname, options.password) | 155 opener.prompt_user_passwd = lambda host, realm: (options.authname, options.password) |
141 f = opener.open(statsurl) | 156 f = opener.open(statsurl) |
142 #f = open("adsl.html") | 157 #f = open("adsl.html") |
143 stats = getstats(f) | 158 stats = getstats(f) |
159 if stats == None: | |
160 if options.verbose: | |
161 print "Modem is offline" | |
162 return | |
144 if options.verbose: | 163 if options.verbose: |
145 print str(stats) | 164 print str(stats) |
146 updaterrd(rrdname, int(time.time()), stats) | 165 updaterrd(rrdname, int(time.time()), stats) |
147 | 166 |
148 # Generate a graph | 167 # Generate a graph |