<p>(第一篇帖子!!!)</p>
<p>从技术上讲,如果正确配置net snmp,则不必初始化或导出任何环境变量。</p>
<p>(注意到我在Ubuntu 12.04.1lts上,所以我真的不需要从源代码编译<code>net-snmp</code>,即使我会完整地介绍我所做的工作,但这应该只在您希望设置一些mib,以便通过<code>net-snmp</code>或其Python绑定自动插入时才适用。)</p>
<p>首先我做了<code>sudo apt-get install libsnmp-base libsnmp-python libsnmp15 snmp</code></p>
<p>这将安装net snmp及其库以及Python绑定。它还会在<code>/usr/share/mibs/netsnmp/</code>中安装一些默认mib(仅用于<code>net-snmp</code>)。如果您想获取其他IETF/IANA MIB,请执行以下操作:</p>
<p><code>sudo apt-get install snmp-mibs-downloader</code></p>
<p>如您所料,它会将大量其他标准MIB(包括IF-MIB等)下载到<code>/var/lib/mibs/iana</code>、<code>/var/lib/mibs/ietf</code>以及<code>/usr/share/mibs/iana</code>和<code>/usr/share/mibs/ietf</code>。如果您想再次下载mib,<code>snmp-mibs-downloader</code>包还提供<code>/usr/bin/download-mibs</code>命令。</p>
<p>接下来,使用<code>snmpconf</code>命令设置您的网络snmp环境:</p>
<pre class="lang-none prettyprint-override"><code>$ 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.
</code></pre>
<p>我用<code>snmpconf -p</code>浏览了菜单项。该进程基本上查找现有的snmp.conf文件(默认情况下为<code>/etc/snmp/snmp.conf</code>),并将这些文件与新创建的配置文件合并,后者将放入由<code>-p</code>选项指定的<code>/home/$USER/.snmp/snmp.conf</code>中。从那以后,您只需要告诉<code>snmpconf</code>在哪里查找mib,但是脚本提供了许多有用的选项,用于在<code>snmp.conf</code>中生成配置指令。</p>
<p>完成<code>snmpconf</code>后,您应该有一个主要的工作环境。以下是我的(非常简单的)骨骼<code>/home/$USER/.snmp/snmp.conf</code>的样子:</p>
<pre class="lang-none prettyprint-override"><code>###########################################################################
#
# 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
</code></pre>
<p>有些问题:</p>
<ol>
<li>当<code>net-snmp</code>加载此配置文件时,它不会执行递归目录搜索,因此您必须给出mib所在目录的绝对路径。</li>
<li>如果您选择告诉<code>net-snmp</code>加载这些目录中的所有300多个mib,则可能会减慢您的SNMP查询速度,并且一定会有一些东西转储到<code>STDERR</code>,因为某些mib可能已过期、错误,或者试图从不存在或未被包下载的mib导入定义。您的选择是:告诉<code>snmpconf</code>您希望如何处理这些错误,或者找出丢失或过期的内容,然后自己下载MIB。如果你选择后者,你可能会发现自己陷入了MIB的困境,所以请记住这一点。就我个人而言,我建议您将它们全部加载,然后反向工作,只加载给定的mib,这对于轮询特定设备是有意义的。</li>
<li>在<code>snmp.conf</code>的搜索路径中指定的目录的顺序很重要,特别是当某些mib引用或依赖于其他mib时。我犯了一个错误,我只是通过在<code>iana</code>目录中获取一个MIB文件并将其移动到<code>ietf</code>目录中就可以离开。我确信有一种方法可以通过编程找出哪些mib依赖于其他mib,并使它们在一个目录中愉快地共存,但我不想浪费大量时间试图找出这一点。</li>
</ol>
<p>这个故事的寓意是,如果您有一个正确的snmp.conf,那么您应该能够这样做:</p>
<pre class="lang-python prettyprint-override"><code>$ 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')
>>>
</code></pre>
<p>仅供参考,我省略了一组<code>STDERR</code>输出,但是如果您希望通过<code>snmp.conf</code>配置指令将<code>STDERR</code>转储到日志文件,您可以再次配置您的环境。</p>
<p>希望这有帮助。</p>