Skip to content
Snippets Groups Projects
Commit 90aaa554 authored by Erik Reid's avatar Erik Reid
Browse files

catch snmp library error

parent f08f6de7
No related branches found
No related tags found
No related merge requests found
......@@ -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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment