(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.