snmp类是SNMP4J的核心,它提供了发送和接收SNMP PDUs的方法,所有的SNMP PDU 类型都可以采用同步或者异步的方式被发送。
Snmp采用独立的传输协议,通过TransportMapping 接口调用addTransportMapping(TransportMapping transportMapping) 方法或者采用默认的构造函数来实现传输映射,以此来实现信息的传输。
下面的代码片段是采用UDP传输方式来实现一个SNMPv3的SNMP session :
Java代码
1. Address targetAddress = GenericAddress.parse("udp:127.0.0.1/161");
2. TransportMapping transport = new DefaultUdpTransportMapping();
3. snmp = new Snmp(transport);
4. USM usm = new USM(SecurityProtocols.getInstance(),
5. new OctetString(MPv3.createLocalEngineID()), 0);
6. SecurityModels.getInstance().addSecurityModel(usm);
7. transport.listen();
如何实现SNMPv3信息的同步发送,下面举例说明:
Java代码
1. // add user to the USM
2. snmp.getUSM().addUser(new OctetString("MD5DES"),
3. new UsmUser(new OctetString("MD5DES"),
4. AuthMD5.ID,
5. new OctetString("MD5DESUserAuthPassword"),
6. PrivDES.ID,
7. new OctetString("MD5DESUserPrivPassword")));
8. // create the target
9. UserTarget target = new UserTarget();
10. target.setAddress(targetAddress);
11. target.setRetries(1);
12. target.setTimeout(5000);
13. target.setVersion(SnmpConstants.version3);
14. target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
15. target.setSecurityName(new OctetString("MD5DES"));
16.
17. // create the PDU
18. PDU pdu = new ScopedPDU();
19. pdu.add(new VariableBinding(new OID("1.3.6")));
20. pdu.setType(PDU.GETNEXT);
21.
22. // send the PDU
23. ResponseEvent response = snmp.send(pdu, target);
24. // extract the response PDU (could be null if timed out)
25. PDU responsePDU = response.getResponse();
26. // extract the address used by the agent to send the response:
27. Address peerAddress = response.getPeerAddress();
28. An asynchronous SNMPv1 request is sent by the following code:
29.
30. // setting up target
31. CommunityTarget target = new CommunityTarget();
32. target.setCommunity(new OctetString("public"));
33. target.setAddress(targetAddress);
34. target.setRetries(2);
35. target.setTimeout(1500);
36. target.setVersion(SnmpConstants.version1);
37. // creating PDU
38. PDU pdu = new PDU();
39. pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,1})));
40. pdu.add(new VariableBinding(new OID(new int[] {1,3,6,1,2,1,1,2})));
41. pdu.setType(PDU.GETNEXT);
42. // sending request
43. ResponseListener listener = new ResponseListener() {
44. public void onResponse(ResponseEvent event) {
45. // Always cancel async request when response has been received
46. // otherwise a memory leak is created! Not canceling a request
47. // immediately can be useful when sending a request to a broadcast
48. // address.
49. ((Snmp)event.getSource()).cancel(event.getRequest(), this);
50. System.out.println("Received response PDU is: "+event.getResponse());
51. }
52. };
53. snmp.sendPDU(pdu, target, null, listener);
54.
55. //Traps (notifications) and other SNMP PDUs can be received by adding the folling code to the first code snippet above:
56. CommandResponder trapPrinter = new CommandResponder() {
57. public synchronized void processPdu(CommandResponderEvent e) {
58. PDU command = e.getPDU();
59. if (command != null) {
60. System.out.println(command.toString());
61. }
62. }
63. };
64. snmp.addCommandResponder(trapPrinter);
65.
66.
67.
68. Version:
69. 1.8
70. Author:
71. Frank Fock
72. 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协议了。