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

pep8

parent cb899354
Branches
Tags
No related merge requests found
...@@ -21,7 +21,7 @@ class SNMPWalkError(ConnectionError): ...@@ -21,7 +21,7 @@ class SNMPWalkError(ConnectionError):
def _cast_snmp_value(value): def _cast_snmp_value(value):
""" """
Cast strings to the simplest native type. Cast things to the simplest native type.
:param value: :param value:
:return: :return:
...@@ -88,14 +88,14 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover ...@@ -88,14 +88,14 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover
pduErrorIndication, pduErrorIndication,
errorIndex, errorIndex,
varBinds) in nextCmd( varBinds) in nextCmd(
SnmpEngine(), SnmpEngine(),
CommunityData(community), CommunityData(community),
UdpTransportTarget((agent_hostname, 161)), UdpTransportTarget((agent_hostname, 161)),
ContextData(), ContextData(),
ObjectType(ObjectIdentity(base_oid)), ObjectType(ObjectIdentity(base_oid)),
lexicographicMode=False, lexicographicMode=False,
lookupNames=True, lookupNames=True,
lookupValues=True): lookupValues=True):
# cf. http://snmplabs.com/ # cf. http://snmplabs.com/
# pysnmp/examples/hlapi/asyncore/sync/contents.html # pysnmp/examples/hlapi/asyncore/sync/contents.html
...@@ -136,14 +136,14 @@ def get_router_snmp_indexes(hostname, community): ...@@ -136,14 +136,14 @@ def get_router_snmp_indexes(hostname, community):
} }
def _v6bytes(l): def _v6bytes(int_str_list):
assert len(l) == 16 assert len(int_str_list) == 16
return struct.pack('!16B', *map(int, l)) return struct.pack('!16B', *map(int, int_str_list))
def _v4str(l): def _v4str(int_str_list):
assert len(l) == 4 assert len(int_str_list) == 4
return '.'.join(l) return '.'.join(int_str_list)
def get_peer_state_info(hostname, community): def get_peer_state_info(hostname, community):
...@@ -175,84 +175,30 @@ def get_peer_state_info(hostname, community): ...@@ -175,84 +175,30 @@ def get_peer_state_info(hostname, community):
'oid': ifc['oid'] 'oid': ifc['oid']
} }
# ############################
# from pysnmp.hlapi import getCmd # if __name__ == '__main__':
#
# 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: # # HOSTNAME = 'mx1.ams.nl.geant.net'
# oid = str(oid) # HOSTNAME = 'mx1.kau.lt.geant.net'
# if not oid.startswith('.'): # COMMUNITY = '0pBiFbD'
# oid = f'.{oid}' # import json
# yield [oid, _cast_snmp_value(value)]
# #
# # for x in get_peer_state_info('mx1.kau.lt.geant.net', '0pBiFbD'):
# # print(x)
# #
# def get( # peerings = get_peer_state_info(HOSTNAME, COMMUNITY)
# router_hostname: str, # print(json.dumps(list(peerings), indent=2, sort_keys=True))
# community_string: str,
# oids) -> dict:
# """
# Sends SNMP get requests for each oid and returns the results in a dict.
# #
# :param router_hostname: # # oids = [x['oid'] for x in peerings]
# :param community_string: # # print(oids)
# :param oids: # # data = dict()
# :return: a dict like {oid: value}, or None if there's any error # # for i in range(0, len(oids), 3):
# """ # # data.update(get(HOSTNAME, COMMUNITY, oids[i:i+3]))
# handler = getCmd( # #
# SnmpEngine(), # # assert all([v for v in data.values()])
# CommunityData(community_string), # # print(json.dumps(data, indent=2))
# UdpTransportTarget((router_hostname, 161)),
# ContextData(),
# *_construct_object_types(oids)
# )
# #
# return {oid: val for oid, val in _fetch(handler)} # # import json
# ############## # # z = get_router_snmp_indexes('mx1.kau.lt.geant.net', '0pBiFbD')
# # print(json.dumps(list(z), indent=2))
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
...@@ -2,7 +2,6 @@ import json ...@@ -2,7 +2,6 @@ import json
import os import os
import jsonschema import jsonschema
import pytest
from inventory_provider import snmp from inventory_provider import snmp
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment