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
d7435460
Commit
d7435460
authored
4 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
don't return current value with peer oid info
parent
472a54e8
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
+46
-37
46 additions, 37 deletions
inventory_provider/snmp.py
with
46 additions
and
37 deletions
inventory_provider/snmp.py
+
46
−
37
View file @
d7435460
...
...
@@ -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
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