diff --git a/inventory_provider/snmp.py b/inventory_provider/snmp.py index 17238cc9f5b03b505be0eb3e46559ab4cec12677..3e7be3cd1e141928862b49d50b57583136d4f81b 100644 --- a/inventory_provider/snmp.py +++ b/inventory_provider/snmp.py @@ -21,7 +21,7 @@ class SNMPWalkError(ConnectionError): def _cast_snmp_value(value): """ - Cast strings to the simplest native type. + Cast things to the simplest native type. :param value: :return: @@ -88,14 +88,14 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover pduErrorIndication, errorIndex, varBinds) in nextCmd( - SnmpEngine(), - CommunityData(community), - UdpTransportTarget((agent_hostname, 161)), - ContextData(), - ObjectType(ObjectIdentity(base_oid)), - lexicographicMode=False, - lookupNames=True, - lookupValues=True): + 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 @@ -136,14 +136,14 @@ def get_router_snmp_indexes(hostname, community): } -def _v6bytes(l): - assert len(l) == 16 - return struct.pack('!16B', *map(int, l)) +def _v6bytes(int_str_list): + assert len(int_str_list) == 16 + return struct.pack('!16B', *map(int, int_str_list)) -def _v4str(l): - assert len(l) == 4 - return '.'.join(l) +def _v4str(int_str_list): + assert len(int_str_list) == 4 + return '.'.join(int_str_list) def get_peer_state_info(hostname, community): @@ -175,84 +175,30 @@ 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 __name__ == '__main__': # -# 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)] +# # HOSTNAME = 'mx1.ams.nl.geant.net' +# HOSTNAME = 'mx1.kau.lt.geant.net' +# COMMUNITY = '0pBiFbD' +# import json # +# # for x in get_peer_state_info('mx1.kau.lt.geant.net', '0pBiFbD'): +# # print(x) # -# def get( -# router_hostname: str, -# community_string: str, -# oids) -> dict: -# """ -# Sends SNMP get requests for each oid and returns the results in a dict. +# peerings = get_peer_state_info(HOSTNAME, COMMUNITY) +# print(json.dumps(list(peerings), indent=2, sort_keys=True)) # -# :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) -# ) +# # oids = [x['oid'] for x in peerings] +# # print(oids) +# # data = dict() +# # for i in range(0, len(oids), 3): +# # data.update(get(HOSTNAME, COMMUNITY, oids[i:i+3])) +# # +# # assert all([v for v in data.values()]) +# # print(json.dumps(data, indent=2)) # -# return {oid: val for oid, val in _fetch(handler)} -# ############## - -if __name__ == '__main__': - - - # HOSTNAME = 'mx1.ams.nl.geant.net' - HOSTNAME = 'mx1.kau.lt.geant.net' - COMMUNITY = '0pBiFbD' - import json - - # for x in get_peer_state_info('mx1.kau.lt.geant.net', '0pBiFbD'): - # print(x) - - peerings = get_peer_state_info(HOSTNAME, COMMUNITY) - print(json.dumps(list(peerings), indent=2, sort_keys=True)) - - # oids = [x['oid'] for x in peerings] - # print(oids) - # data = dict() - # for i in range(0, len(oids), 3): - # data.update(get(HOSTNAME, COMMUNITY, oids[i:i+3])) - # - # assert all([v for v in data.values()]) - # print(json.dumps(data, indent=2)) - - # import json - # z = get_router_snmp_indexes('mx1.kau.lt.geant.net', '0pBiFbD') - # print(json.dumps(list(z), indent=2)) \ No newline at end of file +# # import json +# # z = get_router_snmp_indexes('mx1.kau.lt.geant.net', '0pBiFbD') +# # print(json.dumps(list(z), indent=2)) diff --git a/test/test_snmp_handling.py b/test/test_snmp_handling.py index ac65ec42b0aa778beb68f4bff4cdd42dc3ac6219..38ecb7c9088d4a413ffe54039a91db8828b4c95d 100644 --- a/test/test_snmp_handling.py +++ b/test/test_snmp_handling.py @@ -2,7 +2,6 @@ import json import os import jsonschema -import pytest from inventory_provider import snmp