diff 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
line wrap: on
line diff
--- a/adslstats.py	Sat Mar 28 17:53:25 2009 +1030
+++ b/adslstats.py	Mon Mar 18 23:15:00 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")
@@ -95,6 +107,9 @@
 
     stats = ADSLStats()
     
+    # 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
@@ -141,6 +156,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)