From 5bdc26e2d34cc87e4d4cea10356badce8c2b4024 Mon Sep 17 00:00:00 2001
From: Erik Reid <erik.reid@geant.org>
Date: Mon, 25 May 2020 20:39:22 +0200
Subject: [PATCH] use a ConnectionError instead of assertion for snmp i/o
 errors

---
 inventory_provider/snmp.py | 28 ++++++++++++++++++----------
 1 file changed, 18 insertions(+), 10 deletions(-)

diff --git a/inventory_provider/snmp.py b/inventory_provider/snmp.py
index ded726e7..7947cf35 100644
--- a/inventory_provider/snmp.py
+++ b/inventory_provider/snmp.py
@@ -10,6 +10,10 @@ from pysnmp.smi import builder, compiler
 RFC1213_MIB_IFDESC = '1.3.6.1.2.1.2.2.1.2'
 
 
+class SNMPWalkError(ConnectionError):
+    pass
+
+
 def _v6address_oid2str(dotted_decimal):
     hex_params = []
     for dec in re.split(r'\.', dotted_decimal):
@@ -61,15 +65,19 @@ def walk(agent_hostname, community, base_oid):  # pragma: no cover
 
         # cf. http://snmplabs.com/
         #       pysnmp/examples/hlapi/asyncore/sync/contents.html
-        assert not engineErrorIndication, (
-            f'snmp response engine error indication: '
-            f'{str(engineErrorIndication)} - {agent_hostname}')
-        assert not pduErrorIndication, 'snmp response pdu error %r at %r' % (
-            pduErrorIndication,
-            errorIndex and varBinds[int(errorIndex) - 1][0] or '?')
-        assert errorIndex == 0, (
-            'sanity failure: errorIndex != 0, '
-            'but no error indication')
+        if engineErrorIndication:
+            raise SNMPWalkError(
+                f'snmp response engine error indication: '
+                f'{str(engineErrorIndication)} - {agent_hostname}')
+        if pduErrorIndication:
+            raise SNMPWalkError(
+                'snmp response pdu error %r at %r' % (
+                    pduErrorIndication,
+                    errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
+        if errorIndex == 0:
+            raise SNMPWalkError(
+                'sanity failure: errorIndex != 0, '
+                'but no error indication')
 
         # varBinds = [
         #     rfc1902.ObjectType(rfc1902.ObjectIdentity(x[0]),x[1])
@@ -84,7 +92,7 @@ def walk(agent_hostname, community, base_oid):  # pragma: no cover
 def get_router_snmp_indexes(hostname, community):
     for ifc in walk(hostname, community, RFC1213_MIB_IFDESC):
         m = re.match(r'.*\.(\d+)$', ifc['oid'])
-        assert m, 'sanity failure parsing oid: %r' % ifc['oid']
+        assert m, f'sanity failure parsing oid: {ifc["oid"]}'
         yield {
             'name': ifc['value'],
             'index': int(m.group(1))
-- 
GitLab