2012年(16)
分类:
2012-03-04 10:52:49
原文地址:Snmp4j编程简介之三:Snmp 作者:zhengdong1987
Snmp采用独立的传输协议,通过 接口调用 方法或者采用默认的构造函数来实现传输映射,以此来实现信息的传输。
下面的代码片段是采用UDP传输方式来实现一个SNMPv3的SNMP session :
Address targetAddress = GenericAddress.parse("udp:127.0.0.1/161"); TransportMapping transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport); USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); SecurityModels.getInstance().addSecurityModel(usm); transport.listen();
如何实现SNMPv3信息的同步发送,下面举例说明:
// add user to the USM snmp.getUSM().addUser(new OctetString("MD5DES"), new UsmUser(new OctetString("MD5DES"), AuthMD5.ID, new OctetString("MD5DESUserAuthPassword"), PrivDES.ID, new OctetString("MD5DESUserPrivPassword"))); // create the target UserTarget target = new UserTarget(); target.setAddress(targetAddress); target.setRetries(1); target.setTimeout(5000); target.setVersion(SnmpConstants.version3); target.setSecurityLevel(SecurityLevel.AUTH_PRIV); target.setSecurityName(new OctetString("MD5DES")); // create the PDU PDU pdu = new ScopedPDU(); pdu.add(new VariableBinding(new OID("1.3.6"))); pdu.setType(PDU.GETNEXT); // send the PDU ResponseEvent response = snmp.send(pdu, target); // extract the response PDU (could be null if timed out) PDU responsePDU = response.getResponse(); // extract the address used by the agent to send the response: Address peerAddress = response.getPeerAddress(); An asynchronous SNMPv1 request is sent by the following code: // setting up target CommunityTarget target = new CommunityTarget(); target.setCommunity(new OctetString("public")); target.setAddress(targetAddress); target.setRetries(2); target.setTimeout(1500); target.setVersion(SnmpConstants.version1); // creating PDU PDU pdu = new PDU(); pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,1}))); pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,2}))); pdu.setType(PDU.GETNEXT); // sending request ResponseListener listener = new ResponseListener() { public void onResponse(ResponseEvent event) { // Always cancel async request when response has been received // otherwise a memory leak is created! Not canceling a request // immediately can be useful when sending a request to a broadcast // address. ((Snmp)event.getSource()).cancel(event.getRequest(), this); System.out.println("Received response PDU is: "+event.getResponse()); } }; snmp.sendPDU(pdu, target, null, listener); //Traps (notifications) and other SNMP PDUs can be received by adding the folling code to the first code snippet above: CommandResponder trapPrinter = new CommandResponder() { public synchronized void processPdu(CommandResponderEvent e) { PDU command = e.getPDU(); if (command != null) { System.out.println(command.toString()); } } }; snmp.addCommandResponder(trapPrinter); Version: 1.8 Author: Frank Fock translate:avery_leo
Snmp类提供了一套有关Snmp的功能接口。具体来讲,就是发送、接受、创建Snmp消息。
一个Snmp对象是一个Session,而在Snmp4j中,一个Session可以同多个远程设备通信。
(1) Snmp、Target、PDU三者的关系
Target代表远程设备或者远程实体、PDU代表管理端同Target通信的数据,Snmp就代表管理者管理功能(其实就是数据的收发)的具体执行者。
打个比方:Target就是你远方的恋人,PDU就是你们之间传递的情书、而Snmp就是负责帮你寄信收信的邮差。
(2)Snmp收发数据的两种方式
Snmp可以同步、也可异步收发数据。详细见代码示例说明。
(3)Snmp与传输层协议
Snmp可以定制传输层协议,一般选择udp,也可以选择tcp。详细见代码示例说明。
(4)Snmp与Usm
创建Snmp用来发送Snmpv3版本的消息时候,一般还要创建USM,将它添加至安全模型管理器(SecriryModels)中,同时还需要向Usm中添加相应的USM用户(UsmUser)。详细见代码示例说明。
代码示例:(摘自Snmp4j的API文档)
(1)创建Snmp
1)使用UDP传输协议
TransportMapping transport = new DefaultUdpTransportMapping(); snmp = new Snmp(transport);
2)使用TCP传输协议
TransportMapping transport = new DefaultTcpTransportMapping(); snmp = new Snmp(transport);
3)创建用于Snmpv3的Snmp
// 创建Snmp TransportMapping transport = new DefaultUdpTransportMapping(); Snmp snmp = new Snmp(transport); if (version == SnmpConstants.version3) { byte[] localEngineID = ((MPv3)snmp.getMessageProcessingModel(MessageProcessingModel.MPv3)).createLocalEngineID(); // 创建USM USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(localEngineID), 0); // 将USM添加至安全模式管理器中 // 安全模型管理器采用了单例模式,它内部可以维护为3个安全模型,分别对应Snmp三个版本 SecurityModels.getInstance().addSecurityModel(usm); snmp.setLocalEngine(localEngineID, 0, 0); // 添加用户 snmp.getUSM().addUser(securityName,new UsmUser(securityName,authProtocol, authPassphrase,privProtocol,privPassphrase)); }
(2)同步收发消息
import org.snmp4j.*; ... Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); ... ResponseEvent response = snmp.send(requestPDU, target); if (response.getResponse() == null) { // request timed out ... }else { System.out.println("Received response from: "+ response.getPeerAddress()); // dump response PDU System.out.println(response.getResponse().toString()); }
(3)异步收发消息
import org.snmp4j.*; import org.snmp4j.event.*; ... Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); ... // 增加监听器 ResponseListener listener = new ResponseListener() { public void onResponse(ResponseEvent event) { PDU response = event.getResponse(); PDU request = event.getRequest(); if (response == null) { System.out.println("Request "+request+" timed out"); } else { System.out.println("Received response "+response+" on request "+ request); } }; snmp.sendPDU(request, target, null, listener); ...
(4)实现trap
实现trap需要三步:
1)创建Snmp;
2)对于listen()使处于网络监听(实际上是同于网络编程中的Socket监听);
3)实现CommandResponder
接口的监听器,并且调用Snmp.addCommandResponder(CommandResponder)
注册监听器。
import org.snmp4j.*;
import org.snmp4j.smi.*;
import org.snmp4j.mp.SnmpConstants;
...
TransportMapping transport =
new DefaultUdpTransportMapping(new UdpAddress("0.0.0.0/161"));
Snmp snmp = new Snmp(transport);
if (version == SnmpConstants.version3) {
byte[] localEngineID =
((MPv3)snmp.getMessageProcessingModel(MessageProcessingModel.MPv3)).createLocalEngineID();
USM usm = new USM(SecurityProtocols.getInstance(),
new OctetString(localEngineID), 0);
SecurityModels.getInstance().addSecurityModel(usm);
snmp.setLocalEngine(localEngineID, 0, 0);
// Add the configured user to the USM
...
}
// 注册命令响应监听器
snmp.addCommandResponder(this);
transport.listen();
...
// 实现CommandResponder接口
public synchronized void processPdu(CommandResponderEvent e) {
PDU command = e.getPdu();
if (command != null) {
...
}
}
总结
Snmp内含了一个消息分发器,消息分发器中内含了处理网络的线程,在使用完后最好调用close(),将其资源回归处理。
掌握了上面所说的三个概念,基本上可以使用Snmp4j编写Snmp的程序了。
有关Snmp4j编程最好也最详细的资料:API文档和源代码。关于使用Snmp4j编写Snmp程序的例子,多线程的例子可以参看源代码中:org.snmp4j.test包下的MultiThreadedTrapReceiver.java,完整的例子可以参看 org.snmp4j.tools.console包下的SnmpRequest(一个命令行的Snmp管理器)。
不过,要想快速和深入掌握Snmp编程,最好的办法一定是先弄懂Snmp协议,这方面的资料最权威的就是RFC协议了