diff --git a/inventory_provider/snmp.py b/inventory_provider/snmp.py index 3e7be3cd1e141928862b49d50b57583136d4f81b..05d10c2ab402ed0d60598f94a017903923a82a45 100644 --- a/inventory_provider/snmp.py +++ b/inventory_provider/snmp.py @@ -6,6 +6,7 @@ import struct from pysnmp.hlapi import nextCmd, SnmpEngine, CommunityData, \ UdpTransportTarget, ContextData, ObjectType, ObjectIdentity from pysnmp.smi import builder, compiler +from pysnmp.error import PySnmpError # from pysnmp.smi import view, rfc1902 @@ -84,46 +85,51 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover logger.debug("walking %s: %s" % (agent_hostname, base_oid)) - for (engineErrorIndication, - pduErrorIndication, - errorIndex, - varBinds) in nextCmd( - SnmpEngine(), - CommunityData(community), - UdpTransportTarget((agent_hostname, 161)), - ContextData(), - ObjectType(ObjectIdentity(base_oid)), - lexicographicMode=False, - lookupNames=True, - lookupValues=True): - - # cf. http://snmplabs.com/ - # pysnmp/examples/hlapi/asyncore/sync/contents.html - 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]) - # .resolveWithMib(mibViewController) - # for x in varBinds] - for oid, val in varBinds: - result = { - "oid": _canonify_oid(oid), - "value": _cast_snmp_value(val) - } - logger.debug(result) - yield result + try: + for (engineErrorIndication, + pduErrorIndication, + errorIndex, + varBinds) in nextCmd( + SnmpEngine(), + CommunityData(community), + UdpTransportTarget((agent_hostname, 161)), + ContextData(), + ObjectType(ObjectIdentity(base_oid)), + lexicographicMode=False, + lookupNames=True, + lookupValues=True): + + # cf. http://snmplabs.com/ + # pysnmp/examples/hlapi/asyncore/sync/contents.html + 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]) + # .resolveWithMib(mibViewController) + # for x in varBinds] + for oid, val in varBinds: + result = { + "oid": _canonify_oid(oid), + "value": _cast_snmp_value(val) + } + logger.debug(result) + yield result + + except PySnmpError as e: + raise SNMPWalkError( + f'snmp error communicating with {agent_hostname}: {e}') def get_router_snmp_indexes(hostname, community):