Comments (5)
Yes, I had to do exactly this. For exactly the same issue of formatting the sysObjectID node.
Here's what I came up with:
def oidToNode(oid):
if not oidToNode.nodes:
for mib_loaded in snimpy.manager.loaded:
for node in snimpy.mib.getNodes(mib_loaded):
oidToNode.nodes[node.oid] = node
oid2 = list(oid)
while oid2:
if tuple(oid2) in oidToNode.nodes:
return oidToNode.nodes[tuple(oid2)]
oid2.pop()
oidToNode.nodes = {}
Example:
>>> from snimpy.manager import load
>>> load("CISCO-PRODUCTS-MIB")
>>> n = oidToNode([1, 3, 6, 1, 4, 1, 9, 1, 1317])
>>> print(n)
cat3560cG8PC
>>> print(repr(n))
<Node CISCO-PRODUCTS-MIB::cat3560cG8PC from 'CISCO-PRODUCTS-MIB'>
>>> print(type(n))
<class 'snimpy.mib.Node'>
>>> n.oid
(1, 3, 6, 1, 4, 1, 9, 1, 1317)
>>>
Unfortunately, you may need to also abuse the repr function in the snimpy.mib.Node object to get the MIB name. If anyone knows a better way to do this, let us know so I can fix my scripts.
from snimpy.
Thank you. I was looking at doing it in an alternate way like you suggested, but with not being a python expert (or an amateur for that matter) I felt like there was something obvious I was missing.
Off the top of my head, I think there are several other Cisco MIBs that pull counters and interface stats and reference other OIDs in similar ways. Overall I really like this library and if there was a way to say "follow the OID" on certain queries that would be extremely useful.
from snimpy.
The only other place that I can think of where the returned data type points to another node is in ENTITY-MIB::entAliasMappingIdentifier. All of the counters should just be plain integer types.
Here's another little chunk of code that does something similar:
import re
def displayOid(oid):
oid = [int(x) for x in str(oid).split(".")]
n = snimpy.mib.getByOid(oid)
m = re.match('<\S+ (.*) from .*$', repr(n))
if m:
mibname = m.group(1)
else:
mibname = ''
pieces = 0 - (len(oid) - len(n.oid))
if pieces != 0:
remainder = oid[pieces:]
remainder = [str(x) for x in remainder]
else:
remainder = []
return ".".join([mibname] + remainder)
Example:
>>> from snimpy.manager import load
>>> load("CISCO-PRODUCTS-MIB")
>>> print(displayOid('1.3.6.1.4.1.9.1.1317'))
CISCO-PRODUCTS-MIB::cat3560cG8PC
$ snmpwalk --- ENTITY-MIB::entAliasMappingIdentifier
ENTITY-MIB::entAliasMappingIdentifier.1062.0 = OID: IF-MIB::ifIndex.8
ENTITY-MIB::entAliasMappingIdentifier.1063.0 = OID: IF-MIB::ifIndex.9
ENTITY-MIB::entAliasMappingIdentifier.1064.0 = OID: IF-MIB::ifIndex.10
ENTITY-MIB::entAliasMappingIdentifier.1065.0 = OID: IF-MIB::ifIndex.11
ENTITY-MIB::entAliasMappingIdentifier.1066.0 = OID: IF-MIB::ifIndex.12
ENTITY-MIB::entAliasMappingIdentifier.1067.0 = OID: IF-MIB::ifIndex.13
ENTITY-MIB::entAliasMappingIdentifier.1068.0 = OID: IF-MIB::ifIndex.14
$ snmptranslate -On IF-MIB::ifIndex.13
.1.3.6.1.2.1.2.2.1.1.13
$ python
>>> from snimpy.manager import load
>>> load("CISCO-PRODUCTS-MIB")
>>> load("IF-MIB")
>>> print(displayOid('1.3.6.1.2.1.2.2.1.1.13'))
IF-MIB::ifIndex.13
from snimpy.
You're right, I was thinking of the PoE Mib which references entPhysical instead of IfMib for it's interface naming and appends it to the end of the oid. That's a different, but equally annoying, situation.
from snimpy.
We can implement the high level part of this using format
(see https://www.python.org/dev/peps/pep-3101/#controlling-formatting-on-a-per-type-basis)
print("OID: {}, name: {:name}".format(oid, oid))
Is any of you interested in implementing this?
from snimpy.
Related Issues (20)
- PMPP support? HOT 4
- snimpy/manager.py broken in Python 3.7
- how do I get an OID from a MIB node ? HOT 5
- How to get KEEPALIVED-MIB::vrrpSyncGroupState HOT 8
- print(mgr.fgVpnTunEntPhase1Name.items()) produces "snimpy.snmp.SNMPNoSuchInstance: No such instance exists" HOT 3
- BGP4-V2-MIB-JUNIPER, take 2
- Use the native Python ipaddress types HOT 4
- SNMP Community Strings greater than 32 characters crash HOT 2
- Confusing example in documentation HOT 1
- Deprecation warning: snimpy not compatible with upcoming Python 3.10 HOT 2
- use unittest.mock instead of mock HOT 1
- Installation snimpy error on AlmaLinux 8.6 HOT 11
- Add how to add an new MIB path into the documentation HOT 1
- Migrate from unsupported pysnmp to pysnmp-lexstudio
- Publish a new release HOT 1
- testMultipleThreads failed HOT 2
- TestMain::test_loadfile gets stuck in 2038 HOT 4
- [HOW TO] walk over multiple node with manager?
- Occasional stucks with snimpy 1.0.2, working with 1.0.0 HOT 14
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from snimpy.