使用snmp4j实现Snmp功能(二)

前一篇文章讲了如何用snmp4j实现set和get的功能,今天讲如何接收trap。

snmp4j提供了一个抽象类CommandResponder 类用于接收trap,这个类里面有一个必须实现 的方法processPdu() ,当接收到trap,会自动进入这个方法,因此我们可以将对trap的处理写在这里。

  1. private  TransportMapping transport =  null ;  
  2.   
  3.   
  4.        public   void  initComm()  throws  IOException {  
  5.   
  6.   
  7.               // 设置Agent方的IP和端口   
  8.   
  9.               targetAddress = GenericAddress.parse("udp:192.168.1.1/161" );  
  10.   
  11.               // 设置接收trap的IP和端口   
  12.   
  13.               transport = new  DefaultUdpTransportMapping( new  UdpAddress(  
  14.   
  15.                             "192.168.1.2/162" ));  
  16.   
  17.               snmp = new  Snmp(transport);  
  18.   
  19.   
  20.               CommandResponder trapRec = new  CommandResponder() {  
  21.   
  22.                      public   synchronized   void  processPdu(CommandResponderEvent e) {  
  23.   
  24.                             // 接收trap   
  25.   
  26.                             PDU command = e.getPDU();  
  27.   
  28.                             if  (command !=  null ) {  
  29.   
  30.                                    System.out.println(command.toString());  
  31.   
  32.                             }  
  33.   
  34.                      }  
  35.   
  36.               };  
  37.   
  38.               snmp.addCommandResponder(trapRec);  
  39.   
  40.   
  41.               transport.listen();  
  42.   
  43.        }  

其中targetAddress指Agent端也就是trap发 送, transport 指trap接收方,这里就是本机,假设IP是192.168.1.2,但注意不能写成127.0.0.1。

因为我们无法得知trap什么时候会发送,所以需要有一个线程等待trap的到来,在这个例子中我们使用wait()来等待trap的到来,具体应 用中就要根据实际情况来做了。

  1. public   synchronized   void  listen() {  
  2.   
  3.               System.out.println("Waiting for traps.." );  
  4.   
  5.               try  {  
  6.   
  7.                      this .wait(); //Wait for traps to come in   
  8.   
  9.               } catch  (InterruptedException ex) {  
  10.   
  11.                      System.out.println("Interrupted while waiting for traps: "  + ex);  
  12.   
  13.                      System.exit(-1 );  
  14.   
  15.               }  
  16.   
  17.        }  
  18.   
  19.          
  20.   
  21.        public   static   void  main(String[] args) {  
  22.   
  23.               try  {  
  24.   
  25.                      SnmpUtil util = new  SnmpUtil();  
  26.   
  27.                      util.initComm();  
  28.   
  29.                      util.listen();  
  30.   
  31.               } catch  (IOException e) {  
  32.   
  33.                      e.printStackTrace();  
  34.   
  35.               }  
  36.   
  37.        }  

将上面的代码添加到原来的例子中,就可以接收trap了。

但是还有一个问题,如何让192.168.1.1发送trap呢?这个也可以使用snmp4j来做。其实发送trap和发送set、get PDU是类似的,同样是发送PDU,只不过类型不一样。我们把前面的例子复制到192.168.1.1,在里面添加一段代码:

  1. public   void  setTrap()  throws  IOException {  
  2.   
  3.         // 构造Trap PDU   
  4.   
  5.         PDU pdu = new  PDU();  
  6.   
  7.         pdu.add(new  VariableBinding( new  OID( ".1.3.6.1.2.3377.10.1.1.1.1" ),  
  8.   
  9.                       new  OctetString( "SnmpTrap" )));  
  10.   
  11.         pdu.setType(PDU.TRAP);  
  12.   
  13.         sendPDU(pdu);  
  14.   
  15.         System.out.println("Trap sent successfully." );  
  16.   
  17.     }  

这里PDU的OID和Value可以自己构造,无需使用特定的值。

然后修改地址
targetAddress = GenericAddress.parse ( "udp:192.168.1.2/162" );
transport = new DefaultUdpTransportMapping();

另外需要修改target的version,即改为target.setVersion(SnmpConstants. version2c ) 为什么要这样改我 也没搞清楚,总之verion1收不到。

接下来修改main()函数,调用setTrap()。

然后回到本机运行刚才的例子,当控制台显示“Waiting for traps..”时,运行Agent端的例子。此时如果192.168.1.2打出我们刚刚设置的PDU的信息,就说明Trap的收发成功了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值