pyagentx-snmp,代码是如何链接到MIB的



我正在研究pyagentx(通过python扩展snmp代理)。https://github.com/rayed/pyagentx

我不了解示例代码是如何链接到MIB的。这里的示例是class NetSnmpTestMibTableNET-SNMP-EXAMPLES-MIB.txt链接到netSnmpIETFWGTablehttp://www.net-snmp.org/docs/mibs/NET-SNMP-EXAMPLES-MIB.txt

#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Rayed Alrashed 2015-06-14
AgentX sub agent that implement some parts of NET-SNMP-EXAMPLES-MIB:
<http://www.net-snmp.org/docs/mibs/NET-SNMP-EXAMPLES-MIB.txt>
snmpwalk -v 2c -c public localhost NET-SNMP-EXAMPLES-MIB::netSnmpExampleScalars
snmptable -v 2c -c public -Ci localhost NET-SNMP-EXAMPLES-MIB::netSnmpIETFWGTable 
Try snmpset:
snmpset -v 2c -c public localhost NET-SNMP-EXAMPLES-MIB::netSnmpExampleInteger.0 i 10
snmpset -v 2c -c public localhost NET-SNMP-EXAMPLES-MIB::netSnmpExampleInteger.0 i 200
snmpset -v 2c -c public localhost NET-SNMP-EXAMPLES-MIB::netSnmpExampleString.0 s "Test"
'''
import time
import random
import pyagentx
def str_to_oid(data):
    length = len(data)
    oid_int = [str(ord(i)) for i in data]
    return str(length) + '.' + '.'.join(oid_int)

class NetSnmpTestMibScalar(pyagentx.Updater):
    def update(self):
        self.set_INTEGER('1.0', 1000)
        self.set_OCTETSTRING('3.0', 'String for NET-SNMP-EXAMPLES-MIB')
        self.set_OBJECTIDENTIFIER('4.0', '1.2')
        self.set_IPADDRESS('5.0', '127.0.0.1')
        self.set_COUNTER32('6.0', 2000)
        self.set_GAUGE32('7.0', 2000)
        self.set_TIMETICKS('8.0', 1000000)
        self.set_OPAQUE('9.0', 'Test')
        self.set_COUNTER64('10.0', 2000)

class NetSnmpTestMibTable(pyagentx.Updater):
    def update(self):
        # implement netSnmpIETFWGTable from NET-SNMP-EXAMPLES-MIB.txt
        # Number of entries in table is random to show that MIB is reset
        # on every update
        for i in range(random.randint(3, 5)):
            idx = str_to_oid('group%s' % (i+1))
            self.set_OCTETSTRING('1.1.2.' + idx, 'member 1')
            self.set_OCTETSTRING('1.1.3.' + idx, 'member 2')

class NetSnmpIntegerSet(pyagentx.SetHandler):
    def test(self, oid, data):
        if int(data) > 100:
            raise pyagentx.SetHandlerError()
    def commit(self, oid, data):
        print "COMMIT CALLED: %s = %s" % (oid, data)

class MyAgent(pyagentx.Agent):
    def setup(self):
        self.register('1.3.6.1.4.1.8072.2.1', NetSnmpTestMibScalar)
        self.register('1.3.6.1.4.1.8072.2.2', NetSnmpTestMibTable)
        self.register_set('1.3.6.1.4.1.8072.2.1.1.0', NetSnmpIntegerSet)

def main():
    pyagentx.setup_logging()
    try:
        a = MyAgent()
        a.start()
    except Exception as e:
        print "Unhandled exception:", e
        a.stop()
    except KeyboardInterrupt:
        a.stop()
if __name__=="__main__":
    main()

如果我运行一个命令,我会得到预期的输出,但我不确定它是如何工作的,代码如何知道更新netSnmpIETFWGTable?正如你所看到的,我可以从中读取由以下示例代码设置的值:

snmptable -v 2c -c public -Ci localhost NET-SNMP-EXAMPLES-MIB::netSnmpIETFWGTable 
SNMP table: NET-SNMP-EXAMPLES-MIB::netSnmpIETFWGTable
   index nsIETFWGChair1 nsIETFWGChair2
"group1"     "member 1"     "member 2"
"group2"     "member 1"     "member 2"
"group3"     "member 1"     "member 2"

MIB中的对象名就像一个DNS条目,它们只是指向后台的一个丑陋的数字。

NET-SNMP-EXAMPLES-MIB中可以看到netSnmpIETFWGTable被定义为netSnmpExamples 2。向上滚动几行显示定义为netSnmp 2netSnmpExamples。这在IMPORTS部分中声明为来自NET-SNMP-MIB,因此您需要查看netSnmp是什么,等等。SNMPv2-SMI MIB是SNMP链的顶部。

你最终得到的是netSnmpIETFWGTable翻译成1.3.6.1.4.1.8072.2.2.1。向下查看文件底部的初始化代码,可以看到这个OID的父级链接到类:

self.register('1.3.6.1.4.1.8072.2.2', NetSnmpTestMibTable)

这应该有望回答您的问题,"代码如何知道更新netSnmpIETFWGTable。"


您也可以试用snmptranslate实用程序,它比查看MIB文件更容易!

$ snmptranslate -On NET-SNMP-EXAMPLES-MIB::netSnmpIETFWGTable
.1.3.6.1.4.1.8072.2.2.1
$ snmptranslate -Of NET-SNMP-EXAMPLES-MIB::netSnmpIETFWGTable
.iso.org.dod.internet.private.enterprises.netSnmp.netSnmpExamples.netSnmpExampleTables.netSnmpIETFWGTable
$ snmptranslate 1.3.6.1.4.1.8072.2.2.1
NET-SNMP-EXAMPLES-MIB::netSnmpIETFWGTable
$ snmptranslate -Of 1.3.6.1.4.1.8072.2.2.1
.iso.org.dod.internet.private.enterprises.netSnmp.netSnmpExamples.netSnmpExampleTables.netSnmpIETFWGTable

最新更新