Giter VIP home page Giter VIP logo

Comments (5)

johnworkman avatar johnworkman commented on June 5, 2024

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.

swannie2 avatar swannie2 commented on June 5, 2024

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.

johnworkman avatar johnworkman commented on June 5, 2024

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.

swannie2 avatar swannie2 commented on June 5, 2024

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.

vincentbernat avatar vincentbernat commented on June 5, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.