I'm using net-snmp's python libraries to do some long queries on various switches. I would like to be able to load new mibs -- but I cannot find any documentation on how to do this.
PySNMP appears to be rather complicated and requires me to create Python objects for each mib (which doesn't scale for me); so I'm stuck with net-snmp's libraries (which aren't bad except for the loading mib thing).
I know I can use the -m
and -M
options with the net-snmp command-line tools, and there's documentation on pre-compiling the net-snmp suite (./configure
, make
etc.) with all the mibs (and I assume into the libraries too); if the Python libraries do not offer the ability to load mibs, can I at least configure net-snmp to provide my python libraries access to the mibs without having to recompile?
I found an answer after all. From the snmpcmd(1)
man page:
-m MIBLIST
Specifies a colon separated list of MIB modules (not
files) to load for this application. This overrides (or
augments) the environment variable MIBS, the snmp.conf
directive mibs, and the list of MIBs hardcoded into the
Net-SNMP library.
The key part here is that you can use the MIBS
environment variable the same way you use the -m
command line option...and that support for this is implemented at the library level. This means that if you define the MIBS
environment variable prior to starting Python, it will affect the behavior of the netsnmp
library:
$ python
Python 2.7.2 (default, Oct 27 2011, 01:40:22)
[GCC 4.6.1 20111003 (Red Hat 4.6.1-10)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import netsnmp
>>> os.environ['MIBS'] = 'UPS-MIB:SNMPv2-SMI'
>>> oid = netsnmp.Varbind('upsAlarmOnBattery.0')
>>> netsnmp.snmpget(oid, Version=1, DestHost='myserver', Community='public')
('0',)
>>>
Note that you must set os.environ['MIBS']
before calling any of the netsnmp
module functions (because this will load the library and any environment changes after this will have no affect).
You can (obviously) also set the environment variable outside of Python:
$ export MIBS='UPS-MIB:SNMPv2-SMI'
$ python
>>> import netsnmp
>>> oid = netsnmp.Varbind('upsAlarmOnBattery.0')
>>> netsnmp.snmpget(oid, Version=1, DestHost='myserver', Community='public')
('0',)
>>>