2

When I use:

build-pysnmp-mib  -o iSCSI_RAID_Rack-1.2.1.py iSCSI_RAID_Rack-1.2.1.mib

To convert an .mib file to a .py file it converts it with no errors. When I try to load the file on python I get the following error:

ERROR:

"NameError: name 'MibScalar' is not defined

Full ERROR:

File "C:\Python27\lib\site-packages\pysnmp\smi\builder.py", line 259, in loadModules 'MIB module \"%s\" load error: %s' % (modPath, traceback.format_exception(*sys.exc_info())) SmiError: MIB module "C:\Python27\snmp\MIB\iSCSI_RAID_Rack-1.2.1.pyc" load error: ['Traceback (most recent call last):\n', ' File "C:\Python27\lib\site-packages\pysnmp\smi\builder.py", line 255, in loadModules\n exec(modData, g)\n', ' File "C:\Python27\snmp\MIB\iSCSI_RAID_Rack-1.2.1.py", line 27, in \n sys_status_temp = MibScalar((1, 3, 6, 1, 4, 1, 22274, 1, 1, 1, 2), DisplayString()).setMaxAccess("readonly").setLabel("sys-status-temp")\n', "NameError: name 'MibScalar' is not defined\n"]

CODE:

def addFile(dir, file):
    mibBuilder = cmdGen.snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder

    mibSources = mibBuilder.getMibSources() + (
                                    builder.DirMibSource(dir),
                                    )

    mibBuilder.setMibSources(*mibSources)
    mibBuilder.loadModules(file)
KVL
  • 31
  • 1
  • 5

3 Answers3

0

Your iSCSI_RAID_Rack-1.2.1.py file might be incomplete. That may be caused by smidump (called from build-pysnmp-mib) failure.

Make sure all the MIBs imported by iSCSI_RAID_Rack-1.2.1.mib are in smidump search path.

Another idea is to run smidump manually (in the same way as invoked from build-pysnmp-mib) to see if it complains on bad MIB syntax.

Pooh
  • 276
  • 1
  • 3
0

I had the same issue and I tried it directly with smidump and I was not successful.

The question and the answer I found here are a bit old and nowadays we have the PySMI package. This package is usually available with pip and it comes with a command line tool called mibdump.py.

When I first tried mibdump.py, I had an issue with the version of the python interpreter used in the script. So I changed it myself to point to a valid python interpreter in my system.

After that, I installed the download-mibs tool (snmp-mibs-downloader on Ubuntu repositories). This tool automatically downloads some base MIBs usually necessary for the translation. You can configure the MIB downloader at /etc/snmp-mibs-downloader (at least on Mint Linux), there you can see where the MIBs are being downloaded to.

Then I just:

mibdump.py --destination-directory=<target files directory> --mib-source=<place where the source MIBs are> --mib-source=/var/lib/mibs/iana --mib-source=/var/lib/mibs/ietf <name MIB to be translated>

Notice that I'm using the "--mib-source" to point to the place where my custom MIBs are located and also to point where the base MIBs were downloaded to.

Gustavo Meira
  • 2,875
  • 3
  • 21
  • 33
0

Incase mibdump.py is not working, latest pysnmp creates the *.py for your custom mib file while compiling. Just compile the below code, you custom_mib.py file will be located at ~/.pysnmp/mibs folder

snmpEngine = engine.SnmpEngine()
snmpContext = context.SnmpContext(snmpEngine)
mibBuilder = snmpContext.getMibInstrum().getMibBuilder()
compiler.addMibCompiler(mibBuilder, sources = ['/home/mib_file/']) # at this path you should keep your own mib file and all others dependent library mib files
mibBuilder.addMibSources(builder.DirMibSource('/home/mib_file/'))
mibBuilder.loadModules('MY-SMI') # MY-SMI.mib is the mib file prepared by me
mibBuilder.unloadModules()
print('done')

If you keep your mib file & it's all dependent mibs at correct location like above specified path then loadModules() will generate the *.py file for you.

Rathin Paul
  • 419
  • 4
  • 11