Mercurial > ~darius > hgwebdir.cgi > adslstats
view adslstats.py @ 11:0e1399e48678
Don't point at dev dir
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Thu, 08 Jan 2015 11:22:21 +1030 |
parents | 88cf0a88c826 |
children | a6297e8a42a3 |
line wrap: on
line source
#!/usr/bin/env python2 ############################################################################ # # Parse ADSL link stats for Billion 7300G & generate RRD archives & graphs # ############################################################################ # # Copyright (C) 2015 Daniel O'Connor. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # ############################################################################ import ConfigParser import optparse import os import re import rrdtool import sys import time import urllib from bs4 import BeautifulSoup conf = ConfigParser.ConfigParser() conf.add_section('global') conf.set('global', 'username', 'admin') conf.set('global', 'password', 'admin') conf.set('global', 'name', 'dsl.dons.net.au') conflist = ['adslstats.ini'] if ('HOME' in os.environ): conflist.append(os.path.expanduser('~/.adslstats.ini')) conf.read(conflist) usage = '''%prog [options]''' opts = optparse.OptionParser(usage) opts.add_option('-v', '--verbose', action="store_true", default=False, help="Enable debug output") opts.add_option('-g', '--graph', action="store_true", default=False, help="Generate a graph") opts.add_option('-u', '--update', action="store_true", default=False, help="Update RRD (implies -d)") opts.add_option('-m', '--munin', action="store", default=None, help="Output munin data for ARG") opts.add_option('-a', '--authname', action="store", default=conf.get('global', 'username'), help="Username to login to modem") opts.add_option('-p', '--password', action="store", default=conf.get('global', 'password'), help="Password to login to modem") opts.add_option('-n', '--name', action="store", default=conf.get('global', 'name'), help="Hostname of modem") opts.add_option('-b', '--base', action="store", default="/home/darius/projects/adslstats/adslstats", help="Base directory for RRD & PNGs") (options, args) = opts.parse_args() statsurl = "http://%s/statsadsl.html" % (options.name) rrdname = "%s.rrd" % (options.base) graphbasename = options.base matchnum = re.compile('([0-9]+(\.[0-9]+)?)') statsdict = { 3 : 'Status:', 8 : 'SNR Margin (0.1 dB):', 9 : 'Attenuation (0.1 dB):', 14 : 'Rate (Kbps):', } class ADSLStats(object): def __str__(self): return """Line Rate - Up: %d kbits, Down %d kbits Noise Margin - Up: %.1f dB, Down %.1f dB Attenuation - Up: %.1f dB, Down %.1f dB""" % (self.upstream, self.downstream, self.nmup, self.nmdown, self.attenup, self.attendown) def getstats(f): s = BeautifulSoup(f) a = s.findAll('tr') for i in statsdict: assert a[i].td.contents[0] == statsdict[i] stats = ADSLStats() # Check if the modem is offline if a[3].td.findNext('td').contents[0] != 'Up': return None # dB stats.nmdown = float(a[8].td.findNext('td').contents[0]) / 10.0 stats.nmup = float(a[8].td.findNext('td').findNext('td').contents[0]) / 10.0 stats.attendown = float(a[9].td.findNext('td').contents[0]) / 10.0 stats.attenup = float(a[9].td.findNext('td').findNext('td').contents[0]) / 10.0 # kBit stats.downstream = float(a[14].td.findNext('td').contents[0]) stats.upstream = float(a[14].td.findNext('td').findNext('td').contents[0]) return stats # Setup RRD # We expect data to be logged every 5 minutes # Average 12 5 minute points -> hourly stats (keep 168 - a weeks worth) # Average 288 5 minute points -> daily stats (keep 365 - a years worth) # Detemine minimum & maximum for an hour and keep a weeks worth. def makerrd(filename): rrdtool.create(filename, '--step', '300', 'DS:upstream:GAUGE:3600:32:25000', # Upstream (kbits) - 24mbit is ADSL2+ max 'DS:downstream:GAUGE:3600:32:25000', # Downstream (kbits) 'DS:nmup:GAUGE:3600:0:100', # Upstream Noise margin (dB) 'DS:nmdown:GAUGE:3600:0:100', # Downstream Noise margin (dB) 'DS:attenup:GAUGE:3600:0:100', # Upstream Attenuation (dB) 'DS:attendown:GAUGE:3600:0:100', # Downstream Attenuation (dB) 'RRA:AVERAGE:0.1:12:168', 'RRA:AVERAGE:0.1:288:365', 'RRA:MIN:0.1:12:168', 'RRA:MAX:0.1:12:168') # Update the RRD (format stats as expected) def updaterrd(filename, tstamp, stats): rrdtool.update(filename, '%d:%d:%d:%f:%f:%f:%f' % (tstamp, stats.upstream, stats.downstream, stats.nmup, stats.nmdown, stats.attenup, stats.attendown)) # Open the URL and call the parser def getdata(): opener = urllib.FancyURLopener() opener.prompt_user_passwd = lambda host, realm: (options.authname, options.password) f = opener.open(statsurl) #f = open("adsl.html") stats = getstats(f) if stats == None: return None return stats # Generate a graph def gengraph(): linkargs = ( '-a', 'SVG', '-X', '0', '--vertical-label', 'kbit/sec', '--slope-mode', 'DEF:upstream=%s:upstream:AVERAGE' % rrdname, 'DEF:upstreammin=%s:upstream:MIN' % rrdname, 'DEF:upstreammax=%s:upstream:MAX' % rrdname, 'DEF:downstream=%s:downstream:AVERAGE' % rrdname, 'DEF:downstreammin=%s:downstream:MIN' % rrdname, 'DEF:downstreammax=%s:downstream:MAX' % rrdname, 'CDEF:upstreamdif=upstreammax,upstreammin,-', 'CDEF:downstreamdif=downstreammax,downstreammin,-', 'LINE0:upstreammin#000000:', 'AREA:upstreamdif#00dc76::STACK', 'LINE1:upstream#00ff00:Upstream', 'LINE0:downstreammin#000000:', 'AREA:downstreamdif#ff8686::STACK', 'LINE1:downstream#ff0000:Downstream') signalargs = ( '-a', 'SVG', '--vertical-label', 'dB', '--slope-mode', 'DEF:upstream=%s:upstream:AVERAGE' % rrdname, 'DEF:downstream=%s:downstream:AVERAGE' % rrdname, 'DEF:nmup_=%s:nmup:AVERAGE' % rrdname, 'DEF:nmupmin_=%s:nmup:MIN' % rrdname, 'DEF:nmupmax_=%s:nmup:MAX' % rrdname, 'DEF:nmdown_=%s:nmdown:AVERAGE' % rrdname, 'DEF:nmdownmin_=%s:nmdown:MIN' % rrdname, 'DEF:nmdownmax_=%s:nmdown:MAX' % rrdname, 'DEF:attenup=%s:attenup:AVERAGE' % rrdname, 'DEF:attenupmin=%s:attenup:MIN' % rrdname, 'DEF:attenupmax=%s:attenup:MAX' % rrdname, 'DEF:attendown=%s:attendown:AVERAGE' % rrdname, 'DEF:attendownmin=%s:attendown:MIN' % rrdname, 'DEF:attendownmax=%s:attendown:MAX' % rrdname, 'CDEF:nmup=nmup_,10,*', 'CDEF:nmupmin=nmupmin_,10,*', 'CDEF:nmupmax=nmupmax_,10,*', 'CDEF:nmupdif=nmupmax,nmupmin,-', 'CDEF:nmdown=nmdown_,10,*', 'CDEF:nmdownmin=nmdownmin_,10,*', 'CDEF:nmdownmax=nmdownmax_,10,*', 'CDEF:nmdowndif=nmdownmax,nmdownmin,-', 'CDEF:attenupdif=attenupmax,attenupmin,-', 'CDEF:attendowndif=attendownmax,attendownmin,-', 'LINE0:nmupmin#000000:', 'AREA:nmupdif#5c5cff::STACK', 'LINE1:nmup#0000ff:Noise Margin - Up (1/10 dB)', 'LINE0:nmdownmin#000000:', 'AREA:nmdowndif#009a00::STACK', 'LINE1:nmdown#00ff00:Noise Margin - Down (1/10 dB)', 'LINE0:attenupmin#000000:', 'AREA:attenupdif#f98100::STACK', 'LINE1:attenup#ff0000:Attenuation - Up', 'LINE0:attendownmin#000000:', 'AREA:attendowndif#aaaaaa::STACK', 'LINE1:attendown#000000:Attenuation - Down') rrdtool.graph("%s-hour-link.svg" % (graphbasename), '--width', '768', '--height', '256', '--start', 'end - 7d', '--end', 'now', *linkargs) rrdtool.graph("%s-daily-link.svg" % (graphbasename), '--width', '768', '--height', '256', '--start', 'end - 365d', '--end', 'now', *linkargs) rrdtool.graph("%s-hour-signal.svg" % (graphbasename), '--width', '768', '--height', '256', '--start', 'end - 7d', '--end', 'now', *signalargs) rrdtool.graph("%s-daily-signal.svg" % (graphbasename), '--width', '768', '--height', '256', '--start', 'end - 365d', '--end', 'now', *signalargs) if __name__ == "__main__": names = ['Noise Margin (up)', 'Noise Margin (down)', 'Attenuation (up)', 'Attenuation (down)'] if options.munin != None: # Handle the wrapper passing us $0 directly tmp = options.munin.split('_') if len(tmp) > 1: options.munin = tmp[-1] if options.munin not in ['signal', 'sync']: print "Unknown data type" sys.exit(1) if len(args) > 0: if args[0] == 'config': if options.munin == 'signal': print '''graph_category adsl graph_title ADSL Signal Quality graph_args --base 1000 -l 0 graph_vlabel dB''' for n in names: name = n.translate(None, ' ()').lower() print '''%s.label %s %s.type GAUGE %s.max 100 %s.min 0''' % (name, n, name, name, name) elif options.munin == 'sync': print '''graph_category adsl graph_title ADSL Sync Speed graph_args --base 1024 -l 0 graph_vlabel kbit/sec up.label Up up.type GAUGE up.max 24000 up.min 0 down.label Down down.type GAUGE down.max 24000 down.min 0''' sys.exit(0) if options.update or options.munin: stats = getdata() if stats == None: if options.verbose: print "Modem is offline" if stats != None: if options.update: try: os.stat(rrdname) except OSError, e: if e.errno == 2: print "rrd not found, creating.." makerrd(rrdname) print '%d:%d:%d:%f:%f:%f:%f' % (int(time.time()), stats.upstream, stats.downstream, stats.nmup, stats.nmdown, stats.attenup, stats.attendown) if options.munin != None: if options.munin == 'signal': print '''noisemarginup.value %.1f noisemargindown.value %.1f attenuationup.value %.1f attenuationdown.value %.1f''' % (stats.nmup, stats.nmdown, stats.attenup, stats.attendown) elif options.munin == 'sync': print '''up.value %.1f down.value %.1f''' % (stats.upstream, stats.downstream) if options.graph: gengraph()