使用 westhawk snmp 堆栈接收陷阱

receive trap with westhawk snmp stack

我想接收 snmp 陷阱,我在 snmp4j 上得到它,但现在我正在使用 westhawk snmp 堆栈库来实现陷阱接收器 module.I 使用这个库 sample 来接收陷阱,但是这个code recieve trap as 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());
}
}

基本上,您可以使用以下代码遍历陷阱 PDU 变量绑定:

varbind[] varbinds = trapPdu.getResponseVarbinds();

for (varbind var: varbinds) {
    System.out.println("OID: ", var.getOid().toString());
    System.out.println("Value: ", var.getValue().toString());
}