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

don't return current value with peer oid info

parent 472a54e8
No related branches found
No related tags found
No related merge requests found
......@@ -19,6 +19,26 @@ class SNMPWalkError(ConnectionError):
pass
def _cast_snmp_value(value):
"""
Cast strings to the simplest native type.
:param value:
:return:
"""
try:
return int(value)
except (ValueError, TypeError):
try:
return float(value)
except (ValueError, TypeError):
try:
return str(value)
except (ValueError, TypeError):
pass
return value
def _v6address_oid2str(dotted_decimal):
hex_params = []
for dec in re.split(r'\.', dotted_decimal):
......@@ -130,13 +150,13 @@ def get_peer_state_info(hostname, community):
local = ipaddress.ip_address(_v6bytes(splits[1:17]))
remote = ipaddress.ip_address(_v6bytes(splits[18:]))
else:
logger.error()
logger.error(f'expected v4 or v6 peering, got type {splits[0]}')
assert False
yield {
'local': local.exploded,
'remote': remote.exploded,
'oid': ifc['oid'],
'value': ifc['value'],
'oid': ifc['oid']
}
############################
......@@ -146,29 +166,6 @@ def _construct_object_types(oids):
return [ObjectType(ObjectIdentity(oid)) for oid in oids]
def _cast_snmp_value(value):
"""
Boilerplate method for explicitly casting returned snmp value types.
Copied from the tutorial - negligible performance
impact since in our case we'll always return in the
first condition. This method is in fact totally useless
and should be removed.
:param value:
:return:
"""
try:
return int(value)
except (ValueError, TypeError):
try:
return float(value)
except (ValueError, TypeError):
try:
return str(value)
except (ValueError, TypeError):
pass
return value
def _fetch(handler):
......@@ -188,7 +185,10 @@ def _fetch(handler):
return
for oid, value in var_binds:
yield [str(oid), _cast_snmp_value(value)]
oid = str(oid)
if not oid.startswith('.'):
oid = f'.{oid}'
yield [oid, _cast_snmp_value(value)]
def get(
......@@ -216,18 +216,27 @@ def get(
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('mx1.kau.lt.geant.net', '0pBiFbD')
# 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('mx1.kau.lt.geant.net', '0pBiFbD', oids[i:i+3]))
print(json.dumps(data, indent=2))
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
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