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

use a ConnectionError instead of assertion for snmp i/o errors

parent 5f15bf0a
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,10 @@ from pysnmp.smi import builder, compiler ...@@ -10,6 +10,10 @@ from pysnmp.smi import builder, compiler
RFC1213_MIB_IFDESC = '1.3.6.1.2.1.2.2.1.2' RFC1213_MIB_IFDESC = '1.3.6.1.2.1.2.2.1.2'
class SNMPWalkError(ConnectionError):
pass
def _v6address_oid2str(dotted_decimal): def _v6address_oid2str(dotted_decimal):
hex_params = [] hex_params = []
for dec in re.split(r'\.', dotted_decimal): for dec in re.split(r'\.', dotted_decimal):
...@@ -61,15 +65,19 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover ...@@ -61,15 +65,19 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover
# cf. http://snmplabs.com/ # cf. http://snmplabs.com/
# pysnmp/examples/hlapi/asyncore/sync/contents.html # pysnmp/examples/hlapi/asyncore/sync/contents.html
assert not engineErrorIndication, ( if engineErrorIndication:
f'snmp response engine error indication: ' raise SNMPWalkError(
f'{str(engineErrorIndication)} - {agent_hostname}') f'snmp response engine error indication: '
assert not pduErrorIndication, 'snmp response pdu error %r at %r' % ( f'{str(engineErrorIndication)} - {agent_hostname}')
pduErrorIndication, if pduErrorIndication:
errorIndex and varBinds[int(errorIndex) - 1][0] or '?') raise SNMPWalkError(
assert errorIndex == 0, ( 'snmp response pdu error %r at %r' % (
'sanity failure: errorIndex != 0, ' pduErrorIndication,
'but no error indication') errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
if errorIndex == 0:
raise SNMPWalkError(
'sanity failure: errorIndex != 0, '
'but no error indication')
# varBinds = [ # varBinds = [
# rfc1902.ObjectType(rfc1902.ObjectIdentity(x[0]),x[1]) # rfc1902.ObjectType(rfc1902.ObjectIdentity(x[0]),x[1])
...@@ -84,7 +92,7 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover ...@@ -84,7 +92,7 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover
def get_router_snmp_indexes(hostname, community): def get_router_snmp_indexes(hostname, community):
for ifc in walk(hostname, community, RFC1213_MIB_IFDESC): for ifc in walk(hostname, community, RFC1213_MIB_IFDESC):
m = re.match(r'.*\.(\d+)$', ifc['oid']) 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 { yield {
'name': ifc['value'], 'name': ifc['value'],
'index': int(m.group(1)) 'index': int(m.group(1))
......
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