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

don't use value.PrettyPrint()

parent d7435460
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,6 @@ import struct
from pysnmp.hlapi import nextCmd, SnmpEngine, CommunityData, \
UdpTransportTarget, ContextData, ObjectType, ObjectIdentity
from pysnmp.smi import builder, compiler
# from pysnmp.smi import view, rfc1902
......@@ -15,6 +14,7 @@ RFC1213_MIB_IFDESC = '1.3.6.1.2.1.2.2.1.2'
JNX_BGP_M2_PEER_STATE = '1.3.6.1.4.1.2636.5.1.1.2.1.1.1.2'
logger = logging.getLogger(__name__)
class SNMPWalkError(ConnectionError):
pass
......@@ -107,7 +107,8 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover
# .resolveWithMib(mibViewController)
# for x in varBinds]
for oid, val in varBinds:
result = {"oid": "." + str(oid), "value": val.prettyPrint()}
result = {"oid": "." + str(oid), "value": _cast_snmp_value(val)}
# result = {"oid": "." + str(oid), "value": val.prettyPrint()}
logger.debug(result)
yield result
......@@ -159,60 +160,60 @@ def get_peer_state_info(hostname, community):
'oid': ifc['oid']
}
############################
from pysnmp.hlapi import getCmd
def _construct_object_types(oids):
return [ObjectType(ObjectIdentity(oid)) for oid in oids]
def _fetch(handler):
"""
yields (oid, value) from the response handler
:param handler:
:return: a dict like {oid: value}, or None if there's any error
"""
for (error_indication, error_status, error_index, var_binds) in handler:
if error_indication or error_status:
# raise RuntimeError(
# 'Got SNMP error: {0}'.format(error_indication))
logger.error(f'SNMP error: {error_indication}')
return
for oid, value in var_binds:
oid = str(oid)
if not oid.startswith('.'):
oid = f'.{oid}'
yield [oid, _cast_snmp_value(value)]
def get(
router_hostname: str,
community_string: str,
oids) -> dict:
"""
Sends SNMP get requests for each oid and returns the results in a dict.
:param router_hostname:
:param community_string:
:param oids:
:return: a dict like {oid: value}, or None if there's any error
"""
handler = getCmd(
SnmpEngine(),
CommunityData(community_string),
UdpTransportTarget((router_hostname, 161)),
ContextData(),
*_construct_object_types(oids)
)
return {oid: val for oid, val in _fetch(handler)}
##############
# ############################
# from pysnmp.hlapi import getCmd
#
# def _construct_object_types(oids):
# return [ObjectType(ObjectIdentity(oid)) for oid in oids]
#
#
#
#
# def _fetch(handler):
# """
# yields (oid, value) from the response handler
#
# :param handler:
# :return: a dict like {oid: value}, or None if there's any error
# """
#
# for (error_indication, error_status, error_index, var_binds) in handler:
#
# if error_indication or error_status:
# # raise RuntimeError(
# # 'Got SNMP error: {0}'.format(error_indication))
# logger.error(f'SNMP error: {error_indication}')
# return
#
# for oid, value in var_binds:
# oid = str(oid)
# if not oid.startswith('.'):
# oid = f'.{oid}'
# yield [oid, _cast_snmp_value(value)]
#
#
# def get(
# router_hostname: str,
# community_string: str,
# oids) -> dict:
# """
# Sends SNMP get requests for each oid and returns the results in a dict.
#
# :param router_hostname:
# :param community_string:
# :param oids:
# :return: a dict like {oid: value}, or None if there's any error
# """
# handler = getCmd(
# SnmpEngine(),
# CommunityData(community_string),
# UdpTransportTarget((router_hostname, 161)),
# ContextData(),
# *_construct_object_types(oids)
# )
#
# return {oid: val for oid, val in _fetch(handler)}
# ##############
if __name__ == '__main__':
......
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