diff --git a/inventory_provider/snmp.py b/inventory_provider/snmp.py index c204d2ec681caec930cb0c7e735fb97f7b960f29..18b1130049ec0059cc57dc1fdd579d8a578e73a3 100644 --- a/inventory_provider/snmp.py +++ b/inventory_provider/snmp.py @@ -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__':