changeset 0:98fe11ea4c82

Initial commit of Billion ADSL stats monitor using RRD.
author darius@Inchoate
date Sat, 28 Mar 2009 17:53:25 +1030
parents
children a795b6cd8b1a b1048f889ef8
files adsl.html adslstats.py
diffstat 2 files changed, 394 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/adsl.html	Sat Mar 28 17:53:25 2009 +1030
@@ -0,0 +1,121 @@
+<html>
+<head>
+<meta http-equiv="content-type" content="text/html; charset=utf-8">
+<META http-equiv="Pragma" content="no-cache">
+<META http-equiv="Expires" content="-1">
+<link rel="stylesheet" type="text/css" href="default.css">
+<SCRIPT language="javascript">
+	function SetChannelOptions(index) { 
+	var form=document.adsl;
+	var default_annex='A';
+	a = new Array(7);
+	a[0] = new Option("Auto", "0");
+	a[1] = new Option("ADSL Multimode", "1");
+	a[2] = new Option("ADSL2", "2");
+	a[3] = new Option("ADSL2+", "3");
+	a[4] = new Option("G.Lite", "4");
+	a[5] = new Option("T1.413", "5");
+	a[6] = new Option("G.DMT", "6");
+
+	for(var i = form.ADSLRATE.length - 1; i > 0; i--) {
+		form.ADSLRATE.options[i] = null;
+	}
+	for(var i = form.ADSLMODE.length - 1; i > 0; i--) {
+		form.ADSLMODE.options[i] = null;
+	}
+	if (default_annex == 'A'){
+		b = new Array(4);
+		b[0] = new Option("Open Annex Type and Follow DSLAM's Setting", "0");
+		b[1] = new Option("Annex A", "1");
+		b[2] = new Option("Annex L", "2");
+		b[3] = new Option("Annex M", "3");
+		b[4] = new Option("Annex J", "4");
+		form.ADSLMODE.options[0] = b[0];
+		form.ADSLMODE.options[1] = b[1];
+		form.ADSLMODE.options[2] = b[2];
+		form.ADSLMODE.options[3] = b[3];
+		form.ADSLMODE.options[4] = b[4];
+		form.ADSLMODE.selectedIndex = index; 
+	}else{
+		b = new Array(1);
+		b[0] = new Option("Annex B", "1");
+		form.ADSLMODE.options[0] = b[0];
+		form.ADSLMODE.selectedIndex = index-1; 
+	}
+
+	if (index == 0 || index==1 ){	
+		form.ADSLRATE.options[0] = a[0];
+		form.ADSLRATE.options[1] = a[1];
+		form.ADSLRATE.options[2] = a[2];
+		form.ADSLRATE.options[3] = a[3];
+		form.ADSLRATE.options[4] = a[4];
+		form.ADSLRATE.options[5] = a[5];
+		form.ADSLRATE.options[6] = a[6];
+	}else{
+		form.ADSLRATE.options[0] = a[2];
+		form.ADSLRATE.options[1] = a[3];
+	}
+} 
+
+
+	function load_adsl(){
+		var form = document.adsl;
+		var  ADSL_MODE_VALUE= form.ADSL_MODE.value;
+		SetChannelOptions(form.ADSL_MODE.value);
+		if (ADSL_MODE_VALUE==0 || ADSL_MODE_VALUE==1)
+			form.ADSLRATE.selectedIndex = form.ADSL_RATE.value;
+		else 
+			form.ADSLRATE.selectedIndex = form.ADSL_RATE.value-2;
+	}
+	function verifyform() {
+	var form=document.adsl;
+       	form.apply.disabled=true;
+       	form.reset.disabled=true;
+	form.submit();
+	return true;
+	}
+	</SCRIPT>
+</head>
+<body class="main" link="#3300cc" alink="#ff0000" vlink="#990066" onload="load_adsl();">
+<table border="1" cellpadding="0" cellspacing="0" scrolling="no" style="border-collapse: collapse" bordercolor="#FFFFFF" width="80%">
+<FORM name=adsl method="post" ACTION="/goform/adsl">
+<tr><td class="headline" colspan=2>ADSL</td></tr>
+<tr><td colspan="2" class="header"><nobr>Parameters</nobr></td></tr>
+<tr><td class="title" width=35%><nobr>ADSL Mode</nobr></td>
+<td><nobr>
+	<INPUT type=hidden name="ADSL_MODE" value="0">
+	<SELECT name=ADSLMODE onchange="SetChannelOptions(this.selectedIndex);">
+<tr><td class="title" width=35%><nobr>Modulator</nobr></td>
+<td><nobr>
+	<INPUT type=hidden name="ADSL_RATE" value="0">
+	<SELECT name=ADSLRATE>
+<tr><td class="title" width=35%><nobr>DSP FirmwareVersion</nobr></td>
+<td><nobr>DMT FwVer: 3.7.6.98_A_TC, HwVer:T14F7_1.0
+
+<tr><td class="title" width=35%><nobr>DMT Status</nobr></td>
+<td><nobr>Up
+<tr><td class="title" width=35%><nobr>Operational Mode</nobr></td>
+<td><nobr>ADSL2
+<tr><td class="title" width=35%><nobr>Upstream</nobr></td>
+<td><nobr>1001 kbps
+<tr><td class="title" width=35%><nobr>Downstream</nobr></td>
+<td><nobr>6094 kbps
+<tr><td class="title" width=35%><nobr>Noise Margin (Upstream)</nobr></td>
+<td><nobr>6.3 db
+<tr><td class="title" width=35%><nobr>Noise Margin (Downstream)</nobr></td>
+<td><nobr>6.2 db
+<tr><td class="title" width=35%><nobr>Attenuation (Upstream)</nobr></td>
+<td><nobr>35.2 db
+<tr><td class="title" width=35%><nobr>Attenuation (Downstream)</nobr></td>
+<td><nobr>46.0 db
+<tr>
+<td class=headline colspan=2>
+<br>
+<INPUT type="button" name=apply value="Apply" onclick="verifyform();">&nbsp; 
+<INPUT type="button" name=reset value="Refresh" onClick="location.reload();">
+</td>
+</tr></FORM>
+</table>
+</body>
+</html>
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/adslstats.py	Sat Mar 28 17:53:25 2009 +1030
@@ -0,0 +1,273 @@
+#!/usr/bin/env python
+############################################################################
+#
+# Parse ADSL link stats for Billion 7300G & generate RRD archives & graphs
+#
+############################################################################
+#
+# Copyright (C) 2007 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 optparse
+import os
+import re
+import rrdtool
+import time
+import urllib
+from BeautifulSoup import BeautifulSoup
+
+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")
+opts.add_option('-a', '--authname', action="store", default="admin",
+                    help="Username to login to modem")
+opts.add_option('-p', '--password', action="store", default="admin",
+                    help="Password to login to modem")
+opts.add_option('-n', '--name', action="store", default="dsl",
+                    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/adsl.asp" % (options.name)
+rrdname = "%s.rrd" % (options.base)
+graphbasename = options.base
+
+matchnum = re.compile('([0-9]+(\.[0-9]+)?)')
+statsdict = {
+    7  : 'Upstream',
+    8  : 'Downstream',
+    9  : 'Noise Margin (Upstream)',
+    10 : 'Noise Margin (Downstream)',
+    11 : 'Attenuation (Upstream)',
+    12 : 'Attenuation (Downstream)' }
+
+
+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 cleannum(s):
+    s1 = matchnum.match(s).groups()[0]
+    try:
+        return int(s1)
+    except ValueError:
+        return float(s1)
+    
+def getstats(f):
+    s = BeautifulSoup(f)
+    a = s.findAll('tr')
+    
+    for i in statsdict:
+        assert a[i].td.contents[0].contents[0] == statsdict[i]
+
+    stats = ADSLStats()
+    
+    stats.upstream = cleannum(a[7].td.findNext('td').contents[0].contents[0])   # kbits
+    stats.downstream = cleannum(a[8].td.findNext('td').contents[0].contents[0]) # kbits
+    stats.nmup = cleannum(a[9].td.findNext('td').contents[0].contents[0])       # dB
+    stats.nmdown = cleannum(a[10].td.findNext('td').contents[0].contents[0])    # dB
+    stats.attenup = cleannum(a[11].td.findNext('td').contents[0].contents[0])   # dB
+    stats.attendown = cleannum(a[12].td.findNext('td').contents[0].contents[0]) # dB
+
+    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, the update the RRD
+def doupdate():
+    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 options.verbose:
+        print str(stats)
+    updaterrd(rrdname, int(time.time()), stats)
+
+# Generate a graph
+def gengraph():
+
+    linkargs = (
+        '-a', 'PNG',
+        '-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', 'PNG',
+        '--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.png" % (graphbasename),
+                  '--width', '1024',
+                  '--height', '256',
+                  '--start', 'end - 7d',
+                  '--end', 'now',
+                  *linkargs)
+
+    rrdtool.graph("%s-daily-link.png" % (graphbasename),
+                  '--width', '1024',
+                  '--height', '256',
+                  '--start', 'end - 365d',
+                  '--end', 'now',
+                  *linkargs)
+
+
+    rrdtool.graph("%s-hour-signal.png" % (graphbasename),
+                  '--width', '1024',
+                  '--height', '256',
+                  '--start', 'end - 7d',
+                  '--end', 'now',
+                  *signalargs)
+
+    rrdtool.graph("%s-daily-signal.png" % (graphbasename),
+                  '--width', '1024',
+                  '--height', '256',
+                  '--start', 'end - 365d',
+                  '--end', 'now',
+                  *signalargs)
+
+                  
+if __name__ == "__main__":
+    if options.update:
+        try:
+            os.stat(rrdname)
+        except OSError, e:
+            if e.errno == 2:
+                print "rrd not found, creating.."
+                makerrd(rrdname)
+
+        doupdate()
+
+    if options.graph:
+        gengraph()
+