我是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网页中的一些文档。我可以在我的代理上使用不属于表的变量进行get、集合和遍历。我不能在桌子上散步,我可以在一半的物体上散步,我也不能在任何一个物体上做一套。这就是我在某些对象上尝试get或set时得到的结果。我认为这与我查询表的方式有关,但是一个对象和另一个对象之间没有一致性,即使它们看起来完全相同。
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')现在这是另一列的get
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')这是同一个对象上的一个集的调试器反馈给一个应该在范围内的I4。
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的结构,所以我无法追溯出哪里出了问题,所以如果需要的话,我可以发布其余的代码,这个测试只需要几百行。
谢谢你提供的任何帮助
发布于 2017-10-24 17:33:10
要实现动态SNMP表(可以通过SNMP集创建/删除行),您应该设置MibTable、MibTableRow和MibTableColumn对象的层次结构:
(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类型值的专用状态列。您可以阅读关于RowStatus如何在RFC2579中工作的详细信息(搜索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部分对应于abc的exampleTableColumn1字符串值,这是一个示例索引值)。
以及摧毁整排:
$ 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之外的东西。
完整的示例脚本可以是在这里看到的。
请注意,到目前为止,我们还没有用mibdump.py从ASN.1 MIB生成任何代码。
如果您在代码方面需要更多的手动帮助,我建议您转到GutHub的pysnmp GutHub。
理论射击
使用SNMP时,MIB的使用量是双重的.SNMP客户端(例如管理人员)使用MIB作为“模式”来计算它们从SNMP服务器(例如代理)接收的数据。后者实际上实现了基于MIB模式(模式实例)的具体数据结构。
对于pysnmp,我们使用相同的Python类来实现这两个目的。
MibTable、MibTableRow、MibTableColumn和MibScalar)。MibScalarInstance)的控制之下,无论何时您打算在运行时管理模式实例对象,模式对象都可能对其进行操作。这有望解释为什么pysnmp MIB管理代码看起来类似于管理器/代理实现。
https://stackoverflow.com/questions/46895878
复制相似问题