etingof / pysmi

SNMP MIB parser
http://snmplabs.com/pysmi/
BSD 2-Clause "Simplified" License
137 stars 52 forks source link

MibBuilder is not able to load BITS data structure, gives error #35

Open baid86 opened 6 years ago

baid86 commented 6 years ago

This is the MIB entry where I am getting error hm2DHCPServerPoolFlags = MibTableColumn((1, 3, 6, 1, 4, 1, 248, 11, 91, 1, 1, 1, 5, 1, 5), Bits().clone(namedValues=NamedValues(("interface", 0), ("mac", 1), ("gateway", 2), ("clientid", 3), ("remoteid", 4), ("circuitid", 5), ("dynamic", 6), ("vlanid", 7)))).setMaxAccess("readcreate")

The error which I am getting is

hm2DHCPServerPoolFlags = MibTableColumn((1, 3, 6, 1, 4, 1, 248, 11, 91, 1, 1, 1, 5, 1, 5), Bits().clone(namedValues=NamedValues(("interface", 0), ("mac", 1), ("gateway", 2), ("clientid", 3), ("remoteid", 4), ("circuitid", 5), ("dynamic", 6), ("vlanid", 7)))).setMaxAccess("readcreate")
  File "C:\Python27\lib\site-packages\pysnmp\proto\rfc1902.py", line 605, in __init__
    OctetString.__init__(self, value, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'namedValues'

On some debugging I found that in rfc 1902.py for Bits class the init is like

    def __init__(self, value=univ.noValue, **kwargs):
        if 'namedValues' not in kwargs:
            kwargs['namedValues'] = self.namedValues

        OctetString.__init__(self, value, **kwargs)

So its calling octet string constructer which would be basically init of univ.OctetString which does not have namedValue as keyword args, hence the error

 def __init__(self, value=noValue, tagSet=None, subtypeSpec=None,
                 encoding=None, binValue=noValue, hexValue=noValue):
etingof commented 6 years ago

Oh, that seems to be a bug in pysnmp. Try the HEAD of this branch.

etingof commented 6 years ago

...on the second thought, may be it's actually not so much a bug - may be it's just that you use incompatible version of pyasn1. Could you please try to run original pysnmp with the pyasn1 version matching its requirements.txt?

baid86 commented 6 years ago

Using pyasn1 0.2.3 did not helped. Using 4.4.5 solved that issue but then its starts failing at different point. Basically mibs like these are not getting handled properly

hm2PoeMgmtPortAllowedClassBits  OBJECT-TYPE
                        SYNTAX      BITS {
                            class0(0),
                            class1(1),
                            class2(2),
                            class3(3),
                            class4(4)
                        }
                        MAX-ACCESS read-write
                        STATUS current
                        DESCRIPTION
                            "Bit mask which defines allowed classes per interface."
                        DEFVAL { { class0, class1, class2, class3, class4 } }
                        ::= { hm2PoeMgmtPortEntry 7 }

Where the DEFVAL contains the list. I am not MIB expert so not sure if its a valid entry or not. but this gets compiled as hm2PoeMgmtPortAllowedClassBits = MibTableColumn((1, 3, 6, 1, 4, 1, 248, 11, 12, 1, 1, 3, 1, 7), Bits().clone(namedValues=NamedValues(("class0", 0), ("class1", 1), ("class2", 2), ("class3", 3), ("class4", 4))).clone(namedValues=NamedValues(("class0", 0), ("class1", 1), ("class2", 2), ("class3", 3), ("class4", 4)))).setMaxAccess("readwrite")

And we get error File "C:\\Python27\\lib\\site-packages\\pyasn1\\type\\namedval.py", line 74, in __init__\n raise error.PyAsn1Error(\'Duplicate name %s\' % (name,))\n', 'PyAsn1Error: Duplicate name class4\n']

There is another issue I observed while parsing where syntax like this SYNTAX SnmpAdminString (SIZE(0|4..8)) are not getting parsed properly. Let me know if you want me raise separate issue for that

baid86 commented 6 years ago

With latest pysmi issue does not exists (with 4.4.4) so most probaly updating requirement.txt to mention latest pysmi should work. Though There is still and issue wrt to SYNTAX SnmpAdminString (SIZE(0|4..8)) I will raise separate issue to track that.

slieberth commented 6 years ago

I run the same issue, for me it looks like there is dependency to the python version ... I have three systems, for testing: MAC OS - Python 3.6.6, pysmi==0.3.2;pysnmp==4.4.6 - result: OK Ubuntu 18.4 - Python 3.6.6, pysmi==0.3.2;pysnmp==4.4.6 - result: OK Ubuntu 16.4 - Python 3.5.2, pysmi==0.3.2;pysnmp==4.4.6 -result NOK:

sudo python3 TA/testExecutor/tools/tdcnSnmpWikiTrapReceiver.py /tmp/tdcnSnmpWikiTrapReceiver.log Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/pysnmp/smi/builder.py", line 331, in loadModule exec (modData, g) File "", line 109, in File "/usr/local/lib/python3.5/dist-packages/pysnmp/proto/rfc1902.py", line 207, in clone return univ.OctetString.clone(self, *args, **kwargs).setFixedLength(self.getFixedLength()) TypeError: clone() got an unexpected keyword argument 'namedValues'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "TA/testExecutor/tools/tdcnSnmpWikiTrapReceiver.py", line 35, in 'OSPF-MIB' File "/usr/local/lib/python3.5/dist-packages/pysnmp/smi/builder.py", line 368, in loadModules self.loadModule(modName, userCtx) File "/usr/local/lib/python3.5/dist-packages/pysnmp/smi/builder.py", line 336, in loadModule 'MIB module \"%s\" load error: %s' % (modPath, traceback.format_exception(sys.exc_info())) pysnmp.smi.error.MibLoadError: MIB module "/home/script/.pysnmp/mibs/ISIS-MIB.py" load error: ['Traceback (most recent call last):\n', ' File "/usr/local/lib/python3.5/dist-packages/pysnmp/smi/builder.py", line 331, in loadModule\n exec (modData, g)\n', ' File "", line 109, in \n', ' File "/usr/local/lib/python3.5/dist-packages/pysnmp/proto/rfc1902.py", line 207, in clone\n return univ.OctetString.clone(self, args, kwargs).setFixedLength(self.getFixedLength())\n', "TypeError: clone() got an unexpected keyword argument 'namedValues'\n"]caused by <class 'TypeError'>: clone() got an unexpected keyword argument 'namedValues'

We will upgrade from 16.4(Python 3.5) to 18.4(Python3.6) in the near future. I will share the results ...

slieberth commented 6 years ago

upgrading pyasn1 from 0.2.3 to 0.4.4 solved the issue on my side :-)