簡體   English   中英

python net-snmp加載mibs

[英]python net-snmp loading mibs

我正在使用 net-snmp 的 python 庫對各種開關進行一些長查詢。 我希望能夠加載新的 mib——但我找不到任何有關如何執行此操作的文檔。

PySNMP 似乎相當復雜,需要我為每個 mib 創建 Python 對象(這對我來說不適用); 所以我堅持使用 net-snmp 的庫(除了加載 mib 之外還不錯)。

我知道我可以使用-m-M與NET-SNMP命令行工具選項,並有對預編譯的net-snmp套件的文檔( ./configuremake等)與所有的MIB(和我也假設進入圖書館); 如果 Python 庫不提供加載 mib 的能力,我至少可以配置 net-snmp 以提供對 mib 的 python 庫訪問而無需重新編譯嗎?

如果正確配置 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轉儲到日志文件。

希望這可以幫助。

畢竟我找到了答案。 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',)
>>> 

請注意,您必須調用任何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',)
>>> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM