pysnmp陷阱接收器中的ValueConstraintError

时间:2017-04-11 07:27:38

标签: python pysnmp

我使用此示例(取自here)来接收带有var SSH = require('simple-ssh'); var ssh = new SSH({ host: 'somehost', user: 'username', pass: 'password' }); ssh.exec('screen rtsp2feed 3 3 rtsp://server:port > log.txt', { out: function(stdout) { console.log(stdout); } }).start(); 的SNMP陷阱:

pysnmp

对于某些陷阱,存在以下范围约束错误:

from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher
from pysnmp.carrier.asynsock.dgram import udp, udp6
from pyasn1.codec.ber import decoder
from pysnmp.proto import api

def cbFun(transportDispatcher, transportDomain, transportAddress, wholeMsg):
    while wholeMsg:
        msgVer = int(api.decodeMessageVersion(wholeMsg))
        if msgVer in api.protoModules:
            pMod = api.protoModules[msgVer]
        else:
            print('Unsupported SNMP version %s' % msgVer)
            return
        reqMsg, wholeMsg = decoder.decode(
            wholeMsg, asn1Spec=pMod.Message(),
            )
        print('Notification message from %s:%s: ' % (
            transportDomain, transportAddress
            )
        )
        reqPDU = pMod.apiMessage.getPDU(reqMsg)
        if reqPDU.isSameTypeWith(pMod.TrapPDU()):
            if msgVer == api.protoVersion1:
                print('Enterprise: %s' % (
                    pMod.apiTrapPDU.getEnterprise(reqPDU).prettyPrint()
                    )
                )
                print('Agent Address: %s' % (
                    pMod.apiTrapPDU.getAgentAddr(reqPDU).prettyPrint()
                    )
                )
                print('Generic Trap: %s' % (
                    pMod.apiTrapPDU.getGenericTrap(reqPDU).prettyPrint()
                    )
                )
                print('Specific Trap: %s' % (
                    pMod.apiTrapPDU.getSpecificTrap(reqPDU).prettyPrint()
                    )
                )
                print('Uptime: %s' % (
                    pMod.apiTrapPDU.getTimeStamp(reqPDU).prettyPrint()
                    )
                )
                varBinds = pMod.apiTrapPDU.getVarBindList(reqPDU)
            else:
                varBinds = pMod.apiPDU.getVarBindList(reqPDU)
            print('Var-binds:')
            for oid, val in varBinds:
                print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
    return wholeMsg

transportDispatcher = AsynsockDispatcher()

transportDispatcher.registerRecvCbFun(cbFun)

# UDP/IPv4
transportDispatcher.registerTransport(
    udp.domainName, udp.UdpSocketTransport().openServerMode(('localhost', 162))
)

# UDP/IPv6
transportDispatcher.registerTransport(
    udp6.domainName, udp6.Udp6SocketTransport().openServerMode(('::1', 162))
)

transportDispatcher.jobStarted(1)

try:
    # Dispatcher will never finish as job#1 never reaches zero
    transportDispatcher.runDispatcher()
except:
    transportDispatcher.closeDispatcher()
    raise

有办法处理吗?

1 个答案:

答案 0 :(得分:1)

我在写这个问题时找到了答案 - FAQ中描述了类似的情况。它似乎是供应商编码中的一个错误。针对ValueConstraintError: ConstraintsIntersection(ConstraintsIntersection(), ValueRangeConstraint(0, 4294967295L)) failed at: "ValueRangeConstraint(0, 4294967295L) failed at: "-8249051"" at TimeTicks 类型提供了一种解决方案,可将负值转换为正值:

Counter

此hack也可以应用于from pysnmp.proto import rfc1155, rfc1902, api from pyasn1.codec.ber import encoder, decoder # --- hack Counter type def counterCloneHack(self, *args): if args and args[0] < 0: args = (0xffffffff+args[0]-1,) + args[1:] return self.__class__(*args) rfc1155.Counter.clone = counterCloneHack rfc1902.Counter32.clone = counterCloneHack

TimeTicks