CollIdmdbByCMD.java
12.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
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 com.sitech.util.Formater;
import com.sitech.util.upload.RomoteController;
import com.sitech.util.upload.SSHThread;
import com.sitech.util.upload.TelnetThread;
import org.apache.log4j.Logger;
import java.util.*;
/**
* Created by mooker on 2014/8/29.
*/
public class CollIdmdbByCMD {
private static Logger logger = Logger.getLogger("COLL");
String KBP_CLASS = "10-32-16";
RomoteController tt = null;
String dbserver = "";
public Vector<TblATO_KPIDETAIL> getSystem(HashMap<String, String> params) {
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
// 登陆执行命令,获取结果进行解析
init(params);
if (!tt.isAuthorized()) {
return collResult.KPISet;
}
// 数据库名 CM-00-03-001-08
collResult.addKPI(this.KBP_CLASS + "-10:" + this.dbserver + "-SYS", "CM-00-03-001-08", dbserver);
// 网元厂商 CM-00-02-001-02
collResult.addKPI(this.KBP_CLASS + "-10:" + this.dbserver + "-SYS", "CM-00-02-001-02", "SI-TECH");
Vector<String> vNodeStatus = tt.getResultAsVector(". ./.bash_profile;dmdb_status -v");
if (!vNodeStatus.isEmpty()) {
for (int i = 5; i < vNodeStatus.size(); i++) {
String nodeStatus = (String) vNodeStatus.elementAt(i);
if (nodeStatus != null && nodeStatus.contains("stty:")) {
continue;
}
if (nodeStatus != null && !nodeStatus.startsWith("db")) {
continue;
}
String nodeName = split(nodeStatus, 0);
String mainDb = split(nodeStatus, 1);
String slaveDb = split(nodeStatus, 2);
String mainDbStatus = split(nodeStatus, 3);
String slaveDbStatus = split(nodeStatus, 4);
String _unitId = this.KBP_CLASS + "-21:" + this.dbserver + "-" + Formater.neatenunitid(nodeName);
// 节点名称 CM-00-03-011-01
collResult.addKPI(_unitId, "CM-00-03-011-01", nodeName);
// 节点主库 CM-00-03-011-02
collResult.addKPI(_unitId, "CM-00-03-011-02", mainDb);
// 节点备库 CM-00-03-011-03
collResult.addKPI(_unitId, "CM-00-03-011-03", slaveDb);
// 节点主库状态 FM-00-03-001-10
collResult.addKPI(_unitId, "FM-00-03-001-10", mainDbStatus);
// 节点备库状态 FM-00-03-001-11
collResult.addKPI(_unitId, "FM-00-03-001-11", slaveDbStatus);
}
}
} catch (Exception e) {
logger.error("", e);
} finally {
release();
}
return collResult.getKPISet();
}
public Vector<TblATO_KPIDETAIL> getSession(HashMap<String, String> params) {
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
// 登陆执行命令,获取结果进行解析
init(params);
if (!tt.isAuthorized()) {
return collResult.KPISet;
}
String PRE_UNIT_ID = this.KBP_CLASS + "-22:" + this.dbserver;
Vector<String> vNodeSession = tt.getResultAsVector(". ./.bash_profile;dmdb_status -s | grep ^$ -B2 | sed -e '/Error/i\\--' | sed -e '/Error/a\\session 0'");
if (!vNodeSession.isEmpty()) {
int startIndex = 0;
for (int j = 0; j < vNodeSession.size(); j++) {
String info = (String) vNodeSession.elementAt(j);
if (info != null && info.contains("#####")) {
startIndex = j;
break;
}
}
for (int k = 0; k < startIndex + 2; k++) {
vNodeSession.remove(0);
}
System.out.println("vNodeSession.size() is " + vNodeSession.size());
for (int i = 0; i < (vNodeSession.size() + 1) / 4; i++) {
String nodeName = (String) vNodeSession.elementAt(i * 4 + 1);
String nodeSession = (String) vNodeSession.elementAt(i * 4 + 2);
if ((nodeName != null && nodeName.contains("stty:")) || (nodeSession != null && nodeSession.contains("stty:"))) {
continue;
}
String _nodeName = split(nodeName, 0);
String _nodeSession = split(nodeSession, 1);
String _unitId = PRE_UNIT_ID + "-" + Formater.neatenunitid(_nodeName);
// 会话数量 PM-00-03-009-03
collResult.addKPI(_unitId, "PM-00-03-009-03", String.valueOf(_nodeSession));
}
}
} catch (Exception e) {
logger.error("", e);
} finally {
release();
}
return collResult.getKPISet();
}
public Vector<TblATO_KPIDETAIL> getPerform(HashMap<String, String> params) {
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
try {
init(params);
if (!tt.isAuthorized()) {
return collResult.KPISet;
}
String PRE_UNIT_ID = this.KBP_CLASS + "-22:" + this.dbserver;
Vector<String> vNodeMaxClient = tt.getResultAsVector(". ./.bash_profile;dmdb_param get MAX_CLIENT | grep -v ^$ | awk -F ' ' '{print $1\" \"$5}'");
Map<String, String> nodeMap = new HashMap<String, String>();
if (!vNodeMaxClient.isEmpty()) {
for (int i = 0; i < vNodeMaxClient.size(); i++) {
String nodeMaxClient = (String) vNodeMaxClient.elementAt(i);
if (nodeMaxClient != null && nodeMaxClient.contains("stty:")) {
continue;
}
String nodeName = split(nodeMaxClient, 0);
nodeMap.put(nodeName, nodeName);
String maxClientCfg = split(nodeMaxClient, 1);
String _unitId = PRE_UNIT_ID + "-" + Formater.neatenunitid(nodeName);
// 配置用户连接数 CM-00-03-007-02
collResult.addKPI(_unitId, "CM-00-03-007-02", maxClientCfg);
}
}
Vector<String> vNodeMemory = tt.getResultAsVector(". ./.bash_profile;dmdb_status -m | grep db | awk '{a[$1]+=$4;b[$1]+=$5}END{for(i in a)print i,a[i],b[i]}'");
if (!vNodeMemory.isEmpty()) {
for (int i = 0; i < vNodeMemory.size(); i++) {
String nodeMemory = (String) vNodeMemory.elementAt(i);
if (nodeMemory != null && nodeMemory.contains("stty:")) {
continue;
}
String nodeName = split(nodeMemory, 0);
if (nodeMap.containsKey(nodeName)) {
nodeMap.remove(nodeName);
}
String totalMemory = split(nodeMemory, 1);
String usedMemory = split(nodeMemory, 2);
Double usedRate = Double.parseDouble(usedMemory) * 100 / Double.parseDouble(totalMemory);
String _unitId = PRE_UNIT_ID + "-" + Formater.neatenunitid(nodeName);
// 节点名称 CM-00-03-011-01
collResult.addKPI(_unitId, "CM-00-03-011-01", nodeName);
// 数据库使用最大内存 CM-00-03-007-01
collResult.addKPI(_unitId, "CM-00-03-007-01", totalMemory);
// 已消耗的主机内存 PM-00-01-002-12
collResult.addKPI(_unitId, "PM-00-01-002-12", usedMemory);
// 内存使用率 PM-00-01-002-01
collResult.addKPI(_unitId, "PM-00-01-002-01", Formater.formatDecimalByScale(String.valueOf(usedRate), 4));
}
for (Iterator iter = nodeMap.keySet().iterator(); iter.hasNext(); ) {
String _key = (String) iter.next();
String _unitId = PRE_UNIT_ID + "-" + Formater.neatenunitid(_key);
// 节点名称 CM-00-03-011-01
collResult.addKPI(_unitId, "CM-00-03-011-01", _key);
// 数据库使用最大内存 CM-00-03-007-01
collResult.addKPI(_unitId, "CM-00-03-007-01", "0");
// 已消耗的主机内存 PM-00-01-002-12
collResult.addKPI(_unitId, "PM-00-01-002-12", "0");
// 内存使用率 PM-00-01-002-01
collResult.addKPI(_unitId, "PM-00-01-002-01", "0.0");
}
}
//主备库复制进程信息
Vector<String> NodeStatus = tt.getResultAsVector(". ./.bash_profile;dmdb_status -d");
if (!NodeStatus.isEmpty()) {
for (int i = 5; i < NodeStatus.size(); i++) {
String nodeStatus = (String) NodeStatus.elementAt(i);
if (nodeStatus != null && nodeStatus.contains("stty:")) {
continue;
}
if (nodeStatus != null && !nodeStatus.startsWith("db")) {
continue;
}
String nodeName = split(nodeStatus, 0);
String mainDbStatus = split(nodeStatus, 1);
String slaveDbStatus = split(nodeStatus, 2);
String _unitId = this.KBP_CLASS + "-23:" + this.dbserver + "-" +nodeName;
// 节点名称 CM-00-03-011-01
collResult.addKPI(_unitId, "CM-00-03-011-01", nodeName);
// 节点主库状态 FM-00-03-001-10
collResult.addKPI(_unitId, "FM-00-03-001-10", mainDbStatus);
// 节点备库状态 FM-00-03-001-11
collResult.addKPI(_unitId, "FM-00-03-001-11", slaveDbStatus);
}
}
} catch (Exception e) {
logger.error("", e);
} finally {
release();
}
return collResult.getKPISet();
}
public void init(HashMap params) {
String ipAddr = (String) params.get("IP_ADDR");
String username = (String) params.get("USERNAME");
String password = (String) params.get("PASSWORD");
password = DES3.decrypt(password);
String protocol = (String) params.get("PROTOCOL");
dbserver = (String) params.get("DBNAME");
this.dbserver = Formater.neatenunitid(dbserver);
String protocolPort = null;
if (params.get("PROTOCOL_PORT") != null) {
protocolPort = ((String) params.get("PROTOCOL_PORT")).trim();
}
if (protocol != null && protocol.equals("telnet")) {
if (null != protocolPort && !"".equals(protocolPort)) {
tt = new TelnetThread(ipAddr, Integer.parseInt(protocolPort), username, password);
} else {
tt = new TelnetThread(ipAddr, 23, username, password);
}
} else {
if (null != protocolPort && !"".equals(protocolPort)) {
tt = new SSHThread(ipAddr, Integer.parseInt(protocolPort), username, password);
} else {
tt = new SSHThread(ipAddr, 22, username, password);
}
}
tt.initial();
}
public void release() {
if (tt != null) {
tt.close();
tt = null;
}
System.gc();
}
public String split(String str, int pos) {
StringTokenizer st = new StringTokenizer(str);
Vector values = new Vector();
while (st.hasMoreTokens()) {
values.add(st.nextToken());
}
if (pos >= 0 && pos < values.size()) {
return (String) values.elementAt(pos);
}
return "";
}
}