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
5eca4573
Commit
5eca4573
authored
4 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
don't use value.PrettyPrint()
parent
d7435460
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
inventory_provider/snmp.py
+57
-56
57 additions, 56 deletions
inventory_provider/snmp.py
with
57 additions
and
56 deletions
inventory_provider/snmp.py
+
57
−
56
View file @
5eca4573
...
...
@@ -6,7 +6,6 @@ import struct
from
pysnmp.hlapi
import
nextCmd
,
SnmpEngine
,
CommunityData
,
\
UdpTransportTarget
,
ContextData
,
ObjectType
,
ObjectIdentity
from
pysnmp.smi
import
builder
,
compiler
# from pysnmp.smi import view, rfc1902
...
...
@@ -15,6 +14,7 @@ RFC1213_MIB_IFDESC = '1.3.6.1.2.1.2.2.1.2'
JNX_BGP_M2_PEER_STATE
=
'
1.3.6.1.4.1.2636.5.1.1.2.1.1.1.2
'
logger
=
logging
.
getLogger
(
__name__
)
class
SNMPWalkError
(
ConnectionError
):
pass
...
...
@@ -107,7 +107,8 @@ def walk(agent_hostname, community, base_oid): # pragma: no cover
# .resolveWithMib(mibViewController)
# for x in varBinds]
for
oid
,
val
in
varBinds
:
result
=
{
"
oid
"
:
"
.
"
+
str
(
oid
),
"
value
"
:
val
.
prettyPrint
()}
result
=
{
"
oid
"
:
"
.
"
+
str
(
oid
),
"
value
"
:
_cast_snmp_value
(
val
)}
# result = {"oid": "." + str(oid), "value": val.prettyPrint()}
logger
.
debug
(
result
)
yield
result
...
...
@@ -159,60 +160,60 @@ 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
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
)]
def
get
(
router_hostname
:
str
,
community_string
:
str
,
oids
)
->
dict
:
"""
Sends SNMP get requests for each oid and returns the results in a dict.
: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
)
)
return
{
oid
:
val
for
oid
,
val
in
_fetch
(
handler
)}
##############
#
############################
#
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 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)]
#
#
#
def get(
#
router_hostname: str,
#
community_string: str,
#
oids) -> dict:
#
"""
#
Sends SNMP get requests for each oid and returns the results in a dict.
#
#
: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)
#
)
#
#
return {oid: val for oid, val in _fetch(handler)}
#
##############
if
__name__
==
'
__main__
'
:
...
...
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