2

I'm attempting to use MIB files in PySNMP. The code is fairly straightforward. Nothing complex. Just trying to get the information under an OID. The code I'm using is as follows:

#!/usr/local/bin/python2.7

from pysnmp.smi import builder, view, error
from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()
mibBuilder = builder.MibBuilder()
mibPath = mibBuilder.getMibPath() + ( '/path/to/command/mibs', )
mibBuilder.setMibPath( *mibPath )
mibBuilder.loadModules( 
    'MIB-File',
    )
mibView = view.MibViewController( mibBuilder )
errorIndication, errorStatus, errorIndex, \
                 varBindTable = cmdGen.nextCmd( 
    cmdgen.CommunityData( 'Name', 'Community' ),
    cmdgen.UdpTransportTarget( ( 'IP Address', 161 ) ),
    ( ( '', 'serverName' ), ),
    )

print varBindTable

I know the data put into this is accurate as when I use a asynCommandGenerator.asyncNextCmd using the same udpTransportTarget and CommunityData it works without issue. Plus the error I'm seeing is very specific to the MibBuilder Components.

The Error I'm seeing is:

Traceback (most recent call last):

File "./snmpcollectortest.py", line 11, in

'NS-MIB-smiv2',

File "/usr/lib/python2.7/site-packages/pysnmp-4.2.1-py2.7.egg/pysnmp/smi/builder.py", line 221, in loadModules

pysnmp.smi.error.SmiError: MIB module "/path/to/command/mibs/MIB-File.py" load error: MIB file "ASN1.py[co]" not found in search path

Update:

I found that I didn't have M2Crypto installed which is why I couldn't find ASN1.py. However I have corrected this and I'm still getting the same error.

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
Drahkar
  • 1,694
  • 13
  • 17
  • `mibPath = mibBuilder.getMibPath() + ( '/path/to/command/mibs', )` that shoudl actually be a valid path. Or did you obfuscate it deliberately? Otherwise the error is that MIB can not be found. – favoretti Jan 26 '12 at 17:31
  • That was deliberate due to the fact it was paths on my business environment. – Drahkar Jan 27 '12 at 19:59

1 Answers1

7

The getMibPath()/setMibPath() methods are obsolete. They don't work unless you .egg pysnmp or its MIB modules.

You should always use the getMibSources()/setMibSources() methods instead. These work for both .egg and file-based setup.

mibPath = mibBuilder.getMibSources() + (builder.DirMibSource('/path/to/command/mibs'),)
mibBuilder.setMibSources(*mibPath)
mibBuilder.loadModules(
    'MIB-File',
)

BTW, pysnmp does not require M2Crypto, what is required is pyasn1 and pycrypto (for SNMPv3 ciphering only).

Mike Pennington
  • 41,899
  • 19
  • 136
  • 174
Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
  • Interesting. I'll try making these changes when I get into the office on Monday. As to M2Crypto, its the only location that I'm aware of that supplies the ASN1.py module which is required by the pysnmp MIB module files created by build-pysnmp-mib. Unless there is another way to convert the MIBs to pysnmp modules? – Drahkar Jan 28 '12 at 11:47
  • This was definitely a move in the right direction. Changing this resolved the initial asn1.py error and I was able to quickly resolve several others be installing the pysnmp-mibs module. However now I'm getting a load error: name 'Counter64' is not defined error. I have the SMI packaged installed, so I'm unsure what is causing it. – Drahkar Jan 30 '12 at 12:20
  • 2
    Actually I'll pose that in another question. You answered this one and deserve the accept. Thanks Ilya. – Drahkar Jan 30 '12 at 12:22