comparison adslstats.py @ 3:3748cec0e322

merge
author Daniel O'Connor <darius@dons.net.au>
date Wed, 20 Nov 2013 23:56:29 +1030
parents b1048f889ef8 a795b6cd8b1a
children 98d351a87043
comparison
equal deleted inserted replaced
2:b1048f889ef8 3:3748cec0e322
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()
90 stats.attendown = float(a[131].contents[0]) # dB 102 stats.attendown = float(a[131].contents[0]) # dB
91 stats.attenup = float(a[132].contents[0]) # dB 103 stats.attenup = float(a[132].contents[0]) # dB
92 104
93 stats.downstream = float(a[138].contents[0]) # kbit/sec 105 stats.downstream = float(a[138].contents[0]) # kbit/sec
94 stats.upstream = float(a[139].contents[0]) # kbit/sec 106 stats.upstream = float(a[139].contents[0]) # kbit/sec
107
108 # Check if the modem is offline
109 if a[9].td.findNext('td').contents[0].contents[0].find('N/A') != -1:
110 return None
111 stats.upstream = cleannum(a[7].td.findNext('td').contents[0].contents[0]) # kbits
112 stats.downstream = cleannum(a[8].td.findNext('td').contents[0].contents[0]) # kbits
113 stats.nmup = cleannum(a[9].td.findNext('td').contents[0].contents[0]) # dB
114 stats.nmdown = cleannum(a[10].td.findNext('td').contents[0].contents[0]) # dB
115 stats.attenup = cleannum(a[11].td.findNext('td').contents[0].contents[0]) # dB
116 stats.attendown = cleannum(a[12].td.findNext('td').contents[0].contents[0]) # dB
117 >>>>>>> other
95 118
96 return stats 119 return stats
97 120
98 # Setup RRD 121 # Setup RRD
99 # We expect data to be logged every 5 minutes 122 # We expect data to be logged every 5 minutes
130 opener = urllib.FancyURLopener() 153 opener = urllib.FancyURLopener()
131 opener.prompt_user_passwd = lambda host, realm: (options.authname, options.password) 154 opener.prompt_user_passwd = lambda host, realm: (options.authname, options.password)
132 f = opener.open(statsurl) 155 f = opener.open(statsurl)
133 #f = open("adsl.html") 156 #f = open("adsl.html")
134 stats = getstats(f) 157 stats = getstats(f)
158 if stats == None:
159 if options.verbose:
160 print "Modem is offline"
161 return
135 if options.verbose: 162 if options.verbose:
136 print str(stats) 163 print str(stats)
137 updaterrd(rrdname, int(time.time()), stats) 164 updaterrd(rrdname, int(time.time()), stats)
138 165
139 # Generate a graph 166 # Generate a graph