如何使用AdventNet接收snmpv3陷阱

时间:2018-02-28 16:10:42

标签: java snmp net-snmp snmp-trap

我试图添加选项来获取TRAP V3(我可以获得TRAP V1和V2)。

这是Session init

public void CreateSession()
{
    logger.debug(".in");        
    System.out.println("Waiting to receive traps .......");

    api = new SnmpAPI();
    api.setDebug( true );
    session = new SnmpSession(api);
    session.addSnmpClient(this);
    udpOpt = new UDPProtocolOptions();
    udpOpt.setLocalPort(TRAP_PORT);
    session.setProtocolOptions(udpOpt);
    try
    {
        session.open();
        String message="Succes to bind port: "+session.getLocalPort();
        logger.info(message);
        System.out.println(message);
    }
    catch (Exception ex)
    {
        String message = "Failed to open session - Port in use or permission denied. \n Message-  "+ ex.getMessage() + "\n Will exit from Trap process. ";
        logger.error(message, ex);
        System.err.println(message);
        throw new RuntimeException(message);
    }   
}

每次在V3中设置设备时,我都会使用USMUtils。init_v3_parameters方法执行发现并将其注册到表中。

 private void initV3Parameters(NEData neData) throws InterruptedException
{
    logger.debug("in.");
    try
    {
        UDPProtocolOptions udpOptions = new UDPProtocolOptions();
        udpOptions.setLocalPort(TRAP_PORT);
        udpOptions.setRemoteHost(neData.m_szIpAddress);
         USMUtils.init_v3_parameters(
                neData.usmUser,
                null,
                Integer.valueOf(neData.authProtocol),
                neData.authPassword,
                neData.privPassword,
                udpOptions,
                session,
                false,
                Integer.valueOf(neData.privProtocol));
        printToLog("success",neData);
    }
    catch (SnmpException exp) {
        logger.error(exp.getMessage()+" for ip = "+neData.m_szIpAddress,exp);
        discoveredDeque.put(neData);
        printToLog("failed",neData);
    }


}

当TRAP V1或V2到达时,它就会出现这个功能并且运行正常。但我想添加V3

/**
 * Receives incoming PDUs and adds them to the traps queue. Notifies the
 * internal thread on the arrival of each PDU.
 */
public boolean callback (SnmpSession session, SnmpPDU pdu, int requestID)
{
    logger.info("in session= "+session+" ,pdu= "+pdu+" ,requestID = "+requestID);
    if (pdu == null)
    {
        logger.info("Received null PDU");
    }
    else
    {
        // Add the PDU to the end of the traps queue.
        // Notify the internal thread.
        try
        {
            synchronized (this)
            {
                Object t = m_Traps.addElement(pdu);
                if (t != null)
                {
                    // The new trap replaced an old one.
                    logger.info("Queue full: replaced old PDU");
                }
                notify();
            }
        }
        catch (Exception ex)
        {
           logger.error(ex.getMessage(),ex);
        }
    }
    // All PDUs are handled, so true should always be returned.
    return true;
}

debugPrint实施了SnmpClient,似乎AdventNet获得了TRAP但却没有做任何事情......

SNMP代理使用NetSNMP实现。

让AdventNet处理V3 TRAP缺少什么?

这是正确的回调函数吗?

public boolean callback (SnmpSession session, SnmpPDU pdu, int requestID)

0 个答案:

没有答案