comparison adslstats.py @ 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
comparison
equal deleted inserted replaced
-1:000000000000 0:98fe11ea4c82
1 #!/usr/bin/env python
2 ############################################################################
3 #
4 # Parse ADSL link stats for Billion 7300G & generate RRD archives & graphs
5 #
6 ############################################################################
7 #
8 # Copyright (C) 2007 Daniel O'Connor. All rights reserved.
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
12 # are met:
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
18 #
19 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
23 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 # SUCH DAMAGE.
30 #
31 ############################################################################
32
33 import optparse
34 import os
35 import re
36 import rrdtool
37 import time
38 import urllib
39 from BeautifulSoup import BeautifulSoup
40
41 usage = '''%prog [options]'''
42 opts = optparse.OptionParser(usage)
43 opts.add_option('-v', '--verbose', action="store_true", default=False,
44 help="Enable debug output")
45 opts.add_option('-g', '--graph', action="store_true", default=False,
46 help="Generate a graph")
47 opts.add_option('-u', '--update', action="store_true", default=False,
48 help="Update RRD")
49 opts.add_option('-a', '--authname', action="store", default="admin",
50 help="Username to login to modem")
51 opts.add_option('-p', '--password', action="store", default="admin",
52 help="Password to login to modem")
53 opts.add_option('-n', '--name', action="store", default="dsl",
54 help="Hostname of modem")
55 opts.add_option('-b', '--base', action="store", default="/home/darius/projects/adslstats/adslstats",
56 help="Base directory for RRD & PNGs")
57
58 (options, args) = opts.parse_args()
59
60 statsurl = "http://%s/adsl.asp" % (options.name)
61 rrdname = "%s.rrd" % (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
74 class ADSLStats(object):
75 def __str__(self):
76 return """Line Rate - Up: %d kbits, Down %d kbits
77 Noise Margin - Up: %.1f dB, Down %.1f dB
78 Attenuation - Up: %.1f dB, Down %.1f dB""" % (self.upstream, self.downstream,
79 self.nmup, self.nmdown,
80 self.attenup, self.attendown)
81
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):
90 s = BeautifulSoup(f)
91 a = s.findAll('tr')
92
93 for i in statsdict:
94 assert a[i].td.contents[0].contents[0] == statsdict[i]
95
96 stats = ADSLStats()
97
98 stats.upstream = cleannum(a[7].td.findNext('td').contents[0].contents[0]) # kbits
99 stats.downstream = cleannum(a[8].td.findNext('td').contents[0].contents[0]) # kbits
100 stats.nmup = cleannum(a[9].td.findNext('td').contents[0].contents[0]) # dB
101 stats.nmdown = cleannum(a[10].td.findNext('td').contents[0].contents[0]) # dB
102 stats.attenup = cleannum(a[11].td.findNext('td').contents[0].contents[0]) # dB
103 stats.attendown = cleannum(a[12].td.findNext('td').contents[0].contents[0]) # dB
104
105 return stats
106
107 # Setup RRD
108 # We expect data to be logged every 5 minutes
109 # Average 12 5 minute points -> hourly stats (keep 168 - a weeks worth)
110 # Average 288 5 minute points -> daily stats (keep 365 - a years worth)
111 # Detemine minimum & maximum for an hour and keep a weeks worth.
112 def makerrd(filename):
113 rrdtool.create(filename,
114 '--step', '300',
115 'DS:upstream:GAUGE:3600:32:25000', # Upstream (kbits) - 24mbit is ADSL2+ max
116 'DS:downstream:GAUGE:3600:32:25000', # Downstream (kbits)
117 'DS:nmup:GAUGE:3600:0:100', # Upstream Noise margin (dB)
118 'DS:nmdown:GAUGE:3600:0:100', # Downstream Noise margin (dB)
119 'DS:attenup:GAUGE:3600:0:100', # Upstream Attenuation (dB)
120 'DS:attendown:GAUGE:3600:0:100', # Downstream Attenuation (dB)
121 'RRA:AVERAGE:0.1:12:168',
122 'RRA:AVERAGE:0.1:288:365',
123 'RRA:MIN:0.1:12:168',
124 'RRA:MAX:0.1:12:168')
125
126 # Update the RRD (format stats as expected)
127 def updaterrd(filename, tstamp, stats):
128 rrdtool.update(filename,
129 '%d:%d:%d:%f:%f:%f:%f' % (tstamp,
130 stats.upstream,
131 stats.downstream,
132 stats.nmup,
133 stats.nmdown,
134 stats.attenup,
135 stats.attendown))
136
137 # Open the URL and call the parser, the update the RRD
138 def doupdate():
139 opener = urllib.FancyURLopener()
140 opener.prompt_user_passwd = lambda host, realm: (options.authname, options.password)
141 f = opener.open(statsurl)
142 #f = open("adsl.html")
143 stats = getstats(f)
144 if options.verbose:
145 print str(stats)
146 updaterrd(rrdname, int(time.time()), stats)
147
148 # Generate a graph
149 def gengraph():
150
151 linkargs = (
152 '-a', 'PNG',
153 '-X', '0',
154 '--vertical-label', 'kbit/sec',
155 '--slope-mode',
156
157 'DEF:upstream=%s:upstream:AVERAGE' % rrdname,
158 'DEF:upstreammin=%s:upstream:MIN' % rrdname,
159 'DEF:upstreammax=%s:upstream:MAX' % rrdname,
160
161 'DEF:downstream=%s:downstream:AVERAGE' % rrdname,
162 'DEF:downstreammin=%s:downstream:MIN' % rrdname,
163 'DEF:downstreammax=%s:downstream:MAX' % rrdname,
164
165 'CDEF:upstreamdif=upstreammax,upstreammin,-',
166 'CDEF:downstreamdif=downstreammax,downstreammin,-',
167
168 'LINE0:upstreammin#000000:',
169 'AREA:upstreamdif#00dc76::STACK',
170 'LINE1:upstream#00ff00:Upstream',
171
172 'LINE0:downstreammin#000000:',
173 'AREA:downstreamdif#ff8686::STACK',
174 'LINE1:downstream#ff0000:Downstream')
175
176 signalargs = (
177 '-a', 'PNG',
178 '--vertical-label', 'dB',
179 '--slope-mode',
180
181 'DEF:upstream=%s:upstream:AVERAGE' % rrdname,
182 'DEF:downstream=%s:downstream:AVERAGE' % rrdname,
183
184 'DEF:nmup_=%s:nmup:AVERAGE' % rrdname,
185 'DEF:nmupmin_=%s:nmup:MIN' % rrdname,
186 'DEF:nmupmax_=%s:nmup:MAX' % rrdname,
187
188 'DEF:nmdown_=%s:nmdown:AVERAGE' % rrdname,
189 'DEF:nmdownmin_=%s:nmdown:MIN' % rrdname,
190 'DEF:nmdownmax_=%s:nmdown:MAX' % rrdname,
191
192 'DEF:attenup=%s:attenup:AVERAGE' % rrdname,
193 'DEF:attenupmin=%s:attenup:MIN' % rrdname,
194 'DEF:attenupmax=%s:attenup:MAX' % rrdname,
195
196 'DEF:attendown=%s:attendown:AVERAGE' % rrdname,
197 'DEF:attendownmin=%s:attendown:MIN' % rrdname,
198 'DEF:attendownmax=%s:attendown:MAX' % rrdname,
199
200 'CDEF:nmup=nmup_,10,*',
201 'CDEF:nmupmin=nmupmin_,10,*',
202 'CDEF:nmupmax=nmupmax_,10,*',
203 'CDEF:nmupdif=nmupmax,nmupmin,-',
204
205 'CDEF:nmdown=nmdown_,10,*',
206 'CDEF:nmdownmin=nmdownmin_,10,*',
207 'CDEF:nmdownmax=nmdownmax_,10,*',
208 'CDEF:nmdowndif=nmdownmax,nmdownmin,-',
209
210 'CDEF:attenupdif=attenupmax,attenupmin,-',
211
212 'CDEF:attendowndif=attendownmax,attendownmin,-',
213
214 'LINE0:nmupmin#000000:',
215 'AREA:nmupdif#5c5cff::STACK',
216 'LINE1:nmup#0000ff:Noise Margin - Up (1/10 dB)',
217
218 'LINE0:nmdownmin#000000:',
219 'AREA:nmdowndif#009a00::STACK',
220 'LINE1:nmdown#00ff00:Noise Margin - Down (1/10 dB)',
221
222 'LINE0:attenupmin#000000:',
223 'AREA:attenupdif#f98100::STACK',
224 'LINE1:attenup#ff0000:Attenuation - Up',
225
226 'LINE0:attendownmin#000000:',
227 'AREA:attendowndif#aaaaaa::STACK',
228 'LINE1:attendown#000000:Attenuation - Down')
229
230 rrdtool.graph("%s-hour-link.png" % (graphbasename),
231 '--width', '1024',
232 '--height', '256',
233 '--start', 'end - 7d',
234 '--end', 'now',
235 *linkargs)
236
237 rrdtool.graph("%s-daily-link.png" % (graphbasename),
238 '--width', '1024',
239 '--height', '256',
240 '--start', 'end - 365d',
241 '--end', 'now',
242 *linkargs)
243
244
245 rrdtool.graph("%s-hour-signal.png" % (graphbasename),
246 '--width', '1024',
247 '--height', '256',
248 '--start', 'end - 7d',
249 '--end', 'now',
250 *signalargs)
251
252 rrdtool.graph("%s-daily-signal.png" % (graphbasename),
253 '--width', '1024',
254 '--height', '256',
255 '--start', 'end - 365d',
256 '--end', 'now',
257 *signalargs)
258
259
260 if __name__ == "__main__":
261 if options.update:
262 try:
263 os.stat(rrdname)
264 except OSError, e:
265 if e.errno == 2:
266 print "rrd not found, creating.."
267 makerrd(rrdname)
268
269 doupdate()
270
271 if options.graph:
272 gengraph()
273