Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inventory-provider
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
geant-swd
dashboardv3
inventory-provider
Commits
64398e3d
Commit
64398e3d
authored
4 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
pep8
parent
cb899354
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
inventory_provider/snmp.py
+36
-90
36 additions, 90 deletions
inventory_provider/snmp.py
test/test_snmp_handling.py
+0
-1
0 additions, 1 deletion
test/test_snmp_handling.py
with
36 additions
and
91 deletions
inventory_provider/snmp.py
+
36
−
90
View file @
64398e3d
...
...
@@ -21,7 +21,7 @@ class SNMPWalkError(ConnectionError):
def
_cast_snmp_value
(
value
):
"""
Cast
str
ings to the simplest native type.
Cast
th
ings 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))
This diff is collapsed.
Click to expand it.
test/test_snmp_handling.py
+
0
−
1
View file @
64398e3d
...
...
@@ -2,7 +2,6 @@ import json
import
os
import
jsonschema
import
pytest
from
inventory_provider
import
snmp
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment