changeset 37:4f9a79f733ff

Don't explode when the link is down.
author Daniel O'Connor <darius@dons.net.au>
date Sat, 21 Nov 2020 10:31:42 +1030
parents 5dbc310f1eca
children 6f85bedf9966
files adslstats.py
diffstat 1 files changed, 29 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- 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,