diff 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
line wrap: on
line diff
--- a/adslstats.py	Wed Nov 20 23:55:38 2013 +1030
+++ b/adslstats.py	Wed Nov 20 23:56:29 2013 +1030
@@ -5,7 +5,7 @@
 #
 ############################################################################
 #
-# Copyright (C) 2007 Daniel O'Connor. All rights reserved.
+# Copyright (C) 2013 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
@@ -30,13 +30,25 @@
 #
 ############################################################################
 
+import ConfigParser
 import optparse
 import os
 import re
 import rrdtool
 import time
 import urllib
-from BeautifulSoup import BeautifulSoup
+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)
@@ -46,11 +58,11 @@
                     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",
+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="admin",
+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="dsl",
+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")
@@ -92,6 +104,17 @@
 
     stats.downstream = float(a[138].contents[0])	# kbit/sec
     stats.upstream = float(a[139].contents[0])		# kbit/sec
+    
+    # Check if the modem is offline
+    if a[9].td.findNext('td').contents[0].contents[0].find('N/A') != -1:
+	return None
+    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
+>>>>>>> other
 
     return stats
 
@@ -132,6 +155,10 @@
     f = opener.open(statsurl)
     #f = open("adsl.html")
     stats = getstats(f)
+    if stats == None:
+	if options.verbose:
+	    print "Modem is offline"
+	return
     if options.verbose:
         print str(stats)
     updaterrd(rrdname, int(time.time()), stats)