CollF5lbWithSNMP.java
17.1 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
package com.sitech.ismp.coll.loadBalance;
import com.sitech.ismp.coll.CollBase;
import com.sitech.ismp.coll.net.f5.InterfaceEntry;
import com.sitech.ismp.coll.net.f5.InterfaceEntryWalker;
import com.sitech.ismp.coll.net.f5.MultiHostCpuEntry;
import com.sitech.ismp.coll.net.f5.MultiHostCpuEntryWalker;
import com.sitech.ismp.coll.net.f5.VirtualServEntry;
import com.sitech.ismp.coll.net.f5.VirtualServEntryWalker;
import com.sitech.ismp.coll.net.f5.PoolMemberEntry;
import com.sitech.ismp.coll.net.f5.PoolMemberEntryWalker;
import com.sitech.util.DES3;
import com.sitech.util.Formater;
import com.sitech.util.SNMPClient;
import org.apache.log4j.Logger;
import org.snmp4j.mp.SnmpConstants;
import java.util.HashMap;
import java.util.Vector;
/**
* Created by mooker on 15/8/3.
*/
public class CollF5lbWithSNMP {
private Logger logger = Logger.getLogger("COLL");
private String PRE_UNITID = "10-19-10";
private String ipAddr;
private String deviceName;
private int snmpPort;
private int snmpVersion;
private String snmpCommunity;
private boolean isConn = false;
private boolean isSupportSnmp = false;
private String serurityName;
private String authenticationPassphrase;
private String privacyPassphrase;
private String authProtocal;
private String privProtocal;
private String securityLevel;
private SNMPClient client;
private String sysObjectID;
private void init(HashMap<String, String> params) throws Exception {
ipAddr = params.get("IPADDR");
deviceName = Formater.neatenunitid(params.get("IPADDR"));
// deviceName = Formater.neatenunitid(params.get("DEVICEID"));
try {
snmpCommunity = DES3.decrypt(params.get("COMMUNITY"));
} catch (Exception e) {
throw new Exception("Wrong SNMP COMMUNITY:" + DES3.decrypt(params.get("COMMUNITY")));
}
try {
snmpPort = Integer.parseInt(params.get("SNMPPORT"));
} catch (Exception e) {
throw new Exception("Wrong SNMP PORT:" + params.get("SNMPPORT"));
}
String versionStr = params.get("SNMPVERSION");
if (versionStr != null && versionStr.toLowerCase().trim().equals("v1")) {
snmpVersion = SnmpConstants.version1;
} else if (versionStr != null
&& versionStr.toLowerCase().trim().equals("v2c")) {
snmpVersion = SnmpConstants.version2c;
} else if (versionStr != null
&& versionStr.toLowerCase().trim().equals("v3")) {
snmpVersion = SnmpConstants.version3;
} else {
// 默认按照v2c版本采集
snmpVersion = SnmpConstants.version2c;
}
if (SnmpConstants.version3 == snmpVersion) {
serurityName = params.get("securityName");
if (params.get("authenticationPassphrase") != null) {
authenticationPassphrase = DES3.decrypt(params.get("authenticationPassphrase"));
}
authProtocal = params.get("authenticationProtocol");
privProtocal = params.get("privacyProtocol");
if (params.get("privacyPassphrase") != null) {
privacyPassphrase = DES3.decrypt(params.get("privacyPassphrase"));
}
securityLevel = params.get("auth_priv_mode");
}
}
private void connect() throws Exception {
client = new SNMPClient(ipAddr, snmpPort, snmpCommunity, snmpVersion, securityLevel,
serurityName, authProtocal, authenticationPassphrase, privProtocal, privacyPassphrase);
isSupportSnmp = client.isOpenSNMP();
isConn = client.connect();
if (!isConn || !isSupportSnmp) {
throw new Exception("Device not support SNMP or Collect params is wrong " +
" or Network is error: " + ipAddr + ":" + snmpPort);
}
sysObjectID = client.get("1.3.6.1.2.1.1.2.0");
logger.info("sysObjectID is: " + sysObjectID);
}
public void destroy() {
if(client != null){
client.close();
}
}
public Vector getConfig(HashMap<String, String> params) {
logger.info("begin getConfig");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
connect();
String UNIT_ID = PRE_UNITID + "-10" + ":" + deviceName + "-total";
// CM-00-02-001-04 设备名称
collResult.addKPI(UNIT_ID, "CM-00-02-001-04", getSystemName());
// CM-01-04-001-02 设备IP地址
collResult.addKPI(UNIT_ID, "CM-01-04-001-02", this.ipAddr);
// CM-00-01-001-22 系统启动时间
collResult.addKPI(UNIT_ID, "CM-00-01-001-22", getDevSysUpTime());
// CM-00-02-001-03 设备软件版本
collResult.addKPI(UNIT_ID, "CM-00-02-001-03", getSoftVersion());
} catch (Exception e) {
logger.error("getConfig has error", e);
} finally {
destroy();
}
logger.info("end getConfig");
return collResult.getKPISet();
}
public Vector getIntefaces(HashMap<String, String> params) {
logger.info("begin getIntefaces");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
InterfaceEntryWalker ifTable = new InterfaceEntryWalker();
ifTable.FillTable(params.get("IPADDR"), Integer.parseInt(params.get("SNMPPORT")), DES3.decrypt(params.get("COMMUNITY")));
Vector ifStatRows = ifTable.getEntry();
// IfTable ifTable = new IfTable();
// ifTable.FillTable(params.get("IPADDR"), Integer.parseInt(params.get("SNMPPORT")), DES3.decrypt(params.get("COMMUNITY")));
// Vector ifStatRows = ifTable.getRrows();
for (int i = 0; i < ifStatRows.size(); i++) {
InterfaceEntry row = (InterfaceEntry) ifStatRows.get(i);
String ifName = row.getName();
String UNIT_ID = PRE_UNITID + "-11" + ":" + deviceName + "-" + Formater.neatenunitid(ifName) + "-interface";
// CM-00-02-002-01 端口名称
collResult.addKPI(UNIT_ID, "CM-00-02-002-01", ifName);
// PM-00-01-900-01 端口流量
Double bytesIO = (Double.valueOf(row.getBytesIn()) + Double.valueOf(row.getBytesOut())) / 1024.0 / 1024.0;
collResult.addKPI(UNIT_ID, "PM-00-01-900-01", Formater.formatDecimalByScale(String.valueOf(bytesIO), 2));
// PM-00-04-099-05 端口状态
collResult.addKPI(UNIT_ID, "PM-00-04-099-05", this.parsePauseActive(row.getPauseActive()));
}
} catch (Exception e) {
logger.error("getIntefaces has error", e);
} finally {
destroy();
}
logger.info("end getIntefaces");
return collResult.getKPISet();
}
public Vector getCpuMemConns(HashMap<String, String> params) {
logger.info("begin getCpuMemConns");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
connect();
MultiHostCpuEntryWalker mhceTable = new MultiHostCpuEntryWalker();
mhceTable.FillTable(params.get("IPADDR"), Integer.parseInt(params.get("SNMPPORT")), DES3.decrypt(params.get("COMMUNITY")));
Vector mhceRows = mhceTable.getEntry();
String UNIT_ID = PRE_UNITID + "-10" + ":" + deviceName + "-total";
int cpuRate = 0;
for (int i = 0; i < mhceRows.size(); i++) {
MultiHostCpuEntry row = (MultiHostCpuEntry) mhceRows.get(i);
cpuRate += Integer.parseInt(row.getCpuUsageRatio());
}
// PM-00-01-001-05 CPU利用率
collResult.addKPI(UNIT_ID, "PM-00-01-001-05", String.valueOf(cpuRate / mhceRows.size()));
String usedMem = this.getSysHostMemoryUsed();
String totalMem = this.getSysHostMemoryTotal();
Double dou = Double.valueOf(usedMem) / Double.valueOf(totalMem) * 100;
// PM-00-01-002-01 内存使用率
collResult.addKPI(UNIT_ID, "PM-00-01-002-01", Formater.formatDecimalByScale(String.valueOf(dou), 2));
// PM-23-00-001-04 客户端连接数
collResult.addKPI(UNIT_ID, "PM-23-00-001-04", this.getSysStatClientCurConns());
} catch (Exception e) {
logger.error("getCpuMemConns has error", e);
} finally {
destroy();
}
logger.info("end getCpuMemConns");
return collResult.getKPISet();
}
public Vector getCapacity(HashMap<String, String> params) {
logger.info("begin getCapacity");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
connect();
String UNIT_ID = PRE_UNITID + "-10" + ":" + deviceName + "-total";
// String cliIn1 = this.getSysStatBytesInOut().get("CLIIN");
// String cliOut1 = this.getSysStatBytesInOut().get("CLIIN");
// String serIn1 = this.getSysStatBytesInOut().get("CLIIN");
// String serOut1 = this.getSysStatBytesInOut().get("CLIIN");
// Thread.sleep(5);
// String cliIn2 = this.getSysStatBytesInOut().get("CLIIN");
// String cliOut2 = this.getSysStatBytesInOut().get("CLIIN");
// String serIn2 = this.getSysStatBytesInOut().get("CLIIN");
// String serOut2 = this.getSysStatBytesInOut().get("CLIIN");
String bytesInOut1 = this.getBytesInOut();
Thread.sleep(10 * 1000L);
String bytesInOut2 = this.getBytesInOut();
Double dou = (Double.valueOf(bytesInOut2) - Double.valueOf(bytesInOut1)) / 10;
// PM-00-05-009-31 吞吐率
collResult.addKPI(UNIT_ID, "PM-00-05-009-31", Formater.formatDecimalByScale(String.valueOf(dou), 2));
} catch (Exception e) {
logger.error("getCapacity has error", e);
} finally {
destroy();
}
logger.info("end getCapacity");
return collResult.getKPISet();
}
public Vector getVirtualServer(HashMap<String, String> params) {
logger.info("begin getVirtualServer");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
VirtualServEntryWalker vsewTable = new VirtualServEntryWalker();
vsewTable.FillTable(params.get("IPADDR"), Integer.parseInt(params.get("SNMPPORT")), DES3.decrypt(params.get("COMMUNITY")));
Vector vseRows = vsewTable.getEntry();
for (int i = 0; i < vseRows.size(); i++) {
VirtualServEntry row = (VirtualServEntry) vseRows.get(i);
String vsName = row.getName();
String UNIT_ID = PRE_UNITID + "-12" + ":" + deviceName + "-" + Formater.neatenunitid(vsName);
// CM-00-02-002-10 VS的名称
collResult.addKPI(UNIT_ID, "CM-00-02-002-10", vsName);
// CM-00-02-002-11 VS的IP地址
collResult.addKPI(UNIT_ID, "CM-00-02-002-11", Formater.hexToInt(row.getAddr()));
// CM-00-02-002-12 VS的默认pool
collResult.addKPI(UNIT_ID, "CM-00-02-002-12", row.getDefaultPool());
}
} catch (Exception e) {
logger.error("getVirtualServer has error", e);
} finally {
destroy();
}
logger.info("end getVirtualServer");
return collResult.getKPISet();
}
public Vector getPool(HashMap<String, String> params) {
logger.info("begin getPool");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
PoolMemberEntryWalker pmeTable = new PoolMemberEntryWalker();
pmeTable.FillTable(params.get("IPADDR"), Integer.parseInt(params.get("SNMPPORT")), DES3.decrypt(params.get("COMMUNITY")));
Vector pmeRows = pmeTable.getEntry();
for (int i = 0; i < pmeRows.size(); i++) {
PoolMemberEntry row = (PoolMemberEntry) pmeRows.get(i);
String poolName = row.getPoolName();
String memAddrs = Formater.hexToInt(row.getAddr());
String UNIT_ID = PRE_UNITID + "-13" + ":" + deviceName + "-" + Formater.neatenunitid(poolName) + "_" + Formater.neatenunitid(memAddrs);
// CM-00-02-002-13 Pool名称
collResult.addKPI(UNIT_ID, "CM-00-02-002-13", poolName);
// CM-00-02-002-14 PoolMember的IP地址
collResult.addKPI(UNIT_ID, "CM-00-02-002-14", memAddrs);
}
} catch (Exception e) {
logger.error("getPool has error", e);
} finally {
destroy();
}
logger.info("end getPool");
return collResult.getKPISet();
}
/**
* 采集系统启动时间
*/
private String getDevSysUpTime() {
String oid = "1.3.6.1.2.1.1.3.0";
String sysUpTime = null;
try {
sysUpTime = client.get(oid);
} catch (Exception e) {
logger.error("Exception while getDevSysUpTime", e);
}
return sysUpTime;
}
/**
* 设备软件版本
*/
private String getSoftVersion() {
String oid = "1.3.6.1.2.1.1.1.0";
String version = null;
try {
version = client.get(oid).replace("\n", " ").replace("\r", "");
} catch (Exception e) {
logger.error("Exception while getSoftVersion", e);
}
return version;
}
private String getSystemName() {
String oid = "1.3.6.1.2.1.1.5.0";
String systemName = null;
try {
systemName = client.get(oid);
} catch (Exception e) {
logger.error("Exception while getSystemName", e);
}
return systemName;
}
/**
* 内存总大小
*/
private String getSysHostMemoryTotal() {
String oid = "1.3.6.1.4.1.3375.2.1.7.1.1.0";
String memTotal = null;
try {
memTotal = client.get(oid);
} catch (Exception e) {
logger.error("Exception while getSysHostMemoryTotal", e);
}
return memTotal;
}
/**
* 内存已使用大小
*/
private String getSysHostMemoryUsed() {
String oid = "1.3.6.1.4.1.3375.2.1.7.1.2.0";
String memUsed = "";
try {
memUsed = client.get(oid);
} catch (Exception e) {
logger.error("Exception while getSysHostMemoryUsed", e);
}
return memUsed;
}
private String getSysStatClientCurConns() {
String oid = "1.3.6.1.4.1.3375.2.1.1.2.1.8.0";
String clientConns = "";
try {
clientConns = client.get(oid);
} catch (Exception e) {
logger.error("Exception while getSysStatClientCurConns", e);
}
return clientConns;
}
public HashMap<String, String> getSysStatBytesInOut() {
HashMap<String, String> map = new HashMap<String, String>();
String cliInoid = "1.3.6.1.4.1.3375.2.1.1.2.1.3";
String cliOutoid = "1.3.6.1.4.1.3375.2.1.1.2.1.5";
String serInoid = "1.3.6.1.4.1.3375.2.1.1.2.1.10";
String serOutoid = "1.3.6.1.4.1.3375.2.1.1.2.1.12";
try {
map.put("CLIIN", client.get(cliInoid));
map.put("CLIOUT", client.get(cliOutoid));
map.put("SERIN", client.get(serInoid));
map.put("SEROUT", client.get(serOutoid));
} catch (Exception e) {
logger.error("Exception while getSysStatClientBytesIn", e);
}
return map;
}
public String getBytesInOut() {
Double dou = 0.0;
String cliInoid = "1.3.6.1.4.1.3375.2.1.1.2.1.3.0";
String cliOutoid = "1.3.6.1.4.1.3375.2.1.1.2.1.5.0";
String serInoid = "1.3.6.1.4.1.3375.2.1.1.2.1.10.0";
String serOutoid = "1.3.6.1.4.1.3375.2.1.1.2.1.12.0";
try {
System.out.println(client.get(cliInoid) + " " + client.get(cliOutoid) + " " + client.get(serInoid) + " " + client.get(serOutoid));
dou = (Double.valueOf(client.get(cliInoid)) +
Double.valueOf(client.get(cliOutoid)) +
Double.valueOf(client.get(serInoid)) +
Double.valueOf(client.get(serOutoid))) / 1024.0 / 1024.0;
} catch (Exception e) {
logger.error("Exception while getSysStatClientBytesIn", e);
}
return String.valueOf(dou);
}
/**
* 端口状态映射
* none(0) - no pause,
* txrx(1) - pause all data flow,
* tx(2) - pause out going data flow,
* rx(3) - pause in coming data flow
*/
public String parsePauseActive(String pauseActive) {
String res = "none";
if ("0".equals(pauseActive)) {
res = "no pause";
} else if ("1".equals(pauseActive)) {
res = "pause all";
} else if ("2".equals(pauseActive)) {
res = "pause out";
} else if ("3".equals(pauseActive)) {
res = "pause in";
}
return res;
}
}