diff --git a/inventory_provider/snmp.py b/inventory_provider/snmp.py index ded726e74bbe26357a6a3187e1b8f0b60ce1cd06..7947cf35382cff01595b33f5d42f5f54b6536e2f 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))