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

pep8

parent cb899354
No related branches found
No related tags found
No related merge requests found
......@@ -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))
......@@ -2,7 +2,6 @@ import json
import os
import jsonschema
import pytest
from inventory_provider import snmp
......
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