Chinaunix首页 | 论坛 | 博客
  • 博客访问: 401351
  • 博文数量: 101
  • 博客积分: 2324
  • 博客等级: 大尉
  • 技术积分: 887
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-19 19:28
文章分类

全部博文(101)

文章存档

2012年(3)

2011年(60)

2010年(38)

分类: 系统运维

2010-11-23 15:39:49

前两篇文章讲了如何使用Snmp4j实现Set、Get(使用snmp4j实现Snmp功能(一))以及发送、接收Trap(使用snmp4j实现Snmp功能(二)) 功能。
在我们前面的实现中,如果访问MIB库中不存在的OID,Get方式的话,我们会得到一个Null值,而Set方式时Agent端会把我们发过去的PDU原封不动的返回回来。当然多数情况下这不是我们想要的结果,所以今天我们讲一下如何设置Agent端的Response。
这个功能其实和接收发送Trap是一样的,只不过对象变了一下而已。同样,接收Set和Get的方法写在CommandResponder的processPdu()中。我们把前面写过的initComm() 的processPdu()中添加一段代码(Agent端):
view plaincopy to clipboardprint?
// 设置Response  
 
       if ((command.getType() != PDU.TRAP)  
 
                     && (command.getType() != PDU.V1TRAP)  
 
                     && (command.getType() != PDU.REPORT)  
 
                     && (command.getType() != PDU.RESPONSE)) {  
 
              command.setErrorIndex(0);  
 
              command.setErrorStatus(0);  
 
              command.setType(PDU.RESPONSE);  
 
              // 设置PDU的Value  
 
              command.get(0).setVariable(new OctetString("MYSNMP"));  
 
   
 
              StatusInformation statusInformation = new StatusInformation();  
 
              StateReference ref = e.getStateReference();  
 
              try {  
 
                     System.out.println("send Response!");  
 
                     e.getMessageDispatcher().returnResponsePdu(  
 
                                   e.getMessageProcessingModel(),  
 
                                   e.getSecurityModel(), e.getSecurityName(),  
 
                                   e.getSecurityLevel(), command,  
 
                                   e.getMaxSizeResponsePDU(), ref,  
 
                                   statusInformation);  
 
              } catch (MessageException ex) {  
 
                     System.err.println("Error while sending response: " 
 
                                   + ex.getMessage());  
 
              }  
 
       } 
// 设置Response
       if ((command.getType() != PDU.TRAP)
                     && (command.getType() != PDU.V1TRAP)
                     && (command.getType() != PDU.REPORT)
                     && (command.getType() != PDU.RESPONSE)) {
              command.setErrorIndex(0);
              command.setErrorStatus(0);
              command.setType(PDU.RESPONSE);
              // 设置PDU的Value
              command.get(0).setVariable(new OctetString("MYSNMP"));
 
              StatusInformation statusInformation = new StatusInformation();
              StateReference ref = e.getStateReference();
              try {
                     System.out.println("send Response!");
                     e.getMessageDispatcher().returnResponsePdu(
                                   e.getMessageProcessingModel(),
                                   e.getSecurityModel(), e.getSecurityName(),
                                   e.getSecurityLevel(), command,
                                   e.getMaxSizeResponsePDU(), ref,
                                   statusInformation);
              } catch (MessageException ex) {
                     System.err.println("Error while sending response: "
                                   + ex.getMessage());
              }
       }
 

接下来让Agent端的main()函数调用listen(),运行main()函数,Agent端开始监听来自Manager的Set和Get。
回到本机,向Agent端发送一个Set的PDU或Get的PDU,如果控制台打出我们刚刚在程序中设置的值"MYSNMP",说明我们的Response设置成功啦!
当然,程序中的设置PDU的Value是一个最简单的Response示例,在实际的应用中,应该要根据不同的OID返回不同的Value,这一部分的代码要根据实际的应用去发挥啦。
 
 
阅读(1457) | 评论(2) | 转发(1) |
给主人留下些什么吧!~~

chinaunix网友2010-11-24 13:15:02

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com

chinaunix网友2010-11-24 13:14:52

很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com