CommSnmpGet.java
6.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.sitech.ismp.snmp;
import java.io.IOException;
import java.util.HashMap;
import java.util.Vector;
import org.apache.log4j.Logger;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.GenericAddress;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;
public class CommSnmpGet {
private static final int RETRIES = 2;
private static final int TIME_OUT = 20000;
private Logger logger = Logger.getLogger("COLL");
String ip_s = "";
int port_s = 0;
String community_s = "public";
org.snmp4j.Snmp mSnmp = null;
CommunityTarget mCommunityTarget = null;
String rootOid = "";
HashMap walkHashMap;
public HashMap getWalkHashMap() {
return walkHashMap;
}
public String getRootOid() {
return rootOid;
}
public void setRootOid(String rootOid) {
this.rootOid = rootOid;
}
/**
* 实例化类
*
* @param ip
* 采集IP地址
* @param port
* 采集端口
*/
public CommSnmpGet(String ip, int port, String community) {
ip_s = ip;
port_s = port;
community_s = community;
walkHashMap = new HashMap();
}
/**
* 初始化SNMP
*
*/
public void init() {
try {
String addr = "udp:" + ip_s + "/" + String.valueOf(port_s);
OctetString community = new OctetString(community_s);
Address address = GenericAddress.parse(addr);
mCommunityTarget = new CommunityTarget(address, community);
org.snmp4j.TransportMapping vTransport = new org.snmp4j.transport.DefaultUdpTransportMapping();
mSnmp = new org.snmp4j.Snmp(vTransport);
vTransport.listen();
mCommunityTarget.setCommunity(community);
mCommunityTarget.setAddress(new UdpAddress(java.net.InetAddress
.getByName(ip_s), port_s));
mCommunityTarget.setRetries(RETRIES);
mCommunityTarget.setTimeout(TIME_OUT);
// mCommunityTarget.setMaxSizeRequestPDU(500);
mCommunityTarget.setVersion(org.snmp4j.mp.SnmpConstants.version2c);
} catch (Exception e) {
e.printStackTrace();
}
}
public void workTable(String curr_oid) throws IOException {
if (curr_oid.startsWith(rootOid)) {
PDU pdu_result = this.getNext(curr_oid);
if (pdu_result == null) {
return;
}
VariableBinding vb = pdu_result.get(0);
Variable vars = vb.getVariable();
OID oid = vb.getOid();
String nextVars = vars.toString();
String nextOid = oid.toString();
if (nextOid.startsWith(rootOid)) {
walkHashMap.put(nextOid, nextVars);
System.out.println(nextOid + "=" + nextVars);
}
workTable(nextOid);
}
}
/**
* SNMP是否有应答
*
* @return
*/
public boolean isAdoptable() {
boolean result = false;
String oid = "1.3.6.1.2.1.1.1.0";
PDU pdu = null;
try {
pdu = this.getPdu(oid);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (pdu != null) {
result = true;
}
return result;
}
public boolean isSwitch() {
boolean result = false;
String oid = "1.3.6.1.2.1.17.4.3.1.1";
PDU pdu = null;
try {
pdu = this.getNext(oid);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return result;
}
if (pdu != null) {
VariableBinding vb = pdu.get(0);
String s_oid = vb.getOid().toString();
if (s_oid.startsWith(oid)) {
result = true;
}
}
return result;
}
public void workTable(String rootOid, String curr_oid) throws IOException {
if (curr_oid.startsWith(rootOid)) {
PDU pdu_result = this.getNext(curr_oid);
VariableBinding vb = pdu_result.get(0);
Variable vars = vb.getVariable();
OID oid = vb.getOid();
String nextVars = vars.toString();
String nextOid = oid.toString();
System.out.println("[" + nextOid + "] = " + nextVars);
workTable(rootOid, nextOid);
}
}
public Vector getPDU(Vector oidVector) throws Exception {
PDU pdu = new PDU();
for (int i = 0; i < oidVector.size(); i++) {
// System.out.println("oid: " + (String)oidVector.get(i));
VariableBinding var = new VariableBinding(new OID(
(String) oidVector.get(i)),
new org.snmp4j.smi.OctetString());
pdu.add(var);
}
pdu.setType(PDU.GET);
ResponseEvent res = mSnmp.send(pdu, mCommunityTarget);
if (res.getResponse() == null) {
System.out.println("[can not connect ip address: " + ip_s + "]");
}
return res.getResponse().getVariableBindings();
}
public PDU getPdu(String oid) throws Exception {
PDU pdu = new PDU();
VariableBinding var = new VariableBinding(new OID(oid),
new org.snmp4j.smi.OctetString());
pdu.add(var);
pdu.setType(PDU.GET);
ResponseEvent res = mSnmp.send(pdu, mCommunityTarget);
if (res.getResponse() == null) {
logger.error("[can not connect ip address: " + ip_s + "]");
}
return res.getResponse();
}
/**
* 得到提供oid的下一个PDU
*
* @param oid
* @return PDU 协议数据单元
* @throws IOException
*/
public PDU getNext(String oid) throws IOException {
PDU pdu = new PDU();
VariableBinding var = new VariableBinding(new OID(oid),
new org.snmp4j.smi.OctetString());
pdu.add(var);
pdu.setType(PDU.GETNEXT);
ResponseEvent res = mSnmp.send(pdu, mCommunityTarget);
if (res.getResponse() == null) {
logger.error("[can not connect ip address: " + ip_s + "]");
}
return res.getResponse();
}
/**
* 断开SNMP连接
*
*/
public void release() {
try {
mSnmp.close();
} catch (Exception e) {
e.printStackTrace();
}
mSnmp = null;
}
public static void main(String args[]) throws Exception {
String ip = "";
String port = "";
String community = "";
String oid = "1.3.6.1.2.1.1.1.0";
if (args.length < 3) {
System.out
.println("Please Input Params <IpAddress> <Port> <Community> Or <IpAddress> <Port> <Community> <Oid>");
System.exit(0);
}
if (args.length > 3) {
if (args[3] != null && !(args[3].equals(""))) {
oid = args[3];
}
}
ip = args[0];
port = args[1];
community = args[2];
int int_port = Integer.parseInt(port);
CommSnmpGet snmpGet = new CommSnmpGet(ip, int_port, community);
snmpGet.init();
PDU pdu = snmpGet.getPdu(oid);
if (pdu == null) {
System.out.println("[Ip: " + ip + " can not connect ...]");
} else {
VariableBinding vb = pdu.get(0);
String s_oid = vb.getOid().toString();
String s_var = vb.getVariable().toString();
System.out.println("[---Collection Ip: " + ip + " Information---]");
System.out.println("[---" + s_oid + " = " + s_var + "---]");
System.out.println("[---Collection Success !---]");
}
snmpGet.release();
}
}