python net-snmp 加载 mib

发布于 2024-12-12 15:16:48 字数 415 浏览 11 评论 0原文

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

PySNMP 似乎相当复杂,需要我为每个 mib 创建 Python 对象(这对我来说无法扩展);所以我坚持使用 net-snmp 的库(除了加载 mib 之外,这些库还不错)。

我知道我可以在 net-snmp 命令行工具中使用 -m-M 选项,并且有关于预编译 net-snmp 套件的文档(< code>./configure、make 等)以及所有 mib(我假设也进入了库);如果 Python 库不提供加载 mib 的能力,我是否至少可以配置 net-snmp 以使我的 python 库能够访问 mib,而无需重新编译?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夕色琉璃 2024-12-19 15:16:49

我最终找到了答案。来自 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.

这里的关键部分是您可以像使用 -m 一样使用 MIBS 环境变量> 命令行选项...并且对此的支持是在库级别实现的。这意味着,如果您在启动 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',)
>>> 

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

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

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',)
>>> 
臻嫒无言 2024-12-19 15:16:49

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

(请注意,我使用的是 Ubuntu 12.04.1 LTS,因此我实际上不必从源代码编译 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 命令设置您的 net-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),并将这些文件与新创建的配置文件合并,该文件将放置在 /home 中/$USER/.snmp/snmp.conf-p 选项指定。从那时起,您实际上只需要告诉 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 转储到日志文件。

希望这有帮助。

You technically don't have to initialize or export any environment variables if you configure net-snmp properly.

(Noting that I'm on Ubuntu 12.04.1 LTS so I really didn't have to compile net-snmp from source, and even though I'll cover the entirety of what I did for completeness, this should really only apply if you want to set up some MIBs to be automatically slurped in by 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 the Python bindings. It also installs some default MIBs (only for for net-snmp) in /usr/share/mibs/netsnmp/. If you want to grab a bunch of other IETF/IANA MIBs, do:

sudo apt-get install snmp-mibs-downloader

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

Next, Use the snmpconf command to set up your 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 walked through the menu items. The process basically looks for existing snmp.conf files (/etc/snmp/snmp.conf by default) and will merge those in with the newly created config file that will get put in /home/$USER/.snmp/snmp.conf specified by the -p option. From there on out you really only need to tell snmpconf where to look for MIBs, but there are a number of useful options that are provided by the script for generating configuration directives in snmp.conf.

You should have a mostly working environment after you finish up with snmpconf. Here's what my (very bare-bones) /home/$USER/.snmp/snmp.conf looks like:

###########################################################################
#
# 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 gotchas:

  1. When net-snmp loads this config file it doesn't do a recursive directory search, so you have to give an absolute path to the directory where the MIBs live.
  2. If you choose to tell net-snmp to load all 300+ MIBs in those directories, it could slow down your SNMP queries, and there are bound to be some things dumped to STDERR because of the fact that some MIBs would either be out of date, wrong, or trying to import definitions from MIBs that don't exist or weren't downloaded by the package. Your options are: tell snmpconf how you want those errors to be handled, or figure out what's missing or out of date and download the MIB yourself. If you go for the latter, you may find yourself going down a MIB rabbithole, so keep that in mind. Personally I'd suggest that you load them all, and then work backwards to only load the given MIBs that would make sense for polling a particular device.
  3. The order of the directories that you specify in the search path in snmp.conf is important, especially if some MIBs reference or depend on other MIBs. I made one error I was getting go away simply by taking a MIB file in the iana directory and moving it into the ietf directory. I'm sure there's a way to programmatically figure out which MIBs depend on the others and make them happily coexist in a single directory but I didn't want to waste a bunch of time trying to figure this out.

The moral of the story is, if you've got a proper snmp.conf, you should just be able to 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 omitted a bunch of STDERR output but again you can configure your environment to dump STDERR to a logfile if you wish via snmp.conf configuration directives.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文