pyagentx snmp,代码如何链接到 MIB?
pyagentx snmp, how is the code linked to the MIB?
我正在通过 python 查看 pyagentx(扩展 snmp 代理)。 https://github.com/rayed/pyagentx
我不明白示例代码是如何链接到 MIB 的。这里的示例是 class NetSnmpTestMibTable
从 NET-SNMP-EXAMPLES-MIB.txt
http://www.net-snmp.org/docs/mibs/NET-SNMP-EXAMPLES-MIB.txt
链接到 netSnmpIETFWGTable
#!/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 are like a DNS entry, they're just pointing to an ugly number 中的对象名称。
正在查找 NET-SNMP-EXAMPLES-MIB
you can see netSnmpIETFWGTable
defined as netSnmpExamples 2
. Scrolling up a couple of lines shows netSnmpExamples
defined as netSnmp 2
. This is declared in the IMPORTS
section as coming from NET-SNMP-MIB
, so you'd need to look there to see what netSnmp
is, etc. The SNMPv2-SMI
MIB 是 SNMP 链的顶端。
你最终得到的是 netSnmpIETFWGTable
转换为 1.3.6.1.4.1.8072.2.2.1
。查看文件底部的初始化代码,您可以看到此 OID 的父级链接到 class:
self.register('1.3.6.1.4.1.8072.2.2', NetSnmpTestMibTable)
希望能回答您的问题,"how does the code know to update 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
我正在通过 python 查看 pyagentx(扩展 snmp 代理)。 https://github.com/rayed/pyagentx
我不明白示例代码是如何链接到 MIB 的。这里的示例是 class NetSnmpTestMibTable
从 NET-SNMP-EXAMPLES-MIB.txt
http://www.net-snmp.org/docs/mibs/NET-SNMP-EXAMPLES-MIB.txt
netSnmpIETFWGTable
#!/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 are like a DNS entry, they're just pointing to an ugly number 中的对象名称。
正在查找 NET-SNMP-EXAMPLES-MIB
you can see netSnmpIETFWGTable
defined as netSnmpExamples 2
. Scrolling up a couple of lines shows netSnmpExamples
defined as netSnmp 2
. This is declared in the IMPORTS
section as coming from NET-SNMP-MIB
, so you'd need to look there to see what netSnmp
is, etc. The SNMPv2-SMI
MIB 是 SNMP 链的顶端。
你最终得到的是 netSnmpIETFWGTable
转换为 1.3.6.1.4.1.8072.2.2.1
。查看文件底部的初始化代码,您可以看到此 OID 的父级链接到 class:
self.register('1.3.6.1.4.1.8072.2.2', NetSnmpTestMibTable)
希望能回答您的问题,"how does the code know to update 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