pysnmp-make代理端用于发送所需的陷阱

时间:2018-05-20 12:45:53

标签: agent pysnmp snmp-trap

我已经编写了陷阱接收器程序以获取陷阱但现在我想为我的服务器编写自定义代理程序,将陷阱发送到陷阱接收器
但是样本太差了,对我帮不了多少。样本是:

from pysnmp.carrier.asyncore.dispatch import AsyncoreDispatcher
from pysnmp.carrier.asyncore.dgram import udp, udp6, unix
from pyasn1.codec.ber import decoder
from pysnmp.proto import api


# noinspection PyUnusedLocal
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.getVarBinds(reqPDU)
        else:
            varBinds = pMod.apiPDU.getVarBinds(reqPDU)
        print('Var-binds:')
        for oid, val in varBinds:
            print('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
return wholeMsg


transportDispatcher = AsyncoreDispatcher()

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))
)

## Local domain socket
# transportDispatcher.registerTransport(
#    unix.domainName, unix.UnixSocketTransport().openServerMode('/tmp/snmp-manager')
# )

transportDispatcher.jobStarted(1)

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

我需要我的代理发送正常运行时间,临时值,CPU使用率,RAM使用率等

0 个答案:

没有答案