CollTomcat5.java
10.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
package com.sitech.ismp.coll.middleware.tomcat.impl;
import java.lang.management.MemoryUsage;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Logger;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.openmbean.CompositeDataSupport;
import com.sitech.ismp.coll.CollBase;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.Formater;
public class CollTomcat5 extends CollTomcat implements ICollTomcat {
private Logger logger = Logger.getLogger("COLL");
private static String KBP_CLASS = "10-12-13";
public CollTomcat5() {
}
public Vector<TblATO_KPIDETAIL> getMemory(HashMap<String, String> params) {
CollBase collResult = new CollBase();
init(params);
String domain_name = params.get("DOMAIN_NAME");
String UNIT_ID = KBP_CLASS + "-12:" + Formater.neatenunitid(domain_name) + "-MEMORY";
try {
MemoryUsage heapMemoryUsage = MemoryUsage.from((CompositeDataSupport) this.execute("java.lang:type=Memory", "HeapMemoryUsage"));
// 表示可以用于内存管理的最大内存量(以字节为单位)
double maxMemory = heapMemoryUsage.getMax() / 1024.0;
// 表示保证可以由 Java 虚拟机使用的内存量(以字节为单位)
double commitMemory = heapMemoryUsage.getCommitted() / 1024.0;
// 表示当前已经使用的内存量(以字节为单位)
double usedMemory = heapMemoryUsage.getUsed() / 1024.0;
double heapUsage = (double) usedMemory * 100 / commitMemory;
collResult.addKPI(UNIT_ID, "PM-00-04-004-01", Formater.getByScale(String.valueOf(maxMemory), 4));
collResult.addKPI(UNIT_ID, "PM-00-04-004-02", Formater.getByScale(String.valueOf(commitMemory), 4));
collResult.addKPI(UNIT_ID, "PM-00-04-004-03", Formater.getByScale(String.valueOf(usedMemory), 4));
collResult.addKPI(UNIT_ID, "PM-00-04-004-04", Formater.getByScale(String.valueOf(heapUsage), 2));
logger.info("[" + maxMemory + "][" + commitMemory + "][" + usedMemory + "][" + heapUsage);
MemoryUsage permGenUsage = MemoryUsage.from((CompositeDataSupport) this.execute("java.lang:type=MemoryPool,name=PS Perm Gen", "Usage"));
double maxd = permGenUsage.getMax() / 1024.0;
double committed = permGenUsage.getCommitted() / 1024.0;
double used = permGenUsage.getUsed() / 1024.0;
double permUsage = (double) used * 100 / committed;
collResult.addKPI(UNIT_ID, "PM-00-04-004-05", Formater.getByScale(String.valueOf(maxd), 4));
collResult.addKPI(UNIT_ID, "PM-00-04-004-06", Formater.getByScale(String.valueOf(committed), 4));
collResult.addKPI(UNIT_ID, "PM-00-04-004-07", Formater.getByScale(String.valueOf(used), 4));
collResult.addKPI(UNIT_ID, "PM-00-04-004-08", Formater.getByScale(String.valueOf(permUsage), 2));
logger.info("[" + committed + "][" + used + "][" + permUsage + "]");
} catch (NullPointerException e1) {
e1.printStackTrace();
} finally {
this.release();
}
return collResult.KPISet;
}
public Vector<TblATO_KPIDETAIL> getRuntime(HashMap<String, String> params) {
CollBase collResult = new CollBase();
init(params);
String domain_name = params.get("DOMAIN_NAME");
// String ip_iddr = params.get("IP_IDDR");
String UNIT_ID = KBP_CLASS + "-10:" + Formater.neatenunitid(domain_name) + "-RUNTIME";
try {
HashMap<String, Object> resulMap = this.executeAttributeArray("java.lang:type=Runtime", new String[] { "StartTime", "Uptime", "VmVendor", "VmName", "VmVersion", "Name" });
// Java 虚拟机的启动时间(以毫秒为单位)
Date startTime = new Date(((Long) resulMap.get("StartTime")).longValue());
// Java 虚拟机的正常运行时间(以毫秒为单位)
Long upTime = (Long) resulMap.get("Uptime");
// Java 虚拟机实现供应商
String vmVendor = (String) resulMap.get("VmVendor");
// Java 虚拟机实现名称
String vmName = (String) resulMap.get("VmName");
// Java 虚拟机实现版本
String vmVersion = (String) resulMap.get("VmVersion");
// 正在运行的 Java 虚拟机的名称
String name = (String) resulMap.get("Name");
collResult.addKPI(UNIT_ID, "CM-00-05-009-14", Formater.datetimeToString(startTime));
collResult.addKPI(UNIT_ID, "CM-00-01-999-03", Formater.devDayHourSecond(upTime));
// collResult.addKPI(UNIT_ID, "CM-00-04-004-03", ip_iddr);
collResult.addKPI(UNIT_ID, "CM-00-05-009-34", vmVendor);
collResult.addKPI(UNIT_ID, "CM-00-05-009-35", vmName);
collResult.addKPI(UNIT_ID, "CM-00-05-009-36", vmVersion);
collResult.addKPI(UNIT_ID, "CM-00-05-009-37", name);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
this.release();
} catch (Exception e) {
e.printStackTrace();
}
}
return collResult.KPISet;
}
public Vector<TblATO_KPIDETAIL> getThreading(HashMap<String, String> params) {
CollBase collResult = new CollBase();
init(params);
String domain_name = params.get("DOMAIN_NAME");
String UNIT_ID = KBP_CLASS + "-10:" + Formater.neatenunitid(domain_name) + "-RUNTIME";
try {
HashMap<String, Object> resulMap = this.executeAttributeArray("java.lang:type=Threading", new String[] { "TotalStartedThreadCount", "DaemonThreadCount", "ThreadCount", "PeakThreadCount" });
// 已启动的线程总数
Long totalStartedThreadCount = (Long) resulMap.get("TotalStartedThreadCount");
// 守护线程
Integer daemonThreadCount = (Integer) resulMap.get("DaemonThreadCount");
// 活动线程
Integer threadCount = (Integer) resulMap.get("ThreadCount");
// 峰值
Integer peakThreadCount = (Integer) resulMap.get("PeakThreadCount");
collResult.addKPI(UNIT_ID, "PM-00-05-009-48", String.valueOf(totalStartedThreadCount));
collResult.addKPI(UNIT_ID, "PM-00-05-009-49", String.valueOf(daemonThreadCount));
collResult.addKPI(UNIT_ID, "PM-00-05-009-50", String.valueOf(threadCount));
collResult.addKPI(UNIT_ID, "PM-00-05-009-51", String.valueOf(peakThreadCount));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
this.release();
} catch (Exception e) {
e.printStackTrace();
}
}
return collResult.KPISet;
}
public Vector<TblATO_KPIDETAIL> getThreadPool(HashMap<String, String> params) {
CollBase collResult = new CollBase();
init(params);
String domain_name = params.get("DOMAIN_NAME");
try {
List<HashMap<String, Object>> exeResult = this.executQueryNames("Catalina:type=ThreadPool,*", new String[] { "maxThreads", "currentThreadCount", "currentThreadsBusy" });
for (HashMap<String, Object> resulMap : exeResult) {
// 线程池名
String name = (String) resulMap.get("name");
String UNIT_ID = KBP_CLASS + "-13:" + Formater.neatenunitid(domain_name) + "-" + Formater.neatenunitid(name);
// 最大线程数
String maxThreads = ((Integer) resulMap.get("maxThreads")).toString();
// 当前线程数
String currentThreadCount = ((Integer) resulMap.get("currentThreadCount")).toString();
// 繁忙线程数
String currentThreadsBusy = ((Integer) resulMap.get("currentThreadsBusy")).toString();
collResult.addKPI(UNIT_ID, "CM-00-05-009-25", name);
collResult.addKPI(UNIT_ID, "PM-00-04-009-19", maxThreads);
collResult.addKPI(UNIT_ID, "PM-00-04-009-20", currentThreadCount);
collResult.addKPI(UNIT_ID, "PM-00-04-009-21", currentThreadsBusy);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
this.release();
} catch (Exception e) {
e.printStackTrace();
}
}
return collResult.KPISet;
}
public Vector<TblATO_KPIDETAIL> getSession(HashMap<String, String> params) {
CollBase collResult = new CollBase();
init(params);
String domain_name = params.get("DOMAIN_NAME");
try {
Set<ObjectInstance> managerSet = this.executeQueryMbeans("Catalina:type=Manager,*", null);
for (Object o : managerSet) {
javax.management.ObjectInstance _1 = (javax.management.ObjectInstance) o;
ObjectName obj = _1.getObjectName();
// 应用名
String applicationName = obj.getKeyProperty("path");
String UNIT_ID = KBP_CLASS + "-11:" + Formater.neatenunitid(domain_name) + "-" + Formater.neatenunitid(applicationName);
String objname = obj.getCanonicalName();
// 最大会话数
String maxActiveSessions = this.execute(objname, "maxActiveSessions").toString();
// 会话数
String currentActiveSession = (String) this.execute(objname, "activeSessions").toString();
// 活动会话数
String sessionCounter = (String) this.execute(objname, "sessionCounter").toString();
collResult.addKPI(UNIT_ID, "CM-00-05-009-01", applicationName);
collResult.addKPI(UNIT_ID, "PM-00-05-009-42", maxActiveSessions);
collResult.addKPI(UNIT_ID, "PM-00-05-009-40", currentActiveSession);
collResult.addKPI(UNIT_ID, "PM-00-05-009-43", sessionCounter);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
this.release();
}
return collResult.KPISet;
}
public Vector<TblATO_KPIDETAIL> getClassLoading(HashMap<String, String> params) {
CollBase collResult = new CollBase();
init(params);
String domain_name = params.get("DOMAIN_NAME");
String UNIT_ID = KBP_CLASS + "-14:" + Formater.neatenunitid(domain_name) + "-CLASSLOADING";
try {
HashMap<String, Object> resulMap = this.executeAttributeArray("java.lang:type=ClassLoading", new String[] { "LoadedClassCount", "TotalLoadedClassCount", "UnloadedClassCount" });
// 当前类已装入
Integer loadedClassCount = (Integer) resulMap.get("LoadedClassCount");
// 已装入类的总数
Long totalLoadedClassCount = (Long) resulMap.get("TotalLoadedClassCount");
// 已卸载类的总数
Long unloadedClassCount = (Long) resulMap.get("UnloadedClassCount");
collResult.addKPI(UNIT_ID, "PM-00-05-009-44", String.valueOf(loadedClassCount));
collResult.addKPI(UNIT_ID, "PM-00-05-009-45", String.valueOf(totalLoadedClassCount));
collResult.addKPI(UNIT_ID, "PM-00-05-009-46", String.valueOf(unloadedClassCount));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
this.release();
} catch (Exception e) {
e.printStackTrace();
}
}
return collResult.KPISet;
}
public static void main(String[] args) {
HashMap<String, String> params = new HashMap<String, String>();
params.put("JMX_URL", "service:jmx:rmi:///jndi/rmi://172.21.136.110:1090/jmxrmi");
params.put("DOMAIN_NAME", "local-tomcat5");
CollTomcat5 ct = new CollTomcat5();
Vector<TblATO_KPIDETAIL> result = ct.getClassLoading(params);
for (TblATO_KPIDETAIL tk : result) {
System.out.println("[" + tk.UNIT_ID + "][" + tk.KPI_ID + "][" + tk.KPI_VALUE + "][" + tk.CLL_TIME + "]");
}
}
}