使用SNMP4J接收Trap示例

    SNMP4J文檔中給的example不完整,在網上找了一天也沒有找到一個好的例子,現在自己終於摸索出來了,就給大家做個參考吧。使用SNMP4JV2.0.2測試正常。

  1. /* 發送端使用net-snmp,命令如下: 
  2.          * snmptrap -e 0x0102030405 -v 3 -u myuser -a MD5 -A mypassword -l authNoPriv 218.195.60.1 42 .1.3.6.1.6.3.1.1.5.4 .1.3.6.1.2.1.2.2.1.1 i 7 .1.3.6.1.2.1.2.2.1.7 i 1 .1.3.6.1.2.1.2.2.1.8 i 1 
  3.          */ 

 

  1.  
  2. public class TrapDeamon implements CommandResponder 
  3.     private Snmp snmp; 
  4.  
  5.     public void init() throws Exception 
  6.     {    
  7.  //create a threadpool to process the traps     
  8.         ThreadPool threadPool = ThreadPool.create("TrapDeamon"3); 
  9.         MultiThreadedMessageDispatcher dispatcher = new MultiThreadedMessageDispatcher(threadPool,  new MessageDispatcherImpl()); 
  10.         String nsap = "udp:" + InetAddress.getLocalHost().getHostAddress() + "/162"
  11.         Address listenAddress = GenericAddress.parse(System.getProperty("snmp4j.listenAddress", nsap)); 
  12.         TransportMapping transport = null
  13.         // tcp or udp  
  14.         if (listenAddress instanceof UdpAddress)  
  15.         {   
  16.             transport = new DefaultUdpTransportMapping((UdpAddress) listenAddress);   
  17.         }  
  18.         else  
  19.         {   
  20.             transport = new DefaultTcpTransportMapping((TcpAddress) listenAddress);   
  21.         }   
  22.         //construct a "snmp" instance to handle the snmp massage 
  23.         snmp = new Snmp(dispatcher, transport); 
  24.         //add msg handling module 
  25.         snmp.getMessageDispatcher().addMessageProcessingModel(new MPv1());   
  26.         snmp.getMessageDispatcher().addMessageProcessingModel(new MPv2c());   
  27.         snmp.getMessageDispatcher().addMessageProcessingModel(new MPv3());   
  28.          
  29.         //add the enginID in the trap 
  30.         OctetString engineID = new OctetString(MPv3.createLocalEngineID()); 
  31.        //create and add the userSecurityModel 
  32.         USM usm = new USM(SecurityProtocols.getInstance(),engineID, 0);   
  33.         SecurityModels.getInstance().addSecurityModel(usm);   
  34.          
  35.         //add the securityProtocols,you can skip it if your users are noAuthNoPriv 
  36.         SecurityProtocols.getInstance().addDefaultProtocols();         
  37.          
  38.          
  39.         //create and add the user 
  40.         OctetString userName = new OctetString("myuser"); 
  41.         OctetString password = new OctetString ("mypassword");        
  42.         UsmUser usmUser = new UsmUser(userName, AuthMD5.ID, password, nullnull);         
  43.         snmp.getUSM().addUser(usmUser); 
  44.         
  45. //add other users here...
  46.          
  47.         snmp.listen();       
  48.         snmp.addCommandResponder(this); 
  49.         System.out.println("Listening.."); 
  50.         
  51.     } 
  52.      
  53.     public void processPdu(CommandResponderEvent respEvnt)  
  54.     {        
  55.         String peerAddr = respEvnt.getPeerAddress().toString(); 
  56.         try  
  57.         { 
  58.             String [] remoteAddr = peerAddr.split("/"); 
  59.             String ip = remoteAddr[0]; 
  60.              
  61.             if (respEvnt != null && respEvnt.getPDU() != null)  
  62.             {   
  63.                 PDU pdu = respEvnt.getPDU(); 
  64.                 switch (pdu.getType())  
  65.                 { 
  66.                     case -89://0xA7 represents V2,V3Trap                     
  67.                         parsePduV2(ip, pdu); 
  68.                         break
  69.                     case -92://0xA4 represents V1Trap    
  70.                         parsePduV1(ip, pdu); 
  71.                         break
  72.                     default://not a Trap,ignore it                       
  73.                     break
  74.                 } 
  75.             } 
  76.         } 
  77.         catch (Exception e)  
  78.         { 
  79.             System.out.println("Error occured in processPdu() of " + peerAddr); 
  80.             System.out.println(e.getMessage()); 
  81.             e.printStackTrace(); 
  82.         } 
  83.     } 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章