CollWeblogic10WithSNMP.java
19.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
package com.sitech.ismp.coll;
import java.util.Date;
import java.util.HashMap;
import java.util.Vector;
import com.adventnet.snmp.beans.SnmpTarget;
import com.adventnet.snmp.snmp2.SnmpOID;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.Formater;
public class CollWeblogic10WithSNMP implements CollWeblogicWithSNMPMBean {
SnmpTarget target = null;
String ObjectIP = null;
private boolean DEBUG = false;
String hostname = "";
String community = null;
String ip = null;
int port = 0;
String servicename = null;
String snmp_version ="1";
private Vector result = new Vector();
public Vector coll(HashMap params) {
// TODO Auto-generated method stub
String community = (String) params.get("COMMUNITY");
String ip = (String) params.get("IP"); // 要采集的weblogic10 IP
int port = Integer.parseInt((String) params.get("PORT")); // SNMP端口
String servicename = (String) params.get("SERVICENAME"); // weblogic10所开的server
String snmp_version = (String) params.get("SNMP_VERSION");
if(snmp_version!=null&&!snmp_version.equals("")){
this.snmp_version = snmp_version;
}
String hostname = ip;
this.hostname = hostname;
this.ip = ip;
this.port = port;
this.community = community;
this.servicename = servicename;
initWeblogic(ip, servicename, port, hostname, community);
//this.walkKPIInfo(".1.3.6.1.4.1.140.625");
collWeblogic10();
this.release();
return this.result;
}
public void collWeblogic10() {
try {
// 采集连接池情况
collConnPoolValues();
// 采集日志路径
collLog();
// 采集内存堆相关
collHeap();
// 采集JTA相关
collJTA();
// 采集集群相关
collCluster();
// 采集消息队列
collQueue();
// noneffective();
// 服务运行状态
collRunState();
// String unitID = "10-12-11-10-10:" + hostname + "-total";
// FM-00-04-003-03 主机状态 true 连接正常 false 连接失败可能宕机
// setResult("true", unitID, "FM-00-04-003-03");
} catch (Exception e) {
// 10-12-11-10-10------->10-12-11-11-10
String unitID = "10-12-11-11-10:"
+ Formater.neatenunitid(this.servicename) + "-RunState";
// FM-00-04-003-03 主机状态 true 连接正常 false 连接失败可能宕机
setResult("false", unitID, "FM-00-04-003-03");
}
}
public void collConnPoolValues() {
getThreadPool();
getJDBCPool();
getTransaction();
}
public void getThreadPool(){
try{
String unitID = "10-12-11-11-11:";
unitID = unitID + Formater.neatenunitid(this.servicename) + "-threadpool" ;
//.1.3.6.1.4.1.140.625.367.1.45--- Returns the maximum amount of requests that can be accepted in the priority qeueue.
Vector vc_thread = walkKPIInfo(".1.3.6.1.4.1.140.625.367.1.45");
String thread_max="0";
if(vc_thread.size()>0){
thread_max = (String)vc_thread.elementAt(0);
//CM-00-04-001-01 中间件最大并发连接数
setResult(thread_max, unitID, "CM-00-04-001-01");
}
//.1.3.6.1.4.1.140.625.367.1.25---- Returns the total number of threads in the pool.
Vector vc_current = walkKPIInfo(".1.3.6.1.4.1.140.625.367.1.25");
String current="0";
if(vc_current.size()>0){
current = (String)vc_current.elementAt(0);
//FM-00-04-001-03 客户端连接的请求个数
setResult(current, unitID, "FM-00-04-001-03");
}
//.1.3.6.1.4.1.140.625.367.1.35--- Returns the number of pending requests in the priority queue. This is the total of internal system requests
//and user requests.
Vector vc_queue = walkKPIInfo(".1.3.6.1.4.1.140.625.367.1.25");
String queue_length="0";
if(vc_queue.size()>0){
queue_length = (String)vc_queue.elementAt(0);
//PM-00-04-001-01 客户端连接的队列个数
setResult(queue_length, unitID, "PM-00-04-001-01");
}
//.1.3.6.1.4.1.140.625.365.1.35- Returns the Number of requests waiting for a thread to become available.
Vector vc_pending = walkKPIInfo(".1.3.6.1.4.1.140.625.365.1.35");
String pending="0";
if(vc_pending.size()>0){
pending = (String)vc_pending.elementAt(0);
//PM-00-04-001-03 队列等待数
setResult(pending, unitID, "PM-00-04-001-03");
}
}catch(Exception e){
e.printStackTrace();
}
}
public void getJDBCPool(){
String unitID = "10-12-11-11-11:";
unitID = unitID + Formater.neatenunitid(this.servicename) + "-jdbcpool" ;
try{
//.1.3.6.1.4.1.140.625.190.1.60-The maximum capacity of this JDBC pool
Vector vc_max = walkKPIInfo(".1.3.6.1.4.1.140.625.190.1.60");
String max="0";
if(vc_max.size()>0){
max = (String)vc_max.elementAt(0);
//CM-00-04-003-04 配置的数据库连接池大小
setResult(max, unitID, "CM-00-04-003-04");
}
//.1.3.6.1.4.1.140.625.190.1.25---The current total active connections.
Vector vc_current = walkKPIInfo(".1.3.6.1.4.1.140.625.190.1.25");
String current="0";
if(vc_current.size()>0){
current = (String)vc_current.elementAt(0);
}
double dou_max = 0;
double dou_current=0;
try{
dou_max = Double.valueOf(max).doubleValue();
dou_current = Double.valueOf(current).doubleValue();
double use_rate = 0;
if(dou_max>0){
use_rate=100 * dou_current/dou_max;
}
//PM-00-04-003-02 当前数据库连接池使用比率
setResult(String.valueOf(use_rate), unitID, "PM-00-04-003-02");
}catch(Exception e){
e.printStackTrace();
// setResult(String.valueOf(100), unitID, "CM-00-04-003-04");
}
}catch(Exception e){
e.printStackTrace();
}
}
public void getTransaction(){
String unitID = "10-12-11-11-11:";
unitID = unitID + Formater.neatenunitid(this.servicename) + "-transaction" ;
try{
//.1.3.6.1.4.1.140.625.410.1.35--The number of transactions that were rolled back.
Vector vc_total = walkKPIInfo(".1.3.6.1.4.1.140.625.410.1.35");
String total="0";
if(vc_total.size()>0){
total = (String)vc_total.elementAt(0);
}
//FM-00-04-003-02 异常事务数
setResult(total, unitID, "FM-00-04-003-02");
}catch(Exception e){
e.printStackTrace();
}
}
public void collLog() {
String unitID = "10-12-11-11-10:";
try{
unitID = unitID + Formater.neatenunitid(this.servicename) + "-log";
//.1.3.6.1.4.1.140.625.700.1.25---- name of the error log file, including the absolute directory path.
String logfile=null;
Vector vc_log = walkKPIInfo(".1.3.6.1.4.1.140.625.700.1.25");
if(vc_log.size()>0){
logfile = (String)vc_log.elementAt(0);
}
setResult(logfile, unitID, "CM-00-04-003-05");
if(vc_log.size()>1){
logfile = (String)vc_log.elementAt(1);
}
setResult(logfile, unitID, "CM-00-04-003-06");
}catch(Exception e){
e.printStackTrace();
}
}
public void collHeap() {
try {
String unitID = "10-12-11-11-10:";
unitID = unitID + Formater.neatenunitid(this.servicename) + "-heap";
String free = null;
String total = null;
// 1.3.6.1.4.1.140.625.340.1.25------- The current amount of free
// memory in the JVM heap in bytes.
Vector vc_free = walkKPIInfo(".1.3.6.1.4.1.140.625.340.1.25");
if(vc_free.size()>0){
free = (String)vc_free.elementAt(0);
}
//.1.3.6.1.4.1.140.625.340.1.30---The current size of the JVM heap in bytes
Vector vc_total = walkKPIInfo(".1.3.6.1.4.1.140.625.340.1.30");
if(vc_total.size()>0){
total = (String)vc_total.elementAt(0);
}
long l_free = 0;
long l_total = 0;
try{
if(free!=null){
l_free=Long.parseLong(free.trim());
System.out.println(l_free);
}
if(total!=null){
l_total = Long.parseLong(total.trim());
System.out.println(l_total);
}
}catch(Exception e){
e.printStackTrace();
}
if(l_total!=0){
//bytes to M bytes
long l_total_m = l_total/(1024*1024);
setResult(String.valueOf(l_total_m), unitID, "CM-00-04-003-02");
}else{
setResult("0", unitID, "CM-00-04-003-02");
}
if(l_total!=0){
double a=l_free;
double b=l_total;
double use_rate = 100-100D*l_free/l_total;
System.out.println(use_rate);
setResult(String.valueOf(use_rate), unitID, "PM-00-04-003-01");
}else{
setResult("100", unitID, "PM-00-04-003-01");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void collJTA() {
getserverRuntimeName();
getserverRuntimeListenAddress();
getserverRuntimeListenPort();
getserverRuntimeAdminServerHost();
getserverRuntimeAdminServerListenPort();
}
public void collQueue() {
String unitID = "10-12-11-11-11:";
unitID = unitID + Formater.neatenunitid(this.servicename) + "-queue" ;
try{
String current="";
//.1.3.6.1.4.1.140.625.180.1.35-- The number of waiting requests in the queue
Vector vc_current= walkKPIInfo(".1.3.6.1.4.1.140.625.180.1.35");
if(vc_current.size()>0){
current = (String)vc_current.elementAt(0);
}
//.1.3.6.1.4.1.140.625.180.1.40--- The number of requests which have been processed by this
//queue.
String total="";
Vector vc_total = walkKPIInfo(".1.3.6.1.4.1.140.625.180.1.40");
if(vc_total.size()>0){
total = (String)vc_total.elementAt(0);
}
double dou_total=0;
double dou_current=0;
try{
dou_total = Double.valueOf(total).doubleValue();
dou_current = Double.valueOf(current).doubleValue();
double use_rate = 100D*dou_current/dou_total;
//PM-00-04-002-01 消息队列中的占用比例(百分比)
setResult(String.valueOf(use_rate), unitID, "PM-00-04-002-01");
}catch(Exception e){
e.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
}
public void collRunState() {
String unitID = "10-12-11-11-10:";
// FM-00-04-001-01 应用服务器状态
try {
unitID = unitID + Formater.neatenunitid(this.servicename)
+ "-RunState";
// 1.3.6.1.4.1.140.625.360.1.60----- Return the current state of the
// server. Legal values are Initializing, Suspended, Running,
// ShuttingDown.
Vector stateRun = walkKPIInfo(".1.3.6.1.4.1.140.625.360.1.60");// logPath
// and
// logFileName
String state = (String) stateRun.elementAt(0);
if (state != null && state.length() > 0
&& state.toUpperCase().equals("RUNNING")) {
state = "UP";
} else {
state = "DOWN";
}
if (stateRun.size() > 0) {
setResult(state, unitID, "FM-00-04-001-01");
}
} catch (Exception e) {
e.printStackTrace();
}
try {
/*
* unitID = unitID + Formater.neatenunitid(this.servicename) +
* "-RunState"; // .1.3.6.1.4.1.140.625.430.1.30----- Return the
* component's status. Vector stateRun =
* walkKPIInfo(".1.3.6.1.4.1.140.625.430.1.30");// logPath String
* state = (String) stateRun.elementAt(0); if (state != null &&
* state.length() > 0 && state.toUpperCase().equals("RUNNING")) {
* state = "UP"; } else { state = "DOWN"; } if (stateRun.size() > 0)
* { setResult(state, unitID, "FM-00-04-001-02"); }
*/
} catch (Exception e) {
e.printStackTrace();
}
}
public void collCluster() {
try {
// .1.3.6.1.4.1.140.625.110.1.10----- BEA-proprietary MBean type
Vector clusters = walkKPIInfo(".1.3.6.1.4.1.140.625.110.1.10");// Cluster
// CM-00-04-003-01 应用服务器运行模式 应用服务器群集还是单机,true为集群,false为单机
String cluster = "false";
if (!clusters.isEmpty()) {
cluster = "true";
}
String unitID = "10-12-11-10-10:"
+ Formater.neatenunitid(this.servicename) + "-"
+ "runmodel";
setResult(cluster, unitID, "CM-00-04-003-01");
} catch (Exception e) {
e.printStackTrace();
}
}
public void getserverRuntimeName() {
try {
String unitid = "10-12-11-11-10:";
unitid = unitid + Formater.neatenunitid(this.servicename)
+ "-serverName";
// .1.3.6.1.4.1.140.625.360.1.15-------- BEA-proprietary MBean name
Vector vc_active = walkKPIInfo(".1.3.6.1.4.1.140.625.360.1.15");
if (vc_active.size() > 0) {
String count = (String) vc_active.elementAt(0);
setResult(count, unitid, "CM-00-04-003-07");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void getserverRuntimeListenAddress() {
try {
String unitid = "10-12-11-11-10:";
unitid = unitid + Formater.neatenunitid(this.servicename)
+ "-hostAddress";
// .1.3.6.1.4.1.140.625.360.1.30-------- Return the address on which
// this server is listening for
// connections. For example this might return the string:
// santiago/172.17.9.220
Vector vc_address = walkKPIInfo(".1.3.6.1.4.1.140.625.360.1.30");
if (vc_address.size() > 0) {
String address = (String) vc_address.elementAt(0);
setResult(address, unitid, "CM-00-04-003-08");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void getserverRuntimeListenPort() {
try {
String unitid = "10-12-11-11-10:";
unitid = unitid + Formater.neatenunitid(this.servicename)
+ "-servicePort";
// .1.3.6.1.4.1.140.625.360.1.35-------- Return the port on which
// this server is listening for connections.
Vector vc_port = walkKPIInfo(".1.3.6.1.4.1.140.625.360.1.35");
if (vc_port.size() > 0) {
String port = (String) vc_port.elementAt(0);
setResult(port, unitid, "CM-00-04-003-09");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void getserverRuntimeAdminServerHost() {
try {
String unitid = "10-12-11-11-10:";
unitid = unitid + Formater.neatenunitid(this.servicename)
+ "-serviceAddress";
// .1.3.6.1.4.1.140.625.360.1.70-------- Return the address on which
// admin server is listening for
// connections. For example this might return the string:
// santiago
Vector vc_address = walkKPIInfo(".1.3.6.1.4.1.140.625.360.1.70");
if (vc_address.size() > 0) {
String address = (String) vc_address.elementAt(0);
setResult(address, unitid, "CM-00-04-003-10");
}
} catch (Exception e) {
}
}
public void getserverRuntimeAdminServerListenPort() {
try {
String unitid = "10-12-11-11-10:";
unitid = unitid + Formater.neatenunitid(this.servicename)
+ "-serverListenPort";
// .1.3.6.1.4.1.140.625.360.1.75-------- Return the port on which
// admin server is listening for
// connections.
Vector vc_aport = walkKPIInfo(".1.3.6.1.4.1.140.625.360.1.75");
if (vc_aport.size() > 0) {
String port = (String) vc_aport.elementAt(0);
setResult(port, unitid, "CM-00-04-003-11");
}
} catch (Exception e) {
}
}
public void initWeblogic(String weblogicip, String servername, int port,
String hostname, String community) {
try {
this.ObjectIP = weblogicip;
System.out.println("weblogicip=" + weblogicip);
// this.KBP_ID = kbp_id;
// String authProtocol = new String("NO_AUTH");
// Use an SNMP target bean to perform SNMP operations
target = new SnmpTarget();
// target.setLoadFromCompiledMibs(true);
target.setDebug(DEBUG);
if(this.snmp_version.equals("1")){
target.setSnmpVersion(SnmpTarget.VERSION1);
}else if(this.snmp_version.equals("2")){
target.setSnmpVersion(SnmpTarget.VERSION2C);
}else if(this.snmp_version.equals("3")){
target.setSnmpVersion(SnmpTarget.VERSION3);
}
target.setTargetHost(ObjectIP); // set the agent hostname
// target.setTargetHost(hostname); // set the agent hostname(ip or
// hostname):ip of HP workstation
System.out.println("hostname=" + hostname);
// target.setTargetHost( "SUNE450" ); // set the agent hostname:
// solaris
// target.setTargetHost( "iona" ); // set the agent hostname:
// windows
/* SUNE450 是主机的hostname. */
/*String weblogic_COMMUNITY = community.trim();
System.out.println("weblogic_COMMUNITY=" + weblogic_COMMUNITY);
target.setCommunity(weblogic_COMMUNITY);*/
target.setCommunity( this.community.trim());
/**
* 关于weblogic多多服务器情况下,如何采集不同的server的值的方法,主要
* 就是设置COMMUNITY的值,在这个我以admin server with clustered servers的
* 情况来说明,domain name=mydomain2,administrtation server = myserver,
* managed server = server1.
* 1.如果取myserver的相关的OID的值,COMMUNITY="public";
* 2.如果取server1的相关的OID的值,COMMUNITY="public@server1";
* 3.如果取整个domain的所有server的相关的OID的值,COMMUNITY="public@mydomain2";
*/
target.setTargetPort(port);
System.out.println("port=" + port);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 释放initWeblogic(String weblogicip,String servername,int port, String
* hostname,String community) 申请的资源
*/
public void release() {
target.releaseResources();
}
/**
* @author pc103
* @date 2006-07-21
* @param oid
* @param bool
* @return vector 参数说明书: oid-----对应weblogic控制台监控的性能指标id(关键) bool----无多大用处
* 功能:储存各采集结果 返回类型:vector
*
*/
public Vector walkKPIInfo(String oid) {
Vector tmp_result = new Vector();
try {
Vector tmp_oid = new Vector();
target.setObjectID(oid);
target.oidList[0] = new com.adventnet.snmp.snmp2.SnmpOID(oid);// .1.3.6.1.4.1.11.2.3.1.2.2.1
// Each
// entry
// contains
// objects
// for a
// particular
// file
// system.(HP)
int maxtry = 0;
com.adventnet.snmp.snmp2.SnmpOID[] oidList = target
.getSnmpOIDList();
// System.out.println("oidList.length="+oidList.length);
if (oidList == null) {
System.out.println("oidList is null");
return null;
} else {
SnmpOID rootoid = oidList[0];
SnmpOID curroid = null;
while (maxtry++ < 3000) { // limit the max getnexts to 5000
String result1[] = target.snmpGetNextList();
if (result1 == null) {
target.oidList[0] = curroid;
continue;
}
if (!SnmpTarget.isInSubTree(rootoid, target.getSnmpOID()))
break;
for (int i = 0; i < result1.length; i++) {
tmp_result.add(result1[i]);
tmp_oid.add(target.getObjectID(i));
System.out.println(target.getObjectID(i) + ": "
+ result1[i]);
}
if (result1[0].compareTo("NULL") == 0)
break;
}
if (maxtry == 1) { // we did not get a valid row
System.err
.println("Request failed, timed out or no available data("
+ oid + "). \n " + target.getErrorString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
return tmp_result;
}
private void setResult(String value, String unitID, String kpiID) {
try {
TblATO_KPIDETAIL record;
record = new TblATO_KPIDETAIL();
record.setCLL_TIME(new java.util.Date()); // 采集所消耗的时间
record.setKPI_ID(kpiID); // kpi编号
record.setKPI_VALUE(value); // 具体采集得到的值信息
record.setUNIT_ID(unitID); // unit
record.setCLL_TIME(new Date()); // 日期
result.add(record);
} catch (Exception e) {
}
}
public static void main(String[] args) {
HashMap map = new HashMap();
// map.put("IP", "134.224.60.78");
// map.put("IP", "134.224.23.136");
map.put("IP", "172.18.27.41");
map.put("PORT", "10161");
map.put("SERVICENAME", "adminserver");
// map.put("SERVICENAME", "cgServer");
map.put("VERSION", "10");
map.put("COMMUNITY", "public");
CollWeblogic10WithSNMP coll = new CollWeblogic10WithSNMP();
Vector vector=coll.coll(map);
for (int i = 0; i < vector.size(); i++) {
TblATO_KPIDETAIL ato = (TblATO_KPIDETAIL) vector.get(i);
System.out.println(ato.UNIT_ID + " | " + ato.KPI_ID + " | "
+ ato.KPI_VALUE + " | " + ato.CLL_TIME);
}
}
}