文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>使用snmp4j实现Snmp功能(二)

使用snmp4j实现Snmp功能(二)

时间:2010-11-23  来源:zhengdong1987

前一篇文章讲了如何用snmp4j实现set和get的功能,今天讲如何接收trap。 snmp4j提供了一个抽象类CommandResponder类用于接收trap,这个类里面有一个必须实现的方法processPdu(),当接收到trap,会自动进入这个方法,因此我们可以将对trap的处理写在这里。 下面修改上篇文章例子中的initComm()方法: view plaincopy to clipboardprint?
private TransportMapping transport = null;  
 
 
 
      public void initComm() throws IOException {  
 
 
 
             // 设置Agent方的IP和端口  
 
             targetAddress = GenericAddress.parse("udp:192.168.1.1/161");  
 
             // 设置接收trap的IP和端口  
 
             transport = new DefaultUdpTransportMapping(new UdpAddress(  
 
                           "192.168.1.2/162"));  
 
             snmp = new Snmp(transport);  
 
 
 
             CommandResponder trapRec = new CommandResponder() {  
 
                    public synchronized void processPdu(CommandResponderEvent e) {  
 
                           // 接收trap  
 
                           PDU command = e.getPDU();  
 
                           if (command != null) {  
 
                                  System.out.println(command.toString());  
 
                           }  
 
                    }  
 
             };  
 
             snmp.addCommandResponder(trapRec);  
 
 
 
             transport.listen();  
 
      } 
 private TransportMapping transport = null;
         public void initComm() throws IOException {                 // 设置Agent方的IP和端口               targetAddress = GenericAddress.parse("udp:192.168.1.1/161");               // 设置接收trap的IP和端口               transport = new DefaultUdpTransportMapping(new UdpAddress(                             "192.168.1.2/162"));               snmp = new Snmp(transport);                 CommandResponder trapRec = new CommandResponder() {                      public synchronized void processPdu(CommandResponderEvent e) {                             // 接收trap                             PDU command = e.getPDU();                             if (command != null) {                                    System.out.println(command.toString());                             }                      }               };               snmp.addCommandResponder(trapRec);                 transport.listen();        }
 
其中targetAddress指Agent端也就是trap发送,transport指trap接收方,这里就是本机,假设IP是192.168.1.2,但注意不能写成127.0.0.1。 因为我们无法得知trap什么时候会发送,所以需要有一个线程等待trap的到来,在这个例子中我们使用wait()来等待trap的到来,具体应用中就要根据实际情况来做了。
view plaincopy to clipboardprint?
public synchronized void listen() {  
 
             System.out.println("Waiting for traps..");  
 
             try {  
 
                    this.wait();//Wait for traps to come in  
 
             } catch (InterruptedException ex) {  
 
                    System.out.println("Interrupted while waiting for traps: " + ex);  
 
                    System.exit(-1);  
 
             }  
 
      }  
 
        
 
      public static void main(String[] args) {  
 
             try {  
 
                    SnmpUtil util = new SnmpUtil();  
 
                    util.initComm();  
 
                    util.listen();  
 
             } catch (IOException e) {  
 
                    e.printStackTrace();  
 
             }  
 
      } 
 public synchronized void listen() {
              System.out.println("Waiting for traps..");               try {                      this.wait();//Wait for traps to come in               } catch (InterruptedException ex) {                      System.out.println("Interrupted while waiting for traps: " + ex);                      System.exit(-1);               }        }               public static void main(String[] args) {               try {                      SnmpUtil util = new SnmpUtil();                      util.initComm();                      util.listen();               } catch (IOException e) {                      e.printStackTrace();               }        }
 
将上面的代码添加到原来的例子中,就可以接收trap了。 但是还有一个问题,如何让192.168.1.1发送trap呢?这个也可以使用snmp4j来做。其实发送trap和发送set、get PDU是类似的,同样是发送PDU,只不过类型不一样。我们把前面的例子复制到192.168.1.1,在里面添加一段代码: view plaincopy to clipboardprint?
public void setTrap() throws IOException {  
 
       // 构造Trap PDU  
 
       PDU pdu = new PDU();  
 
       pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.1"),  
 
                     new OctetString("SnmpTrap")));  
 
       pdu.setType(PDU.TRAP);  
 
       sendPDU(pdu);  
 
       System.out.println("Trap sent successfully.");  
 
   } 
 public void setTrap() throws IOException {
        // 构造Trap PDU         PDU pdu = new PDU();         pdu.add(new VariableBinding(new OID(".1.3.6.1.2.3377.10.1.1.1.1"),                       new OctetString("SnmpTrap")));         pdu.setType(PDU.TRAP);         sendPDU(pdu);         System.out.println("Trap sent successfully.");     }
 
这里PDU的OID和Value可以自己构造,无需使用特定的值。 然后修改地址
targetAddress = GenericAddress.parse("udp:192.168.1.2/162");
transport = new DefaultUdpTransportMapping(new UdpAddress("192.168.1.1/161"));
另外需要修改target的version,即改为target.setVersion(SnmpConstants.version2c)为什么要这样改我也没搞清楚,总之verion1收不到。 接下来修改main()函数,调用setTrap()。 然后回到本机运行刚才的例子,当控制台显示“Waiting for traps..”时,运行Agent端的例子。此时如果192.168.1.2打出我们刚刚设置的PDU的信息,就说明Trap的收发成功了。
 
相关阅读 更多 +
排行榜 更多 +
夕鸟

夕鸟

生活实用 下载
partyplay

partyplay

聊天通讯 下载
婚礼纪

婚礼纪

生活实用 下载