接收与westhawk snmp堆栈的陷阱

时间:2016-08-15 08:07:18

标签: java snmp

我想接收snmp陷阱,我在snmp4j上得到它,但现在我使用westhawk snmp堆栈库来实现陷阱接收器模块。我使用这个库sample来接收陷阱,但是此代码接收陷阱为rawPdu,当我编辑代码什么也没收到时,我该怎么办? 这是我的代码:

package snmp4j;

import java.util.*;
import uk.co.westhawk.snmp.stack.*;    
import uk.co.westhawk.snmp.event.*;    
import uk.co.westhawk.snmp.util.*;    

public class SnmpTrackTrapReceiver implements TrapListener, Observer
{
    private SnmpContext context;

public void init () 
{

    String host = "localhost";
    int port = 162 ;
    String community = "public" ;
    try 
    {
        context = new SnmpContextv2c(host, port);
        context.setCommunity(community);
        context.addTrapListener(this);
    }
    catch (java.io.IOException exc)
    {
        System.out.println("ReceiveTrap.init(): IOException " 
            + exc.getMessage() + "\n" + exc);
        exc.printStackTrace() ;
        System.exit(0);
    }
}

public void trapReceived(TrapEvent evt)
{
    int port = evt.getHostPort();
    Pdu trapPdu = evt.getPdu();
    int reqId = trapPdu.getReqId();
    SnmpContextBasisFace rcontext = trapPdu.getContext();
    int version = rcontext.getVersion();
    String host = rcontext.getHost();

    System.out.println("\ttrap id " + reqId 
        + ", v " + SnmpUtilities.getSnmpVersionString(version)
        + " from host " + host
        + ", sent from port " + port);
    System.out.println("\ttrap " + trapPdu.toString());
}
}

0 个答案:

没有答案