CollMemcachedWithCMD.java
10.4 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
package com.sitech.ismp.coll.middleware.memcached;
import com.sitech.ismp.coll.CollBase;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.Formater;
import net.rubyeye.xmemcached.MemcachedClient;
import net.rubyeye.xmemcached.MemcachedClientBuilder;
import net.rubyeye.xmemcached.XMemcachedClientBuilder;
import net.rubyeye.xmemcached.utils.AddrUtil;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
/**
* Created with IntelliJ IDEA.
* User: mooker
* Date: 14-7-7
* Time: 上午11:03
* To change this template use File | Settings | File Templates.
*/
public class CollMemcachedWithCMD {
private Logger logger = Logger.getLogger(CollMemcachedWithCMD.class);
private static MemcachedClient memcachedClient;
String KBP_CLASS = "10-12-16";
String hostip = "";
String httpport = "";
String devicename = "";
String neat_mc_name = "";
public void init(HashMap params) {
hostip = (String) params.get("IP_ADDR");
httpport = (String) params.get("HTTP_PORT");
devicename = (String) params.get("DEVICENAME");
this.neat_mc_name = Formater.neatenunitid(devicename);
try {
MemcachedClientBuilder builder = new XMemcachedClientBuilder(AddrUtil.getAddresses(hostip + ":" + httpport));
memcachedClient = builder.build();
logger.info("memcached is connected");
} catch (IOException e) {
logger.error("memcached is disconnected", e);
}
}
public void release() {
if (memcachedClient != null) {
try {
memcachedClient.shutdown();
} catch (IOException e) {
logger.error("shutdown memcached is error", e);
}
memcachedClient = null;
}
System.gc();
}
public Vector<TblATO_KPIDETAIL> getConfig(HashMap<String, String> params) {
logger.info("Begin getConfig.....");
CollBase collResult = new CollBase();
String pro_PRE_UNITID = KBP_CLASS + "-10";
init(params);
try {
Map<InetSocketAddress, Map<String, String>> result = memcachedClient.getStats();
for (InetSocketAddress isa : result.keySet()) {
String address = isa.getAddress().getHostAddress();
int port = isa.getPort();
System.out.println("[" + address + "][" + port + "]");
// memcached服务监听地址 CM-00-04-009-05
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-total", "CM-00-04-009-05", address);
// memcached服务监听端口 CM-00-04-009-06
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-total", "CM-00-04-009-06", port + "");
Map<String, String> stats = result.get(isa);
for (String kpiKey : stats.keySet()) {
// System.out.println("【" + kpiKey + "】【" + stats.get(kpiKey) + "】");
if (kpiKey.equals("pid")) {
// memcached服务进程的进程ID CM-00-01-005-01
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-total", "CM-00-01-005-01", stats.get(kpiKey));
} else if (kpiKey.equals("uptime")) {
// memcached服务从启动到当前所经过的时间,单位是秒 CM-00-05-009-14
// collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-total", "CM-00-05-009-14", Formater.devDayHourSecond(Long.parseLong(stats.get(kpiKey))));
} else if (kpiKey.equals("time")) {
// memcached服务器所在主机当前系统的时间,单位是秒 CM-00-01-999-04
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-total", "CM-00-01-999-04", stats.get(kpiKey));
} else if (kpiKey.equals("version")) {
// memcached组件的版本 CM-00-04-009-04
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-total", "CM-00-04-009-04", stats.get(kpiKey));
}
}
}
} catch (Exception e) {
logger.error("get memcached status is error", e);
} finally {
release();
}
return collResult.KPISet;
}
public Vector<TblATO_KPIDETAIL> getStatus(HashMap<String, String> params) {
logger.info("Begin getStatus.....");
CollBase collResult = new CollBase();
String pro_PRE_UNITID = KBP_CLASS + "-11";
init(params);
try {
Map<InetSocketAddress, Map<String, String>> result = memcachedClient.getStats();
for (InetSocketAddress isa : result.keySet()) {
Map<String, String> stats = result.get(isa);
for (String kpiKey : stats.keySet()) {
if (kpiKey.equals("curr_items")) {
// 表示当前缓存中存放的所有缓存对象的数量 PM-00-05-009-62
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-stats", "PM-00-05-009-62", stats.get(kpiKey));
} else if (kpiKey.equals("total_items")) {
// 表示从memcached服务启动到当前时间,系统存储过的所有对象的数量,包括目前已经从缓存中删除的对象 PM-00-05-009-63
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-stats", "PM-00-05-009-63", stats.get(kpiKey));
} else if (kpiKey.equals("bytes")) {
// 表示系统存储缓存对象所使用的存储空间,单位为字节 PM-00-05-009-64
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-stats", "PM-00-05-009-64", stats.get(kpiKey));
} else if (kpiKey.equals("curr_connections")) {
// 表示当前系统打开的连接数 PM-00-05-009-06
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-stats", "PM-00-05-009-06", stats.get(kpiKey));
} else if (kpiKey.equals("total_connections")) {
// 表示从memcached服务启动到当前时间,系统打开过的连接的总数 PM-00-05-009-08
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-stats", "PM-00-05-009-08", stats.get(kpiKey));
} else if (kpiKey.equals("cmd_get")) {
// 累积获取数据的数量 PM-00-05-009-65
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-stats", "PM-00-05-009-65", stats.get(kpiKey));
} else if (kpiKey.equals("cmd_set")) {
// 累积保存数据的数量 PM-00-05-009-66
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-stats", "PM-00-05-009-66", stats.get(kpiKey));
} else if (kpiKey.equals("get_hits")) {
// 表示获取数据成功的次数 PM-00-05-009-67
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-stats", "PM-00-05-009-67", stats.get(kpiKey));
} else if (kpiKey.equals("get_misses")) {
// 表示获取数据失败的次数 PM-00-05-009-68
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-stats", "PM-00-05-009-68", stats.get(kpiKey));
} else if (kpiKey.equals("evictions")) {
// 为了给新的数据项目释放空间,从缓存移除的缓存对象的数目 PM-00-05-009-69
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-stats", "PM-00-05-009-69", stats.get(kpiKey));
} else if (kpiKey.equals("bytes_read")) {
// memcached服务器从网络读取的总的字节数 PM-00-05-009-70
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-stats", "PM-00-05-009-70", stats.get(kpiKey));
} else if (kpiKey.equals("bytes_written")) {
// memcached服务器发送到网络的总的字节数 PM-00-05-009-71
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-stats", "PM-00-05-009-71", stats.get(kpiKey));
} else if (kpiKey.equals("limit_maxbytes")) {
// memcached服务缓存允许使用的最大字节数 PM-00-05-009-72
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-stats", "PM-00-05-009-72", stats.get(kpiKey));
}
}
}
} catch (Exception e) {
logger.error("get memcached status is error", e);
} finally {
release();
}
return collResult.KPISet;
}
public Vector<TblATO_KPIDETAIL> getSlabs(HashMap<String, String> params) {
logger.info("Begin getSlabs.....");
CollBase collResult = new CollBase();
String pro_PRE_UNITID = KBP_CLASS + "-12";
init(params);
try {
Map<InetSocketAddress, Map<String, String>> result = memcachedClient.getStatsByItem("slabs");
for (InetSocketAddress isa : result.keySet()) {
Map<String, String> stats = result.get(isa);
for (String kpiKey : stats.keySet()) {
// System.out.println("【" + kpiKey + "】【" + stats.get(kpiKey) + "】");
if (kpiKey.equals("total_malloced")) {
// 已经分配给slab的字节数 PM-00-05-009-73
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-slabs", "PM-00-05-009-73", stats.get(kpiKey));
} else if (kpiKey.equals("active_slabs")) {
// 已经分配的slab数量 PM-00-05-009-74
collResult.addKPI(pro_PRE_UNITID + ":" + neat_mc_name + "-slabs", "PM-00-05-009-74", stats.get(kpiKey));
}
}
}
} catch (Exception e) {
logger.error("get memcached status is error", e);
} finally {
release();
}
return collResult.KPISet;
}
public Vector<TblATO_KPIDETAIL> getItems(HashMap<String, String> params) {
logger.info("Begin getItems.....");
CollBase collResult = new CollBase();
return collResult.KPISet;
}
}