ObjectIdentity.resolveWithMib()无法解析InetAddressIPv4类型的实例索引

时间:2017-09-29 02:16:20

标签: pysnmp

在步行TCP-MIB::tcpConnectionTable时,我注意到他们的表索引在调用resolveWithMib()后未解决;转向MIB检测调试显示,大小约束检查应用于文本IP地址值,而不是原始(4字节)值。非IP地址表索引不会出现此问题,例如IF-MIB::ifIndex

重现:

from pysnmp.smi.rfc1902 import ObjectIdentity
from pysnmp.smi.builder import MibBuilder
from pysnmp.smi.compiler import addMibCompiler
from pysnmp.smi.view import MibViewController
from pysnmp.debug import Debug, setLogger

builder = MibBuilder()
addMibCompiler(builder)
builder.loadModules('IF-MIB')
builder.loadModules('TCP-MIB')
view = MibViewController(builder)

def doit(*poargs):
    print(ObjectIdentity(*poargs).resolveWithMib(view).getMibSymbol())

setLogger(Debug('all'))
doit('.1.3.6.1.2.1.2.2.1.2.1')
doit('.1.3.6.1.2.1.6.19.1.7.1.9.49.48.46.48.46.48.46.49.49.22.1.14.49.57.50.46.49.54.57.46.50.48.46.49.53.52.2629')

返回:

2017-09-28 19:14:35,045 pysnmp: running pysnmp version 4.3.9
2017-09-28 19:14:35,045 pysnmp: debug category 'all' enabled
2017-09-28 19:14:35,045 pysnmp: resolving .1.3.6.1.2.1.2.2.1.2.1 as OID or label
2017-09-28 19:14:35,046 pysnmp: indexMib: re-indexing MIB view
2017-09-28 19:14:35,049 pysnmp: getNodeNameByOid: resolved :1.3.6.1.2.1.2.2.1.2.1 -> ('iso', 'org', 'dod', 'internet', 'mgmt', 'mib-2', 'interfaces', 'ifTable', 'ifEntry', 'ifDescr').1
2017-09-28 19:14:35,049 pysnmp: resolved ('.1.3.6.1.2.1.2.2.1.2.1',) into prefix ObjectName('1.3.6.1.2.1.2.2.1.2') and suffix ObjectName('1')
2017-09-28 19:14:35,049 pysnmp: getNodeNameByOid: resolved :1.3.6.1.2.1.2.2.1.2 -> ('iso', 'org', 'dod', 'internet', 'mgmt', 'mib-2', 'interfaces', 'ifTable', 'ifEntry', 'ifDescr').()
2017-09-28 19:14:35,049 pysnmp: resolved prefix ObjectName('1.3.6.1.2.1.2.2.1.2') into MIB node MibTableColumn((1, 3, 6, 1, 2, 1, 2, 2, 1, 2), DisplayString(subtypeSpec=ConstraintsIntersection(ValueSizeConstraint(0, 65535), ValueSizeConstraint(0, 255), ValueSizeConstraint(0, 255))))
2017-09-28 19:14:35,049 pysnmp: getNodeNameByOid: resolved :(1, 3, 6, 1, 2, 1, 2, 2, 1) -> ('iso', 'org', 'dod', 'internet', 'mgmt', 'mib-2', 'interfaces', 'ifTable', 'ifEntry').()
2017-09-28 19:14:35,049 pysnmp: resolved indices are (InterfaceIndex(1),)
('IF-MIB', 'ifDescr', (InterfaceIndex(1),))
2017-09-28 19:14:35,050 pysnmp: resolving .1.3.6.1.2.1.6.19.1.7.1.9.49.48.46.48.46.48.46.49.49.22.1.14.49.57.50.46.49.54.57.46.50.48.46.49.53.52.2629 as OID or label
2017-09-28 19:14:35,051 pysnmp: getNodeNameByOid: resolved :1.3.6.1.2.1.6.19.1.7.1.9.49.48.46.48.46.48.46.49.49.22.1.14.49.57.50.46.49.54.57.46.50.48.46.49.53.52.2629 -> ('iso', 'org', 'dod', 'internet', 'mgmt', 'mib-2', 'tcp', 'tcpConnectionTable', 'tcpConnectionEntry', 'tcpConnectionState').1.9.49.48.46.48.46.48.46.49.49.22.1.14.49.57.50.46.49.54.57.46.50.48.46.49.53.52.2629
2017-09-28 19:14:35,051 pysnmp: resolved ('.1.3.6.1.2.1.6.19.1.7.1.9.49.48.46.48.46.48.46.49.49.22.1.14.49.57.50.46.49.54.57.46.50.48.46.49.53.52.2629',) into prefix ObjectName('1.3.6.1.2.1.6.19.1.7') and suffix ObjectName('1.9.49.48.46.48.46.48.46.49.49.22.1.14.49.57.50.46.49.54.57.46.50.48.46.49.53.52.2629')
2017-09-28 19:14:35,051 pysnmp: getNodeNameByOid: resolved :1.3.6.1.2.1.6.19.1.7 -> ('iso', 'org', 'dod', 'internet', 'mgmt', 'mib-2', 'tcp', 'tcpConnectionTable', 'tcpConnectionEntry', 'tcpConnectionState').()
2017-09-28 19:14:35,051 pysnmp: resolved prefix ObjectName('1.3.6.1.2.1.6.19.1.7') into MIB node MibTableColumn((1, 3, 6, 1, 2, 1, 6, 19, 1, 7), Integer32(subtypeSpec=ConstraintsIntersection(ValueRangeConstraint(-2147483648, 2147483647), ConstraintsUnion(SingleValueConstraint(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12))), NamedValues((('synReceived', 4), ('established', 5), ('finWait2', 7), ('timeWait', 11), ('finWait1', 6), ('lastAck', 9), ('closeWait', 8), ('deleteTCB', 12), ('closed', 1), ('closing', 10), ('listen', 2), ('synSent', 3)))))
2017-09-28 19:14:35,051 pysnmp: getNodeNameByOid: resolved :(1, 3, 6, 1, 2, 1, 6, 19, 1) -> ('iso', 'org', 'dod', 'internet', 'mgmt', 'mib-2', 'tcp', 'tcpConnectionTable', 'tcpConnectionEntry').()
2017-09-28 19:14:35,052 pysnmp: error resolving table indices at MibTableRow, (9, 49, 48, 46, 48, 46, 48, 46, 49, 49, 22, 1, 14, 49, 57, 50, 46, 49, 54, 57, 46, 50, 48, 46, 49, 53, 52, 2629): ConstraintsIntersection(ValueSizeConstraint(0, 65535), ValueSizeConstraint(4, 4)) failed at: ValueConstraintError("ValueSizeConstraint(4, 4) failed at: ValueConstraintError('10.0.0.11',)",) at InetAddressIPv4
2017-09-28 19:14:35,052 pysnmp: resolved indices are ((9, 49, 48, 46, 48, 46, 48, 46, 49, 49, 22, 1, 14, 49, 57, 50, 46, 49, 54, 57, 46, 50, 48, 46, 49, 53, 52, 2629),)
('TCP-MIB', 'tcpConnectionState', ((9, 49, 48, 46, 48, 46, 48, 46, 49, 49, 22, 1, 14, 49, 57, 50, 46, 49, 54, 57, 46, 50, 48, 46, 49, 53, 52, 2629),))

注意IF-MIB::ifDescr的实例索引如何正确解析为(InterfaceIndex(1),),而TCP-MIB::tcpConnectionState的实例索引未解析为((9, 49, 48, 46, ...),)

如何修复或解决此问题?

1 个答案:

答案 0 :(得分:1)

在我看来,你正在传递格式错误的索引。例如,46 ASCII序号表示IPv4点分表示法中的小数点。永远不应将此点编码到OID中。

以下是tcpConnectionTable的索引结构:

INDEX   { tcpConnectionLocalAddressType,
          tcpConnectionLocalAddress,
          tcpConnectionLocalPort,
          tcpConnectionRemAddressType,
          tcpConnectionRemAddress,
          tcpConnectionRemPort }

所有这些组件应该以任何有效索引结束。如果我带你的复制器并尝试反向转换,例如从符号索引(易于手工构建)到OID并返回:

oid = ObjectIdentity('TCP-MIB', 'tcpConnectionState', 
                     'ipv4', '10.0.0.11', 22,
                     'ipv4', '10.0.0.11', 1024).resolveWithMib(view)
print(oid)
mib_obj = ObjectIdentity(oid).resolveWithMib(view)
print(mib_obj.getMibSymbol())

我可以看到索引如何编码到OID中并证明pysnmp可以解码它们:

1.3.6.1.2.1.6.19.1.7.1.4.10.0.0.11.22.1.4.10.0.0.11.1024
('TCP-MIB', 'tcpConnectionState', 
 (InetAddressType('ipv4'), InetAddressIPv4(hexValue='0a00000b'), InetPortNumber(22),
  InetAddressType('ipv4'), InetAddressIPv4(hexValue='0a00000b'), InetPortNumber(1024)))