CollCassandraWithJMX.java
9.84 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
package com.sitech.ismp.coll.database;
import com.sitech.ismp.coll.CollBase;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.DES3;
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 java.util.HashMap;
import java.util.Map;
import java.util.Vector;
/**
* Created with IntelliJ IDEA.
* User: AMCC
* Date: 15-6-10
* Time: 下午2:31
* To change this template use File | Settings | File Templates.
*/
public class CollCassandraWithJMX extends CollBase {
private final String PRE_UNITID = "10-41-14"; // Cassamdra的标识
private String deviceName = "";
MBeanServerConnection mbsc = null;
JMXConnector connector = null;
public void init(HashMap<String, String> params) {
try {
String collIp = (String) params.get("COLLIP");
String jmxPort = (String) params.get("JMXPORT");
String user = (String) params.get("COLLUSER");
// String password = (String) params.get("COLLPASSWORD");
String password = DES3.decrypt(params.get("COLLPASSWORD"));
deviceName = (String) params.get("DEVICENAME");
String jmxURL = "service:jmx:rmi:///jndi/rmi://" + collIp + ":" + jmxPort + "/jmxrmi";//tomcat jmx url
logger.info("jmxURL=" + jmxURL);
JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);
Map map = new HashMap();
String[] credentials = new String[]{user, password};
map.put("jmx.remote.credentials", credentials);
connector = JMXConnectorFactory.connect(serviceURL, map);
mbsc = connector.getMBeanServerConnection();
} 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; //堆使用率
String unitId = PRE_UNITID + "-11:" + deviceName + "-memory";
// PM-41-02-001-04 堆当前分配
collResult.addKPI(unitId, "PM-41-02-001-04", String.valueOf(commitMemory));
// PM-41-02-001-03 允许支配的内存堆
collResult.addKPI(unitId, "PM-41-02-001-03", String.valueOf(maxMemory));
// PM-41-02-001-05 当前使用的内存堆
collResult.addKPI(unitId, "PM-41-02-001-05", String.valueOf(usedMemory));
// PM-41-02-001-19 堆使用率
collResult.addKPI(unitId, "PM-41-02-001-19", String.valueOf(heapUsage));
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
connector.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return collResult.getKPISet();
}
/**
* 采集即时状态指标
*
* @param params
* @return
*/
public Vector getStorageProxy(HashMap params) {
logger.info("begin getStorageService");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
ObjectName storageProxyObjName = new ObjectName("org.apache.cassandra.db:type=StorageProxy");
String readOperations = mbsc.getAttribute(storageProxyObjName, "ReadOperations").toString();
String writeOperations = mbsc.getAttribute(storageProxyObjName, "WriteOperations").toString();
String totalReadLatencyMicros = mbsc.getAttribute(storageProxyObjName, "TotalReadLatencyMicros").toString();
String totalWriteLatencyMicros = mbsc.getAttribute(storageProxyObjName, "TotalWriteLatencyMicros").toString();
String unitId = PRE_UNITID + "-14:" + deviceName + "-proxy";
// PM-41-02-001-10 即时读次数
collResult.addKPI(unitId, "PM-41-02-001-10", readOperations);
// PM-41-02-001-11 即时写次数
collResult.addKPI(unitId, "PM-41-02-001-11", writeOperations);
// PM-41-02-001-14 即时读延迟
collResult.addKPI(unitId, "PM-41-02-001-14", totalReadLatencyMicros);
// PM-41-02-001-15 即时写延迟
collResult.addKPI(unitId, "PM-41-02-001-15", totalWriteLatencyMicros);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
connector.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return collResult.getKPISet();
}
/**
* 采集操作系统指标
*
* @param params
* @return
*/
public Vector getOperatingSystem(HashMap params) {
logger.info("begin getStorageService");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
ObjectName storageSystemObjName = new ObjectName("java.lang:type=OperatingSystem");
String processCpuTime = mbsc.getAttribute(storageSystemObjName, "ProcessCpuTime").toString();
String availableProcessors = mbsc.getAttribute(storageSystemObjName, "AvailableProcessors").toString();
String unitId = PRE_UNITID + "-10:" + deviceName + "-system";
// PM-41-02-001-01 进CPU使用时间
collResult.addKPI(unitId, "PM-41-02-001-01", processCpuTime);
// PM-41-02-001-02 CPU可用进程
collResult.addKPI(unitId, "PM-41-02-001-02", availableProcessors);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
connector.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return collResult.getKPISet();
}
/**
* 采集存储服务指标
*
* @param params
* @return
*/
public Vector getStorageService(HashMap params) {
logger.info("begin getStorageService");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
ObjectName storageserviceObjName = new ObjectName("org.apache.cassandra.db:type=StorageService");
String liveNodes = mbsc.getAttribute(storageserviceObjName, "LiveNodes").toString();
String load = mbsc.getAttribute(storageserviceObjName, "Load").toString();
String unitId = PRE_UNITID + "-13:" + deviceName + "-system";
// FM-41-02-001-01 进程状态
collResult.addKPI(unitId, "FM-41-02-001-01", liveNodes);
// PM-41-02-001-09 当前节点存储数据量
collResult.addKPI(unitId, "PM-41-02-001-09", load);
} 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("java.lang:type=Threading");
String peakThreads = mbsc.getAttribute(threadpoolObjName, "PeakThreadCount").toString();
String threads = mbsc.getAttribute(threadpoolObjName, "ThreadCount").toString();
String unitId = PRE_UNITID + "-12:" + deviceName + "-thread";
// PM-41-02-001-07 峰值线程数
collResult.addKPI(unitId, "PM-41-02-001-07", peakThreads);
// PM-41-02-001-06 线程数
collResult.addKPI(unitId, "PM-41-02-001-06", threads);
} 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;
}
}