pythonnetsnmp加载mibs

2024-05-20 21:38:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用net snmp的python库在各种交换机上执行一些长查询。我希望能够加载新的MIB—但是找不到任何关于如何执行此操作的文档。

PySNMP看起来相当复杂,需要我为每个mib创建Python对象(这对我来说不可伸缩);因此我不得不使用net snmp的库(除了加载mib之外,这些库还不错)。

我知道我可以在net snmp命令行工具中使用-m-M选项,还有关于对所有mib预编译netsnmp套件(./configuremake等)的文档(我也假设这些库中有);如果Python库不提供加载mib的能力,我至少可以配置netsnmp来提供我的python库无需重新编译就可以访问mib?


Tags: 工具对象命令行文档makenet套件configure
2条回答

我终于找到了答案。从snmpcmd(1)手册页:

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

这里的关键部分是,您可以使用MIBS环境变量,就像使用-m命令行选项一样……而且对它的支持是在库级别实现的。这意味着,如果在启动Python之前定义了MIBS环境变量,它将影响netsnmp库的行为:

$ 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',)
>>> 

注意,必须在调用任何netsnmp模块函数之前设置os.environ['MIBS'](因为这将加载库,之后的任何环境更改都不会产生影响)。

您还可以(显然)在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',)
>>> 

(第一篇帖子!!!)

从技术上讲,如果正确配置net snmp,则不必初始化或导出任何环境变量。

(注意到我在Ubuntu 12.04.1lts上,所以我真的不需要从源代码编译net-snmp,即使我会完整地介绍我所做的工作,但这应该只在您希望设置一些mib,以便通过net-snmp或其Python绑定自动插入时才适用。)

首先我做了sudo apt-get install libsnmp-base libsnmp-python libsnmp15 snmp

这将安装net snmp及其库以及Python绑定。它还会在/usr/share/mibs/netsnmp/中安装一些默认mib(仅用于net-snmp)。如果您想获取其他IETF/IANA MIB,请执行以下操作:

sudo apt-get install snmp-mibs-downloader

如您所料,它会将大量其他标准MIB(包括IF-MIB等)下载到/var/lib/mibs/iana/var/lib/mibs/ietf以及/usr/share/mibs/iana/usr/share/mibs/ietf。如果您想再次下载mib,snmp-mibs-downloader包还提供/usr/bin/download-mibs命令。

接下来,使用snmpconf命令设置您的网络snmp环境:

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

我用snmpconf -p浏览了菜单项。该进程基本上查找现有的snmp.conf文件(默认情况下为/etc/snmp/snmp.conf),并将这些文件与新创建的配置文件合并,后者将放入由-p选项指定的/home/$USER/.snmp/snmp.conf中。从那以后,您只需要告诉snmpconf在哪里查找mib,但是脚本提供了许多有用的选项,用于在snmp.conf中生成配置指令。

完成snmpconf后,您应该有一个主要的工作环境。以下是我的(非常简单的)骨骼/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

有些问题:

  1. net-snmp加载此配置文件时,它不会执行递归目录搜索,因此您必须给出mib所在目录的绝对路径。
  2. 如果您选择告诉net-snmp加载这些目录中的所有300多个mib,则可能会减慢您的SNMP查询速度,并且一定会有一些东西转储到STDERR,因为某些mib可能已过期、错误,或者试图从不存在或未被包下载的mib导入定义。您的选择是:告诉snmpconf您希望如何处理这些错误,或者找出丢失或过期的内容,然后自己下载MIB。如果你选择后者,你可能会发现自己陷入了MIB的困境,所以请记住这一点。就我个人而言,我建议您将它们全部加载,然后反向工作,只加载给定的mib,这对于轮询特定设备是有意义的。
  3. snmp.conf的搜索路径中指定的目录的顺序很重要,特别是当某些mib引用或依赖于其他mib时。我犯了一个错误,我只是通过在iana目录中获取一个MIB文件并将其移动到ietf目录中就可以离开。我确信有一种方法可以通过编程找出哪些mib依赖于其他mib,并使它们在一个目录中愉快地共存,但我不想浪费大量时间试图找出这一点。

这个故事的寓意是,如果您有一个正确的snmp.conf,那么您应该能够这样做:

$ 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')
>>>

仅供参考,我省略了一组STDERR输出,但是如果您希望通过snmp.conf配置指令将STDERR转储到日志文件,您可以再次配置您的环境。

希望这有帮助。

相关问题 更多 >