Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MiB Translation Fail with resolveWithMib #62

Closed
sbhattach opened this issue Jun 21, 2017 · 9 comments
Closed

MiB Translation Fail with resolveWithMib #62

sbhattach opened this issue Jun 21, 2017 · 9 comments

Comments

@sbhattach
Copy link

sbhattach commented Jun 21, 2017

Hi,

I am performing a walk(Refer walk_out.txt
walk_out.txt
) and then trying to translate the O/P using some thing like this:

        custom_mib_paths = "/data/users/MIBS/,/usr/share/snmp/mibs/"
        load_mib_modules = "SNMPv2-SMI,<CUSTOM-MIB-1>,<CUSTOM-MIB-2>"
        try:
            mibBuilder = builder.MibBuilder()
            compiler.addMibCompiler(mibBuilder, sources=custom_mib_paths)
            mibViewController = view.MibViewController(mibBuilder)
            for mibs in load_mib_modules.split(','):
                mibBuilder.loadModules(mibs)
        except error.MibNotFoundError:
            testcase_Utils.pNote("Mib Not Found!", "Error")
        output = rfc1902.ObjectType(rfc1902.ObjectIdentity(name), val).resolveWithMib(mibViewController).prettyPrint()

And getting this bellow error:

    output = rfc1902.ObjectType(rfc1902.ObjectIdentity(name), val).resolveWithMib(mibViewController).prettyPrint()
  File "/home/VIRT/lib/python2.7/site-packages/pysnmp/smi/rfc1902.py", line 853, in resolveWithMib
    raise SmiError('MIB object %r is not OBJECT-TYPE (MIB not loaded?)' % (self.__args[0],))
SmiError: MIB object ObjectIdentity(ObjectIdentity(ObjectName('1.3.6.1.2.1.1.2.0'))) is not OBJECT-TYPE (MIB not loaded?)

Now without MIB Translation code (with prettyPrint ) its provides o/p something like this:

-I- SNMPv2-MIB::sysDescr.0 = Tseries
-I- SNMPv2-MIB::sysObjectID.0 = SNMPv2-SMI::enterprises.211.24.12
-I- SNMPv2-MIB::sysUpTime.0 = 677417

Now What could be the problem here with this Translation. Is it the custom MIB file which I am using is creatiing any problem ?
But '1.3.6.1.2.1.1.2.0' is a part of SNMPv2-SMI.

@sbhattach
Copy link
Author

pip_list.txt
PIP List added.

@etingof
Copy link
Owner

etingof commented Jun 24, 2017

The 1.3.6.1.2.1.1.2.0 OID is in fact part of SNMPv2-MIB (sysObjectID.0), do you have it loaded as well?

The other possibility is that you redefine 1.3.6.1.2.1.1.2.0 OID somewhere in your custom MIBs and that OID has a non-OBJECT-TYPE MIB type.

If you enable pysnmp debugging, the whole MIB lookup process should be reported. That might help figuring out the root cause.

@sbhattach
Copy link
Author

I attached the Debug log
debug_log.txt

@etingof
Copy link
Owner

etingof commented Jun 27, 2017

It's hard to tell the exact problem without seeing the whole program and relate it to the debug you provided...

Could you please add SNMPv2-MIB to the load_mib_modules list (you can drop SNMPv2-SMI as it's a dependency):

        load_mib_modules = "SNMPv2-MIB, <CUSTOM-MIB-1>,<CUSTOM-MIB-2>"

...and re-run your case?

Also, I am not sure how your code is structured, but if you run more than one SNMP query per process invocation, it makes sense (from performance standpoint) to run MIB loading pieces just once.

@sbhattach
Copy link
Author

sbhattach commented Jun 27, 2017

Hi @etingof with SNMPv2-MIB insted of SNMPv2-SMI its working fine.

The structure of code is something like this:

from pysnmp.entity.rfc3413.oneliner import cmdgen
cmdGen = cmdgen.CommandGenerator()

oid = (1,3,6,1)
errindication, errstatus, errindex,\
varBindTable = cmdgen.nextCmd(auth_data,
                                    transport,
                                    oid, lexicographicMode=True,
                                    ignoreNonIncreasingOid=True, maxRows=5000,
                                    lookupNames=True, lookupValues=True)

def translate_mib(custom_mib_paths, load_mib_modules, name, val):
    if custom_mib_paths and load_mib_modules:
        try:
            mibBuilder = builder.MibBuilder()
            compiler.addMibCompiler(mibBuilder, sources=custom_mib_paths)
            mibViewController = view.MibViewController(mibBuilder)
            for mibs in load_mib_modules.split(','):
                mibBuilder.loadModules(mibs)
        except error.MibNotFoundError:
            print("Mib Not Found!", "Error")
    if custom_mib_paths and load_mib_modules:
        output = rfc1902.ObjectType(rfc1902.ObjectIdentity(name), val).resolveWithMib(mibViewController).prettyPrint()
        op_list = output.split(" = ")
        name = op_list[0].strip()
        val = op_list[1].strip()
        print('%s = %s' %
                         (name,
                          val))
        return name, val
    else:
        print('%s = %s' %
                         (name.prettyPrint(),
                          val.prettyPrint()))
        return name.prettyPrint(), val.prettyPrint()

custom_mib_paths = "/data/users/MIBS/,/usr/share/snmp/mibs/"
load_mib_modules = "SNMPv2-SMI,<CUSTOM-MIB-1>,<CUSTOM-MIB-2>"

for varBindTableRow in varBindTable:
    for name, val in varBindTableRow:
        translate_mib(custom_mib_paths, load_mib_modules, name, val)

@etingof
Copy link
Owner

etingof commented Jun 27, 2017

So does that solve you problem or not quite?

I'm not sure why you need the translate_mib() function at all? It's all seem to be implemented inside pysnmp already. Could you just use ObjectIdentity.addAsn1MibSource and ObjectIdentity.loadMibs instead?

If you still need that custom MIB resolver, I'd strongly recommend making the MibBuilder and MibViewController objects initilaized just once rather than inside a loop, as it's currently is.

@sbhattach
Copy link
Author

@etingof This solve the problem.
But in my code I have tried the same addAsn1MibSource and .loadMibs when user is providing MIB string(not OID) as a input. which works fine.

But when its with oid for some reason its failing.

>>> ObjectIdentity('1.3.6.1')
ObjectIdentity('1.3.6.1')
>>> ObjectIdentity('1.3.6.1').addAsn1Source('/data/users/sbhattac/MIBS/,/usr/share/snmp/mibs/'.split(','))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sbhattac/VIRT/lib/python2.7/site-packages/pysnmp/smi/rfc1902.py", line 634, in __getattr__
    raise SmiError('%s object not properly initialized for accessing %s' % (self.__class__.__name__, attr))
pysnmp.smi.error.SmiError: ObjectIdentity object not properly initialized for accessing addAsn1Source
>>>
>>> ObjectIdentity('SNMPv2-MIB', 'sysDescr').addAsn1Source('http://mibs.snmplabs.com/asn1/@mib@')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/sbhattac/VIRT/lib/python2.7/site-packages/pysnmp/smi/rfc1902.py", line 634, in __getattr__
    raise SmiError('%s object not properly initialized for accessing %s' % (self.__class__.__name__, attr))
pysnmp.smi.error.SmiError: ObjectIdentity object not properly initialized for accessing addAsn1Source
>>>

Can you please confirm its behavior.

@etingof
Copy link
Owner

etingof commented Jun 27, 2017

That's because you are using the wrong method. It should be .addAsn1MibSource, not .addAsn1Source.

@sbhattach
Copy link
Author

@etingof with addAsn1MibSource its working fine please close it.
Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants