CollResinWithJMX.java
12.5 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
package com.sitech.ismp.coll.centercoll;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.apache.log4j.Logger;
import com.sitech.ismp.coll.CollBase;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
public class CollResinWithJMX extends CollBase {
private final String PRE_UNITID = "10-12-14"; // Resin的标识
private final String PRE_UNITID_PM = PRE_UNITID+"-11"; // 性能指标
private final String PRE_UNITID_CM = PRE_UNITID+"-10"; // 配置指标
private final String PRE_UNITID_FM = PRE_UNITID+"-12"; // 状态指标
private final static String COLL_IP = "COLL_IP";
private final static String JMX_PORT = "JMX_PORT";
private final static String HTTP_PORT = "HTTP_PORT";
String hostName = "";
MBeanServerConnection mbsc = null;
JMXConnector connector = null;
public void init(HashMap params) {
String collIp = (String) params.get(COLL_IP);
String jmxPort = (String) params.get(JMX_PORT);
String jmxURL = "service:jmx:rmi:///jndi/rmi://"+collIp+":"+jmxPort+"/jmxrmi";//tomcat jmx url
try{
//System.out.println("connect jmx sevice");
System.out.println("jmxURL="+jmxURL);
JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);
Map map = new HashMap();
String[] credentials = new String[] { "monitorRole" , "QED" };
map.put("jmx.remote.credentials", credentials);
connector = JMXConnectorFactory.connect(serviceURL, map);
mbsc = connector.getMBeanServerConnection();
// 获取主机名称
ObjectName runtimeObjName = new ObjectName("java.lang:type=Runtime");
hostName = (String)mbsc.getAttribute(runtimeObjName, "Name");
int pos = hostName.indexOf("@", 0);
hostName = hostName.substring(pos+1, hostName.length());
} catch (Exception e) {
logger.error(e);
e.printStackTrace();
}
}
/**
* 采集内存使用指标
* @param params
* @return
*/
public Vector getMemory(HashMap params) {
logger.info("begin getMemory");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
ObjectName heapObjName = new ObjectName("java.lang:type=Memory");
CompositeDataSupport heapMemoryUsage = (CompositeDataSupport)mbsc.getAttribute(heapObjName, "HeapMemoryUsage");
long maxMemory = ((Long)heapMemoryUsage.get("max")).longValue()/(1024*1024);//堆最大
long commitMemory = ((Long)heapMemoryUsage.get("committed")).longValue()/(1024*1024);//堆当前分配
long usedMemory = ((Long)heapMemoryUsage.get("used")).longValue()/(1024*1024); //当前使用的
double heapUsage = (double)usedMemory*100/commitMemory;//堆使用率
/*
MemoryUsage nonheapMemoryUsage = MemoryUsage.from((CompositeDataSupport)mbsc.getAttribute(heapObjName, "NonHeapMemoryUsage"));
long noncommitMemory = nonheapMemoryUsage.getCommitted();
long nonusedMemory = heapMemoryUsage.getUsed();
System.out.println("nonheap:"+(double)nonusedMemory*100/noncommitMemory+"%");
*/
ObjectName permObjName = new ObjectName("java.lang:type=MemoryPool,name=Perm Gen");
CompositeDataSupport permGenUsage = (CompositeDataSupport)mbsc.getAttribute(permObjName, "Usage");
long committed = ((Long)heapMemoryUsage.get("committed")).longValue()/(1024*1024);//持久堆大小
long used = ((Long)permGenUsage.get("used")).longValue()/(1024*1024);
double permUsage = (double)used*100/committed;//持久堆使用率
//当前内存堆使用比率
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-memory",
"PM-00-04-003-01", String.valueOf((int)heapUsage));
//允许支配的内存堆
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-memory",
"CM-00-04-003-02", String.valueOf(maxMemory));
//当前使用的内存堆
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-memory",
"PM-00-04-004-01", String.valueOf(usedMemory));
//持久内存堆大小
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-memory",
"PM-00-04-004-02", String.valueOf(committed));
//当前使用的持久内存堆大小
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-memory",
"PM-00-04-004-03", String.valueOf(used));
//内存持久堆使用率
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-memory",
"PM-00-04-004-04", String.valueOf(permUsage));
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
connector.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return collResult.getKPISet();
}
/**
* 采集会话指标
* @param params
* @return
*/
public Vector getSession(HashMap params) {
logger.info("begin getSession");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
String httPort = (String) params.get(HTTP_PORT);
ObjectName threadpoolObjName = new ObjectName("resin:type=Port,name="+httPort);
//请求总数
// String reqCountTotal = mbsc.getAttribute( threadpoolObjName, "RequestCountTotal").toString();
//最大连接数
String idleThreads = mbsc.getAttribute( threadpoolObjName, "ConnectionMax").toString();
//请求时间总数
String reqTimeTotal = mbsc.getAttribute( threadpoolObjName, "RequestTimeTotal").toString();
//保持活动总数
String minSpareThreads = mbsc.getAttribute( threadpoolObjName, "KeepaliveCountTotal").toString();
//保持最大数
String maxThreads = mbsc.getAttribute( threadpoolObjName, "KeepaliveMax").toString();
//请求总数
// collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-session",
// "PM-00-04-004-10", reqCountTotal);
//最大连接数
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-thread",
"PM-00-04-004-11", idleThreads);
//请求时间总数
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-thread",
"PM-00-04-004-12", reqTimeTotal);
//保持活动总数
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-thread",
"PM-00-04-004-13", minSpareThreads);
//保持最大数
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-thread",
"PM-00-04-004-14", maxThreads);
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
connector.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return collResult.getKPISet();
}
/**
* 采集运行指标
* @param params
* @return
*/
public Vector getRuntime(HashMap params) {
logger.info("begin getRuntime");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
ObjectName runtimeObjName = new ObjectName("java.lang:type=Runtime");
//System.out.println("厂商:"+ (String)mbsc.getAttribute(runtimeObjName, "VmVendor"));
//System.out.println("程序:"+ (String)mbsc.getAttribute(runtimeObjName, "VmName"));
//System.out.println("版本:"+ (String)mbsc.getAttribute(runtimeObjName, "VmVersion"));
Date runTime= new Date(((Long)mbsc.getAttribute(runtimeObjName, "StartTime")).longValue());
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//启动时间
String startTime = df.format(runTime);
//连续工作时间
Long timespan=(Long)mbsc.getAttribute(runtimeObjName, "Uptime");
String upTime =formatTimeSpan(timespan.longValue());
collResult.addKPI(this.PRE_UNITID_FM + ":" + hostName + "-runtime","FM-00-04-004-01", startTime);//启动时间
collResult.addKPI(this.PRE_UNITID_FM + ":" + hostName + "-runtime","FM-00-04-004-02", upTime);//连续工作时间
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
connector.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return collResult.getKPISet();
}
public Vector getThreadPool(HashMap params) {
logger.info("begin getThreadPool");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
ObjectName threadpoolObjName = new ObjectName("resin:type=ThreadPool");
//活动线程数
String activeThreads = mbsc.getAttribute( threadpoolObjName, "ThreadActiveCount").toString();
//空闲线程数
String idleThreads =mbsc.getAttribute( threadpoolObjName, "ThreadIdleCount").toString();
//线程数
String threads = mbsc.getAttribute( threadpoolObjName, "ThreadCount").toString();
//最小空闲线程数
String minSpareThreads = mbsc.getAttribute( threadpoolObjName, "SpareThreadMin").toString();
//最大线程数
String maxThreads = mbsc.getAttribute( threadpoolObjName, "ThreadMax").toString();
//活动线程数
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-thread",
"PM-00-04-004-15", activeThreads);
//空闲线程数
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-thread",
"PM-00-04-004-16", idleThreads);
//线程数
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-thread",
"PM-00-04-004-17", threads);
//最小空闲线程数
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-thread",
"PM-00-04-004-08", minSpareThreads);
//最大线程数
collResult.addKPI(this.PRE_UNITID_PM + ":" + hostName + "-thread",
"PM-00-04-004-05", maxThreads);
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
connector.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return collResult.getKPISet();
}
public static String formatTimeSpan(long ms){
int ss = 1000;
int mi = ss * 60;
int hh = mi * 60;
int dd = hh * 24;
long day = ms / dd;
long hour = (ms - day * dd) / hh;
long minute = (ms - day * dd - hour * hh) / mi;
long second = (ms - day * dd - hour * hh - minute * mi) / ss;
long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
String strDay = day < 10 ? "" + day : "" + day;
String strHour = hour < 10 ? "0" + hour : "" + hour;
String strMinute = minute < 10 ? "0" + minute : "" + minute;
String strSecond = second < 10 ? "0" + second : "" + second;
String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;
strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;
return strDay + "天" + strHour + ":" + strMinute + ":" + strSecond + " " + strMilliSecond;
}
/**
* @param args
*/
public static void main(String[] args) {
try {
HashMap params = new HashMap();
//params.put(COLL_IP, "172.20.203.203");
//params.put(COLL_IP, "192.168.6.250");
params.put(COLL_IP, "192.168.6.250");
//params.put(COLL_IP, "192.168.3.254");
params.put(JMX_PORT, "9999");
params.put(HTTP_PORT, "6666");
System.out.println("begin................");
CollResinWithJMX coll = new CollResinWithJMX();
Vector lst = coll.getMemory(params);
System.out.println("lst.size() is " + lst.size());
for (int i = 0; i < lst.size(); i++) {
TblATO_KPIDETAIL kpidetail = (TblATO_KPIDETAIL) lst.get(i);
System.out.println(kpidetail.UNIT_ID + "|" + kpidetail.KPI_ID + "|"
+ kpidetail.CLL_TIME + "|" + kpidetail.KPI_VALUE);
}
lst = coll.getSession(params);
for (int i = 0; i < lst.size(); i++) {
TblATO_KPIDETAIL kpidetail = (TblATO_KPIDETAIL) lst.get(i);
System.out.println(kpidetail.UNIT_ID + "|" + kpidetail.KPI_ID + "|"
+ kpidetail.CLL_TIME + "|" + kpidetail.KPI_VALUE);
}
lst = coll.getThreadPool(params);
for (int i = 0; i < lst.size(); i++) {
TblATO_KPIDETAIL kpidetail = (TblATO_KPIDETAIL) lst.get(i);
System.out.println(kpidetail.UNIT_ID + "|" + kpidetail.KPI_ID + "|"
+ kpidetail.CLL_TIME + "|" + kpidetail.KPI_VALUE);
}
lst = coll.getRuntime(params);
for (int i = 0; i < lst.size(); i++) {
TblATO_KPIDETAIL kpidetail = (TblATO_KPIDETAIL) lst.get(i);
System.out.println(kpidetail.UNIT_ID + "|" + kpidetail.KPI_ID + "|"
+ kpidetail.CLL_TIME + "|" + kpidetail.KPI_VALUE);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}