Mibs for python net-snmp download

I am using python net-snmp libraries to execute some long requests on different switches. I would like to be able to download new mibs, but I cannot find documentation on how to do this.

PySNMP seems rather complicated and requires me to create Python objects for each mib (which does not scale for me); so i'm stuck in net-snmp libraries (which is nice, except for the Mib downloadable stuff).

I know that I can use the -m and -m options using the net-snmp command-line tools, as well as the documentation for pre-compiling the net-snmp set ( ./configure , make , etc.). with all mibs (and I also accept libraries); if the Python libraries do not offer the ability to download mibs, can I at least configure net-snmp to give my python libraries access to mib without recompiling?

+4
source share
2 answers

In the end, I found the answer. On 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 just like 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 before starting Python, this 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',) >>> 

Please note that before calling any of the functions of the netsnmp module netsnmp you must set os.environ['MIBS'] (because this will lead to loading the library and any changes to the environment after it has no effect).

You can (obviously) also set an 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',) >>> 
+3
source

(First post !!!)

Technically, you do not need to initialize or export any environment variables if you configured net-snmp correctly.

(Noting that I am on Ubuntu 12.04.1 LTS, I really did not have to compile net-snmp from the source code, and although I will fully consider everything that I did for completeness, this really should be applied if you want to configure some MIBs, which will be automatically overridden using net-snmp or its Python bindings.)

First I did sudo apt-get install libsnmp-base libsnmp-python libsnmp15 snmp

This will install net-snmp and its libraries, as well as Python bindings. It also sets some default MIBs ( net-snmp ) to /usr/share/mibs/netsnmp/ . If you want to capture a bunch of other IETF / IANA MIBs, follow these steps:

sudo apt-get install snmp-mibs-downloader

Which, as you would expect, will load a ton of other standard MIBs (including IF-MIB, etc.) into /var/lib/mibs/iana , /var/lib/mibs/ietf , as well as /usr/share/mibs/iana and /usr/share/mibs/ietf . The snmp-mibs-downloader package also provides the command /usr/bin/download-mibs if you want to download the MIB again.

Next, use the snmpconf command to configure the net-snmp environment:

 $ snmpconf -h /usr/bin/snmpconf [options] [FILETOCREATE...] options: -f overwrite existing files without prompting -i install created files into /usr/share/snmp. -p install created files into /home/$USER/.snmp. -I DIR install created files into DIR. -a Don't ask any questions, just read in current current .conf files and comment them -r all|none Read in all or none of the .conf files found. -R file,... Read in a particular list of .conf files. -g GROUP Ask a series of GROUPed questions. -G List known GROUPs. -c conf_dir use alternate configuration directory. -q run more quietly with less advice. -d turn on debugging output. -D turn on debugging dumper output. 

I used snmpconf -p and went through the menu items. The process basically searches for existing snmp.conf files ( /etc/snmp/snmp.conf by default) and merges them with the newly created configuration file, which will be placed in /home/$USER/.snmp/snmp.conf specified by the -p . From there, you just need to tell snmpconf where to look for the MIB, but there are a number of useful options that the script provides for creating configuration directives in snmp.conf .

After you finish working with snmpconf , you should have a working environment basically. This is what my (very bare bones) /home/$USER/.snmp/snmp.conf :

 ########################################################################### # # snmp.conf # # - created by the snmpconf configuration program # ########################################################################### # SECTION: Textual mib parsing # # This section controls the textual mib parser. Textual # mibs are parsed in order to convert OIDs, enumerated # lists, and ... to and from textual representations # and numerical representations. # mibdirs: Specifies directories to be searched for mibs. # Adding a '+' sign to the front of the argument appends the new # directory to the list of directories already being searched. # arguments: [+]directory[:directory...] mibdirs : +/usr/share/mibs/iana:/usr/share/mibs/ietf:/usr/share/mibs/netsnmp:/home/$USERNAME/.snmp/mibs/newmibs # mibs: Specifies a list of mibs to be searched for and loaded. # Adding a '+' sign to the front of the argument appends the new # mib name to the list of mibs already being searched for. # arguments: [+]mibname[:mibname...] mibs +ALL 

Some fixes:

  • When net-snmp loads this configuration file, it does not search in the recursive directory, so you must specify the absolute path to the directory in which the MIB is located.
  • If you decide to tell net-snmp to load all 300+ MIBs in these directories, this can slow down your SNMP requests, and some things will be reset to STDERR , as some MIBs will either be outdated or erroneous, or try to import definitions from MIBs that do not exist or have not been downloaded by the package. Your options: tell snmpconf how you want these errors to be processed, or find out what is missing or outdated, and load the MIB yourself. If you go after the latter, you may run into rabbitol MIB, so keep that in mind. Personally, I suggest you download them all, and then work back to download only the MIB data, which would make sense to poll a specific device.
  • The directory order that you specify in the search path in snmp.conf is important, especially if some MIBs are referenced or dependent on other MIBs. I made one mistake that I got fired by simply taking the MIB file in the iana directory and moving it to the ietf directory. I'm sure there is a way to programmatically determine which MIBs depend on others and make them happily coexist in the same directory, but I did not want to spend a lot of time trying to figure it out.

The moral of the story is if you have the correct snmp.conf, you should simply do this:

 $ python >>> import netsnmp >>> oid = netsnmp.VarList(netsnmp.Varbind('dot1qTpFdbPort')) >>> res = netsnmp.snmpwalk(oid, Version=2, DestHost='10.0.0.1', Community='pub') >>> print res ('2', '1') >>> 

FYI I missed a bunch of STDERR , but again you can configure your environment to dump STDERR to a log file, if you want, using snmp.conf configuration snmp.conf .

Hope this helps.

+2
source

All Articles