comparison adslstats.py @ 2:b1048f889ef8

Newer Billion modem
author Daniel O'Connor <darius@dons.net.au>
date Wed, 20 Nov 2013 23:55:38 +1030
parents 98fe11ea4c82
children 3748cec0e322
comparison
equal deleted inserted replaced
0:98fe11ea4c82 2:b1048f889ef8
55 opts.add_option('-b', '--base', action="store", default="/home/darius/projects/adslstats/adslstats", 55 opts.add_option('-b', '--base', action="store", default="/home/darius/projects/adslstats/adslstats",
56 help="Base directory for RRD & PNGs") 56 help="Base directory for RRD & PNGs")
57 57
58 (options, args) = opts.parse_args() 58 (options, args) = opts.parse_args()
59 59
60 statsurl = "http://%s/adsl.asp" % (options.name) 60 statsurl = "http://%s/status/status_deviceinfo.htm" % (options.name)
61 rrdname = "%s.rrd" % (options.base) 61 rrdname = "%s.rrd" % (options.base)
62 graphbasename = options.base 62 graphbasename = options.base
63
64 matchnum = re.compile('([0-9]+(\.[0-9]+)?)')
65 statsdict = {
66 7 : 'Upstream',
67 8 : 'Downstream',
68 9 : 'Noise Margin (Upstream)',
69 10 : 'Noise Margin (Downstream)',
70 11 : 'Attenuation (Upstream)',
71 12 : 'Attenuation (Downstream)' }
72
73 63
74 class ADSLStats(object): 64 class ADSLStats(object):
75 def __str__(self): 65 def __str__(self):
76 return """Line Rate - Up: %d kbits, Down %d kbits 66 return """Line Rate - Up: %d kbits, Down %d kbits
77 Noise Margin - Up: %.1f dB, Down %.1f dB 67 Noise Margin - Up: %.1f dB, Down %.1f dB
78 Attenuation - Up: %.1f dB, Down %.1f dB""" % (self.upstream, self.downstream, 68 Attenuation - Up: %.1f dB, Down %.1f dB""" % (self.upstream, self.downstream,
79 self.nmup, self.nmdown, 69 self.nmup, self.nmdown,
80 self.attenup, self.attendown) 70 self.attenup, self.attendown)
81 71
82 def cleannum(s):
83 s1 = matchnum.match(s).groups()[0]
84 try:
85 return int(s1)
86 except ValueError:
87 return float(s1)
88
89 def getstats(f): 72 def getstats(f):
90 s = BeautifulSoup(f) 73 s = BeautifulSoup(f)
91 a = s.findAll('tr') 74 a = s.findAll('td')
92 75
93 for i in statsdict: 76 # Sanity check in case the firmware changes page layout
94 assert a[i].td.contents[0].contents[0] == statsdict[i] 77 assert(a[122]('font')[0].contents[0] == 'SNR Margin')
78 assert(a[129]('font')[0].contents[0] == 'Line Attenuation')
79 assert(a[136]('font')[0].contents[0] == 'Data Rate')
80
81 # Check if the modem is offline
82 if a[124].contents[0].strip() == 'N/A':
83 return None
95 84
96 stats = ADSLStats() 85 stats = ADSLStats()
97 86
98 stats.upstream = cleannum(a[7].td.findNext('td').contents[0].contents[0]) # kbits 87 stats.nmdown = float(a[124].contents[0]) # dB
99 stats.downstream = cleannum(a[8].td.findNext('td').contents[0].contents[0]) # kbits 88 stats.nmup = float(a[125].contents[0]) # dB
100 stats.nmup = cleannum(a[9].td.findNext('td').contents[0].contents[0]) # dB 89
101 stats.nmdown = cleannum(a[10].td.findNext('td').contents[0].contents[0]) # dB 90 stats.attendown = float(a[131].contents[0]) # dB
102 stats.attenup = cleannum(a[11].td.findNext('td').contents[0].contents[0]) # dB 91 stats.attenup = float(a[132].contents[0]) # dB
103 stats.attendown = cleannum(a[12].td.findNext('td').contents[0].contents[0]) # dB 92
93 stats.downstream = float(a[138].contents[0]) # kbit/sec
94 stats.upstream = float(a[139].contents[0]) # kbit/sec
104 95
105 return stats 96 return stats
106 97
107 # Setup RRD 98 # Setup RRD
108 # We expect data to be logged every 5 minutes 99 # We expect data to be logged every 5 minutes