CollTimestenWithCMD.java
10.9 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package com.sitech.ismp.coll;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Vector;
import org.apache.log4j.Logger;
public class CollTimestenWithCMD {
private Logger logger = Logger.getLogger("COLL");
private String KBP_ID = "10-11-34";
private String TTSERVER;
private String CMD_DIR;
private String IP_ADDR;
private String PORT;
private String USERNAME;
private String PASSWORD;
private String LOG_DIR;
private boolean IS_DIR = true;
private boolean IS_LOG_DIR = true;
/**
* 初始化数据库链接
*/
public void initParams(HashMap params) {
this.TTSERVER = (String) params.get("TTSERVER");
this.CMD_DIR = (String) params.get("CMD_DIR");
this.IP_ADDR = (String) params.get("IP_ADDR");
this.PORT = (String) params.get("PORT");
this.USERNAME = (String) params.get("USERNAME");
this.PASSWORD = (String) params.get("PASSWORD");
this.LOG_DIR = (String) params.get("LOG_DIR");
File file = new File(CMD_DIR);
if (!file.exists() || !file.isDirectory()) {
logger.info("CMD_DIR is not a directory");
this.IS_DIR = false;
}
File logsfile = new File(LOG_DIR);
if (!logsfile.exists() || !logsfile.isDirectory()) {
logger.info("LOG_DIR is not a directory");
this.IS_LOG_DIR = false;
}
}
/**
* 执行命令
*/
private Vector execute(String shellCommand) {
logger.info("cmd: " + shellCommand);
Vector vResult = null;
try {
Process process = Runtime.getRuntime().exec(shellCommand);
vResult = new Vector();
BufferedReader reader = null;
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
logger.info("line: " + line);
while (line != null) {
vResult.addElement(line);
line = reader.readLine();
}
reader.close();
return vResult;
} catch (Exception e) {
logger.info("在本机上可能没有 " + shellCommand + " 命令!");
} finally {
Runtime.getRuntime().freeMemory();
Runtime.getRuntime().gc();
}
return vResult;
}
/**
* 获取配置数据
*/
public Vector getConfig(HashMap params) {
initParams(params);
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
String config_PRE_UNITID = this.KBP_ID + "-10";
if (IS_DIR) {
// 采集数据库版本和位数 ttVersion
String timesten_version_cmd = this.CMD_DIR
+ System.getProperty("file.separator") + "ttVersion";
/**
* TimesTen Release 7.0.6.2.0 (32 bit NT) (tt70_32:17000) 2010-06-16T07:34:37Z
* Instance admin: mooker
* Instance home directory: F:\TimesTen\tt70_32
* Daemon home directory: F:\TimesTen\tt70_32\srv\info
* Access control enabled.
*/
Vector version = this.execute(timesten_version_cmd);
String vesrion = "unkown";
String bitinfo = "32";
if (version != null && version.size() > 0) {
logger.info("size is not 0");
for (int i = 0; i < version.size(); i++) {
String res = (String) version.get(i);
if (res.indexOf("Release") >= 0) {
String[] ver = res.split(" ");
if (ver != null && ver.length > 0) {
vesrion = ver[2];
bitinfo = ver[3].substring(1);
}
}
}
} else {
logger.info("size is 0");
}
// CM-00-03-001-01 数据库名
collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total",
"CM-00-03-001-01", this.TTSERVER);
// CM-00-03-001-02 数据库版本
collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total",
"CM-00-03-001-02", vesrion);
// CM-00-03-001-03 数据库位数
collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total",
"CM-00-03-001-03", bitinfo + "bit");
}
return collResult.getKPISet();
}
/**
* 获取性能数据
*
* @param params
* @return
*/
public Vector getPerformance(HashMap params) {
return null;
}
/**
* 监控锁
*/
public Vector getLockInfo(HashMap params) {
initParams(params);
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
String config_PRE_UNITID = this.KBP_ID + "-11";
if (IS_DIR) {
String timesten_lock_cmd = this.CMD_DIR
+ System.getProperty("file.separator") + "ttxact.sh";
logger.info("timesten_lock_cmd is " + timesten_lock_cmd);
Vector lock_v = this.execute(timesten_lock_cmd);
// 共享锁数
int alock_total = 0;
// 更新锁数
int block_total = 0;
// 独占锁数
int clock_total = 0;
// 系统锁
int dlock_total = 0;
if (lock_v != null && lock_v.size() > 0) {
for (int i = 0; i < lock_v.size(); i++) {
String res = (String) lock_v.get(i);
if (res.indexOf("Command") >= 0) {
String type = collResult.split(res, 2);
if (type.equals("S")) {
alock_total++;
}
}
if (res.indexOf("Row") >= 0) {
block_total++;
}
if (res.indexOf("Xn") >= 0) {
String type = collResult.split(res, 0);
String flag = collResult.split(res, 2);
String info = collResult.split(res, 3);
clock_total++;
logger.info("type+flag+info: " + type + " " + flag + " " + info);
}
if (res.indexOf("SYS.TABLES") >= 0) {
dlock_total++;
}
}
}
// logger.info("alock_total " + alock_total);
// logger.info("block_total " + block_total);
// logger.info("clock_total " + clock_total);
// logger.info("dlock_total " + dlock_total);
// PM-00-03-008-01 总锁数
collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total",
"PM-00-03-008-01", String.valueOf(block_total + clock_total + dlock_total));
// PM-00-03-008-17 共享锁数
collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total",
"PM-00-03-008-17", String.valueOf(alock_total));
// PM-00-03-008-18 更新锁数
collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total",
"PM-00-03-008-18", String.valueOf(block_total));
// PM-00-03-008-19 独占锁数
collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total",
"PM-00-03-008-19", String.valueOf(clock_total));
// PM-00-03-008-20 系统锁
collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total",
"PM-00-03-008-20", String.valueOf(dlock_total));
}
return collResult.getKPISet();
}
/**
* 实时连接数 ttstatus | grep Server | wc -l
*/
public Vector getLinkedNum(HashMap params) {
initParams(params);
CollBase collResult = new CollBase();
String config_PRE_UNITID = this.KBP_ID + "-10";
int linknum = 0;
if (IS_DIR) {
String timesten_link_cmd = this.CMD_DIR
+ System.getProperty("file.separator") + "ttstatus";
logger.info("timesten_link_cmd: " + timesten_link_cmd);
Vector linknum_v = this.execute(timesten_link_cmd);
if (linknum_v != null && linknum_v.size() > 0) {
for (int i = 0; i < linknum_v.size(); i++) {
String res = (String) linknum_v.get(i);
if (res.indexOf("Server") >= 0) {
linknum++;
}
}
}
}
logger.info(String.valueOf(linknum));
// FM-00-03-001-09 实时连接数
collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total",
"FM-00-03-001-09", String.valueOf(linknum));
return collResult.getKPISet();
}
/**
* 事务采集
*/
public Vector getTransaction(HashMap params) {
initParams(params);
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
String config_PRE_UNITID = this.KBP_ID + "-12";
if (IS_DIR) {
String timesten_trans_cmd = this.CMD_DIR
+ System.getProperty("file.separator") + "ttlogholds.sh";
logger.info("timesten_trans_cmd is " + timesten_trans_cmd);
Vector trans_v = this.execute(timesten_trans_cmd);
if (trans_v != null && trans_v.size() > 0) {
int rowid = 1;
for (int i = 0; i < trans_v.size(); i++) {
String res = (String) trans_v.get(i);
if (res.indexOf("<") == 0) {
String[] info = res.split(",");
String index = (String) info[0].substring(1).trim();
String seq = (String) info[1].trim();
String tranname = (String) info[2].trim();
String xid = (String) info[3].substring(0, info[3].length() - 1).trim();
logger.info("<>" + index + " " + seq + " " + tranname + " " + xid);
// FM-00-04-003-04 日志序列号
collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-" + rowid,
"FM-00-04-003-04", index);
// FM-00-04-003-05 事务类型
collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-" + rowid,
"FM-00-04-003-05", tranname);
// FM-00-04-003-06 事务说明
collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-" + rowid,
"FM-00-04-003-06", xid);
rowid++;
}
}
}
}
return collResult.getKPISet();
}
/**
* standby复制监测
*/
public Vector getStandby(HashMap params) {
initParams(params);
TTSQLTarget collectiondbbase = new TTSQLTarget();
collectiondbbase.init(IP_ADDR, PORT, USERNAME, PASSWORD, TTSERVER);
getStandby(collectiondbbase);
Vector v = collectiondbbase.getKPISet();
collectiondbbase.releaseResources();
return v;
}
private void getStandby(TTSQLTarget collectiondbbase) {
String config_PRE_UNITID = this.KBP_ID + "-10";
Vector v = collectiondbbase.getSQLKPIInfos("{call ttRepStateGet}");
for (int i = 0; i < v.size(); i++) {
Vector row = (Vector) v.get(i);
String repstate = (String) row.get(0);
logger.info(repstate);
// 复制状态
collectiondbbase.addKPI(config_PRE_UNITID,
"PM-00-03-900-01", repstate);
}
}
/**
* 复制异常
*/
public Vector getDSLog(HashMap params) {
logger.info("collect error");
initParams(params);
CollBase collResult = new CollBase();
String config_PRE_UNITID = this.KBP_ID + "-10";
int logsnum = 0;
logger.info("IS_LOG_DIR" + this.LOG_DIR + " " + IS_LOG_DIR);
if (IS_LOG_DIR) {
String timesten_log_cmd = "ls " + this.LOG_DIR;
logger.info("timesten_log_cmd: " + timesten_log_cmd);
Vector linknum_v = this.execute(timesten_log_cmd);
for (int i = 0; i < linknum_v.size(); i++) {
String row = (String) linknum_v.get(i);
logger.info("tempinfo: " + row);
if (row.indexOf("log") != -1) {
logsnum++;
}
}
// logsnum = linknum_v.size();
}
logger.info("大于2告警: " + logsnum);
// PM-00-03-900-02 复制异常
collResult.addKPI(config_PRE_UNITID + ":" + this.TTSERVER + "-total",
"PM-00-03-900-02", String.valueOf(logsnum));
return collResult.getKPISet();
}
/**
* @param args
*/
public static void main(String[] args) {
HashMap params = new HashMap();
// params.put("TTSERVER", "test");
// params.put("CMD_DIR", "F:\\TimesTen\\tt70_32\\bin");
// params.put("ID_ADDR", "127.0.0.1");
// params.put("LOG_DIR", "F:\\TimesTen\\ds");
params.put("TTSERVER", "abm");
params.put("CMD_DIR", "/ibnms/masteragent/data");
params.put("ID_ADDR", "192.168.10.61");
params.put("LOG_DIR", "/tt/DS");
CollTimestenWithCMD cmd = new CollTimestenWithCMD();
cmd.getTransaction(params);
// cmd.getStandby(params);
// cmd.getLinkedNum(params);
// cmd.getLockInfo(params);
// cmd.getDSLog(params);
}
}