# HG changeset patch # User Daniel O'Connor # Date 1605916902 -37800 # Node ID 4f9a79f733ffb41a2e27ebca564c33b052485152 # Parent 5dbc310f1eca2b34379cc2adf9e6829ffc76660b Don't explode when the link is down. diff -r 5dbc310f1eca -r 4f9a79f733ff adslstats.py --- a/adslstats.py Tue Nov 17 15:47:56 2020 +1030 +++ b/adslstats.py Sat Nov 21 10:31:42 2020 +1030 @@ -106,6 +106,9 @@ class DSLStats(object): def __str__(self): + if self.upstream == None: + return 'Disconnected' + s = '''Line Rate - Up: %d kbits, Down %d kbits Maximum Rate - Up: %d kbit, Down %s kbit Noise Margin - Up: %.1f dB, Down %.1f dB @@ -177,11 +180,15 @@ # Helper function to extract data def getvals(bs, text, mult = 1): - subs = bs.findAll('label', text = text)[0].fetchNextSiblings()[0].strings + subs = bs.findAll('label', text = text) + if len(subs) == 0: + return None, None + subs = subs[0].fetchNextSiblings()[0].strings tmp = [float(s.split()[0]) for s in subs] return [s * mult for s in tmp] - if list(bs.findAll('label', text = 'DSL Status')[0].fetchNextSiblings()[0].strings)[0] == 'Up': + tmp = bs.findAll('label', text = 'DSL Status') + if len(tmp) > 0 and list(tmp[0].fetchNextSiblings()[0].strings)[0] == 'Up': stats.linkup = True else: stats.linkup = False @@ -192,20 +199,28 @@ stats.nmup, stats.nmdown = getvals(bs, 'Noise Margin') # Line attenuation returns several values for each direction, parse specially and just take the first one - upattens, downattens = list(bs.findAll('label', text = 'Line Attenuation')[0].fetchNextSiblings()[0].strings) - stats.attenup = float(re.findall('([0-9.N/A]+)', upattens)[0]) - stats.attendown = float(re.findall('([0-9.N/A]+)', downattens)[0]) + tmp = bs.findAll('label', text = 'Line Attenuation') + if len(tmp) == 0: + stats.attenup, stats.attendown = None, None + else: + upattens, downattens = list(tmp[0].fetchNextSiblings()[0].strings) + stats.attenup = float(re.findall('([0-9.N/A]+)', upattens)[0]) + stats.attendown = float(re.findall('([0-9.N/A]+)', downattens)[0]) # Convert something like '2days 17hours 28min 19sec' into seconds - uptime = re.findall('([0-9]+)', list(bs.findAll('label', text = 'DSL Uptime')[0].fetchNextSiblings()[0].strings)[0]) - uptime.reverse() # End up with an array of seconds, minutes, hours, etc - mults = [1, 60, 60 * 60, 24 * 60 * 60] - # Basic sanity check of the number of elements, should be at least 1 - if len(uptime) == 0 or len(uptime) > len(mults): - print('Unexpected number of uptime elements (%s)' % str(uptime)) + tmp = bs.findAll('label', text = 'DSL Uptime') + if len(tmp) == 0: stats.uptime = None else: - stats.uptime = reduce(lambda a, b: a + b, [int(a[0]) * a[1] for a in zip(uptime, mults)]) + uptime = re.findall('([0-9]+)', list(tmp[0].fetchNextSiblings()[0].strings)[0]) + uptime.reverse() # End up with an array of seconds, minutes, hours, etc + mults = [1, 60, 60 * 60, 24 * 60 * 60] + # Basic sanity check of the number of elements, should be at least 1 + if len(uptime) == 0 or len(uptime) > len(mults): + print('Unexpected number of uptime elements (%s)' % str(uptime)) + stats.uptime = None + else: + stats.uptime = reduce(lambda a, b: a + b, [int(a[0]) * a[1] for a in zip(uptime, mults)]) return True @@ -237,6 +252,8 @@ # Update the RRD (format stats as expected) def updaterrd(filename, tstamp, stats): + if stats.uptime == None: + return rrdtool.update(filename, '%d:%d:%d:%d:%d:%f:%f:%f:%f:U:U:%f:%f:%d' % ( tstamp,