pysnmp代理表实现

时间:2017-10-23 18:11:23

标签: python snmp pysnmp

我是snmp的新手。我正在使用pysnmp编写snmp代理程序,我使用mibdump.py编译了MIB并使用以下示例运行它:http://www.nealc.com/blog/blog/2013/02/23/writing-an-snmp-agent-with-a-custom-mib-using-pysnmp/ http://www.cloud-rocket.com/2013/08/writing-custom-mib-for-pysnmp-agent/以及de pysnmp网页的一些文档。我可以使用不属于表的变量对我的代理执行get,a set和walk。我不能在桌子上散步,我可以做一半的物体,我不能做任何一个。这是我在一些对象上尝试获取或设置时得到的结果。我认为这与我查询表格的方式有关,但是一个对象与另一个对象之间没有一致性,即使它们看起来完全相同。

pysnmp$ snmpset -v2c -c private localhost 1.3.6.1.4.1.1206.4.2.3.3.2.1.3.0 i 1
Error in packet.
Reason: notWritable (That object does not support modification)
Failed object: iso.3.6.1.4.1.1206.4.2.3.3.2.1.3.0

这是一个由mibdump.py定义的对象,如

fontName = MibTableColumn((1, 3, 6, 1, 4, 1, 1206, 4, 2, 3, 3, 2, 1, 3), DisplayString().subtype(subtypeSpec=ValueSizeConstraint(0, 64))).setMaxAccess("readwrite")
if mibBuilder.loadTexts: fontName.setStatus('mandatory')

现在这是另一个专栏

pysnmp$ snmpget -v2c -c public localhost 1.3.6.1.4.1.1206.4.2.3.3.2.1.5.0 
iso.3.6.1.4.1.1206.4.2.3.3.2.1.5.0 = No Such Instance currently exists at this OID

像这样定义

fontCharSpacing = MibTableColumn((1, 3, 6, 1, 4, 1, 1206, 4, 2, 3, 3, 2, 1, 5), Integer32().subtype(subtypeSpec=ValueRangeConstraint(0, 255))).setMaxAccess("readwrite")
if mibBuilder.loadTexts: fontCharSpacing.setStatus('mandatory')

这是同一个对象上的一个集合的调试器反馈到一个应该在范围内的i 4

ValueConstraintError: ConstraintsIntersection(ValueRangeConstraint(-2147483648, 2147483647), ValueRangeConstraint(1, 255)) failed at: ValueConstraintError('ValueRangeConstraint(1, 255) failed at: ValueConstraintError(0,)',) at Integer32

我认为还有一些错误与我有关,对我而言,似乎MIB.py文件无法正确读取,可能是因为我在自定义mib时覆盖了一些代码。使用下一个功能

def createVariable(SuperClass, getValue, sValue, *args):
    """This is going to create a instance variable that we can export. 
    getValue is a function to call to retreive the value of the scalar
    """
    class Var(SuperClass):
        def readGet(self, name, *args):
            print " Getting var..."
            return name, self.syntax.clone(getValue())

        def writeTest(self, name, *args ):
            print " Testing var..."

        def writeCommit(self, name, val, *args ):
            print " Setting var..."
            sValue(val)

    return Var(*args)

我不理解pysnmp的结构所以我无法追溯出错的地方,所以我可以在必要时发布剩下的代码,这个测试只有几百行。

感谢您提供的任何帮助

1 个答案:

答案 0 :(得分:0)

要实现动态SNMP表(您可以通过SNMP SET创建/删除行),您应该设置MibTableMibTableRowMibTableColumn个对象的层次结构:

(MibTable,
 MibTableRow,
 MibTableColumn,
 MibScalarInstance) = mibBuilder.importSymbols(
    'SNMPv2-SMI',
    'MibTable',
    'MibTableRow',
    'MibTableColumn',
    'MibScalarInstance'
)

在反映对象层次结构的OID下用pysnmp注册它们,例如:

RowStatus, = mibBuilder.importSymbols('SNMPv2-TC', 'RowStatus')

mibBuilder.exportSymbols(
    '__EXAMPLE-MIB',
    # table object
    exampleTable=MibTable((1, 3, 6, 6, 1)).setMaxAccess('readcreate'),
    # table row object, also carries references to table indices
    exampleTableEntry=MibTableRow((1, 3, 6, 6, 1, 5)).setMaxAccess('readcreate').setIndexNames((0, '__EXAMPLE-MIB', 'exampleTableColumn1')),
    # table column: string index
    exampleTableColumn1=MibTableColumn((1, 3, 6, 6, 1, 5, 1), v2c.OctetString()).setMaxAccess('readcreate'),
    # table column: string value
    exampleTableColumn2=MibTableColumn((1, 3, 6, 6, 1, 5, 2), v2c.OctetString()).setMaxAccess('readcreate'),
    # table column: integer value with default
    exampleTableColumn3=MibTableColumn((1, 3, 6, 6, 1, 5, 3), v2c.Integer32(123)).setMaxAccess('readcreate'),
    # table column: row status
    exampleTableStatus=MibTableColumn((1, 3, 6, 6, 1, 5, 4), RowStatus('notExists')).setMaxAccess('readcreate')
)

确保说明表索引应如何显示,例如超过MibTableColumn OID的OID的尾随部分。您可以通过将一个或多个列配置为.setIndexNames()来完成此操作。

如果您希望能够一次性创建/删除整行,则需要在表中具有专用的状态列,其值为RowStatus。您可以在RFC2579中查看有关RowStatus工作原理的详细信息(搜索RowStatus)。

到目前为止,您应该可以创建新行:

$ snmpset -v2c -c public 127.0.0.1 1.3.6.6.1.5.2.97.98.99 s “my value”
$ snmpset -v2c -c public 127.0.0.1 1.3.6.6.1.5.4.97.98.99 i 4

(OID的97.98.99部分对应exampleTableColumn1字符串值abc,这是一个示例索引值。)

除了破坏整行:

$ snmpset -v2c -c public 127.0.0.1 1.3.6.6.1.5.4.97.98.99 i 6

以这种方式创建/销毁列时,会在.createTest()个对象上调用createCommit() / destroyTest()destroyCommit() / MibTableColumn方法。在修改值时调用writeTest() / writeCommit()方法。 这些是您可能想要覆盖的方法来控制pysnmp之外的东西。

完整的示例脚本可以是seen here

请注意,到目前为止,我们没有使用mibdump.py从ASN.1 MIB生成任何代码。

如果您需要更多关于代码的动手帮助,我建议您转到GutHub的pysnmp issues

理论拍摄

使用SNMP,MIB的使用量是双倍的。 SNMP客户端(例如管理员)使用MIB作为"模式"找出他们从SNMP服务器(例如代理商)收到的数据。后者实际上是基于MIB模式(模式实例)实现具体的数据结构。

使用pysnmp,我们使用相同的Python类集来实现这两个目的。

  • 管理员通常只使用架构对象(例如MibTableMibTableRowMibTableColumnMibScalar
  • 当您打算在运行时管理架构实例对象时,代理将其数据置于可能由架构对象操纵的架构实例对象(MibScalarInstance)的控制之下。

这有希望解释为什么pysnmp MIB管理代码对于经理/代理实现看起来类似。