2016-08-15 156 views
1

我想接收snmp陷阱,我在snmp4j上得到它,但現在我正在使用westhawk snmp堆棧庫來實現陷阱接收器模塊。我使用此庫sample來接收陷阱,但是此代碼接收作爲rawPdu陷阱,當我編輯代碼沒有收到任何東西時,我該怎麼辦? 這裏是我的代碼:接收陷阱與westhawk snmp堆棧

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

基本上可以通過陷阱PDU迭代變量綁定使用下面的代碼:

varbind[] varbinds = trapPdu.getResponseVarbinds(); 

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

我的問題是代碼沒有收到任何事件作爲trapEvent,你收到的陷阱? –

+0

確保您已爲所有SNMP版本配置上下文 - v1,v2c,v3。因此,您可以處理所有類型的傳入陷阱PDU。 –

+0

我現在修復了,可惜沒有任何改變。我現在應該怎麼做 ? –