CollFileWithCMD.java
12.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
package com.sitech.ismp.coll.host;
import com.sitech.ismp.coll.CollBase;
import com.sitech.ismp.coll.basic.TblATO_KPIDETAIL;
import com.sitech.util.Formater;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import java.io.*;
import java.util.*;
/**
* @author liush_bj 日期:2014-10-08
* 主机文件采集
*/
public class CollFileWithCMD extends CollBase {
static final String RESULT_PATH = "../result/";
static final String SH_COLL_FILE = "getFile.sh";
static final String RESULT_FILE = "fileResult";
public Vector<TblATO_KPIDETAIL> getFileChange(HashMap<String, String> params) {
logger.info("Begin getFileChange...");
logger.info(params.toString());
CollBase collResult = new CollBase();
// 执行脚本
exec(SH_COLL_FILE);
// 获得脚本执行结果
List<String> result = getResult(RESULT_FILE);
if(null == result || result.isEmpty()){
return collResult.KPISet;
}
List<List<String>> list = parseResult(result);
String deviceId = params.get("DEVICE_ID");
String UNIT_ID = params.get("KBP_CLASS");
if(null == deviceId || "".equals(deviceId.trim())){
// 获取主机名称 : uname -a
List<String> vHostName = list.get(0);
deviceId = split(vHostName.get(0), 1);
}
//kctune 系统参数变动监控
try{
List<String> netlogV = list.get(1);
String host_name = deviceId;
// 得到unit_id中使用的主机名称
String neat_host_name = Formater.neatenunitid(host_name);
//文件名称
collResult.addKPI(UNIT_ID + "-20:" + neat_host_name+"-kctune", "CM-10-10-020-01", "kctune");
Boolean isChange = checkFileChange(netlogV,"kctune",deviceId);
//变动部分有结果,则生成告警指标
collResult.addKPI(UNIT_ID + "-20:" + neat_host_name+"-kctune", "FM-10-10-020-01",isChange.toString());
} catch (Exception e) {
e.getMessage();
e.printStackTrace();
} finally {
}
try {
//得到filechange UnitId
String filechangeUnitId = UNIT_ID + "-20:" + deviceId + "-";
/**
* 分析传入的文件绝对路径参数
* 如果传入的参数中是路径则采集该路径下所有的文件
* 如果传入的参数中包含文件名则采集该文件
*/
for(int n=2;n <list.size();n++){
List<String> paths = list.get(n);
if(paths.size()<2) continue;//如果某条指令结果不正常,跳过;
String p = paths.get(0);
//文件名称
collResult.addKPI(filechangeUnitId+p, "CM-10-10-020-01", p);
Boolean isChange = checkFileChange(paths,p,deviceId);
//变动部分有结果,则生成告警指标
collResult.addKPI(filechangeUnitId+ p, "FM-10-10-020-01",isChange.toString());
}
}catch (Exception e) {
logger.error("Exception while get fileChange!",e);
}
logger.info("END COLL fileChange ");
return collResult.getKPISet();
}
private Boolean checkFileChange(List result,String fileName,String deviceId){
Boolean isChange = false;
try{
String now = this.getVetorString(result);
String data_path = "../filechange";
fileName = StringUtils.replace(fileName,"/","-");
String now_filename = "now_" + deviceId + "-"+fileName+".dat";
String tmp_filename = "tmp_" + deviceId + "-"+fileName+".dat";
String diff_filename = "diff_" + deviceId + "-"+fileName+".dat";
//目录不存在则创建
File f = new File(data_path);
if(!f.exists()){
f.mkdir();
}
File now_file = new File(data_path+"/" + now_filename);// 最新日志信息
File tmp_file = new File(data_path+"/" + tmp_filename);// 上次日志信息
File diff_file = new File(data_path+"/" + diff_filename);// 增量日志
PrintWriter write_now = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(now_file), "GB2312"));
PrintWriter write_tmp = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(tmp_file, true), "GB2312"));
PrintWriter write_diff = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(diff_file), "GB2312"));
String path = now_file.getAbsolutePath();
logger.info("Current path is " + path);
// 日志解析
if (tmp_file.exists() && tmp_file.length() > 0) {// 非第一次采集
write_now.write(now);
write_now.flush();
write_now.close();
// 读取上次日志文件(tmp)
FileReader tmp_reader = new FileReader(tmp_file);
BufferedReader tmp = new BufferedReader(tmp_reader);
String line;
String tmp_file_string = "";
while ((line = tmp.readLine()) != null) {
tmp_file_string += line + "\n";
}
tmp.close();
tmp_reader.close();
// 读取本次日志文件(now),生成差异文件(diff)
FileReader now_reader = new FileReader(now_file);
BufferedReader br = new BufferedReader(now_reader);
while ((line = br.readLine()) != null) {
if (tmp_file_string.indexOf(line) == -1)
write_diff.write(line);
else
continue;
}
br.close();
now_reader.close();
write_diff.flush();
write_diff.close();
write_tmp = new PrintWriter(new OutputStreamWriter(
new FileOutputStream(tmp_file), "GB2312"));
write_tmp.write(now);
write_tmp.flush();
write_tmp.close();
// 生成指标
FileReader diff_reader = new FileReader(diff_file);
BufferedReader diff = new BufferedReader(diff_reader);
//变动部分有结果,则生成告警指标
if (diff.readLine() != null) {
isChange =true;
}
diff.close();
diff_reader.close();
} else {// 第一次采集
write_tmp.write(now);
write_tmp.flush();
write_tmp.close();
write_diff.write(now);
write_diff.flush();
write_diff.close();
}
}catch (Exception e) {
logger.error("Exception while get fileChange!",e);
}finally {
return isChange;
}
}
public String getVetorString(List v) {
StringBuffer sb = new StringBuffer();
if (v != null && v.size() > 0) {
for (int i = 0; i < v.size(); i++) {
String s = (String) v.get(i);
if (s != null && i < v.size() - 1) {
sb.append(s + "\n");
}
}
}
return sb.toString();
}
/**
* 通知执行脚本
* 通知方式:写一个文件(文件名为采集脚本名称,如:getCpu.sh、getMem.sh)到notice目录,
* contab会定时执行脚本扫描该目录,得到文件名,并执行脚本
*
* @throws
* @since Ver 1.1
*/
private void exec(String shellName) {
String noticePath = "../notice/ibm/";
String noticeFileName = noticePath + shellName;
PrintWriter kpiFileStream = null;
try {
kpiFileStream = new PrintWriter(new FileWriter(Formater.replaceSpace(noticeFileName), false), true);
kpiFileStream.println(shellName);
} catch (IOException e) {
logger.error("[HP COLL] Exception while exec(" + shellName + ")", e);
} finally {
if (kpiFileStream != null) {
kpiFileStream.flush();
kpiFileStream.close();
}
}
}
/**
* 获得脚本执行结果
*
* @throws
* @since Ver 1.1
*/
private List<String> getResult(String resultFileName) {
try {
Thread.sleep(20 * 1000L);
} catch (InterruptedException e) {
logger.warn("[HP COLL] InterruptedException.");
}
StringBuffer sb = new StringBuffer();
// 超时时间:2分钟
long timeout = 3 * 60 * 1000L;
List<String> result = new ArrayList<String>();
long beginTime = new Date().getTime();
File file = new File(RESULT_PATH);
while (true) {
if (new Date().getTime() - beginTime > timeout) {
// 超时退出
logger.warn("[HP COLL] Timeout, exit.");
break;
}
File[] filelist = file.listFiles();
if (filelist == null || filelist.length == 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
logger.error("[HP COLL] InterruptedException", e);
}
continue;
}
File resultFile = null;
// 根据文件名找到采集文件
for (int i = 0; i < filelist.length; i++) {
if (filelist[i].getName().startsWith(resultFileName)) {
resultFile = filelist[i];
break;
}
}
if (resultFile != null) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(resultFile));
String line = "";
while ((line = br.readLine()) != null) {
result.add(line);
sb.append(line + "\n");
}
break;
} catch (Exception e) {
logger.error("[HP COLL] Exception while readFile:" + resultFileName, e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
logger.error("[HP COLL] IOException:", e);
}
}
logger.info("[HP COLL] Delete coll result file : " + resultFile.getName());
logger.info(RESULT_PATH + ":\n" + sb.toString());
resultFile.delete();
}
}
}// end while
return result;
}
/**
* 解析每个命令的执行结果
*
* @throws
* @since Ver 1.1
*/
private List<List<String>> parseResult(List<String> result) {
//计算结果有多少个分割线分割的集合
int num =1;
for (String line : result) {
if (line.trim().equals(SPLIT_LINE) || line.trim().endsWith(SPLIT_LINE)){
num++;
}
}
List<List<String>> list = new ArrayList<List<String>>();
for (int i = 0; i < num; i++) {
list.add(new ArrayList<String>());
}
int i = 0;
for (String line : result) {
if (line.trim().equals(SPLIT_LINE)) {
i++;
continue;
}else if(line.trim().endsWith(SPLIT_LINE)){
line = line.replace(SPLIT_LINE,"");
list.get(i).add(line);
i++;
}else{
list.get(i).add(line);
}
}
return list;
}
public static void main(String[] args){
CollFileWithCMD cmd = new CollFileWithCMD();
// String deviceId = "11:22";
// String s1= "/ssdbomc";
// System.out.println(deviceId.substring(deviceId.indexOf(":")+1,deviceId.length()));
// System.out.println(s1.split("/")[s1.split("/").length-1]);
HashMap<String, String> params = new HashMap<String, String>();
//
// params.put("IP_ADDR", "172.21.0.203");
// params.put("PASSWORD", "a1a50f36f1303ba3ed36f2b019e508f97628ee3c065f0984");
// params.put("PROTOCOL", "ssh");
// params.put("USERNAME", "ssdbomc");
// params.put("FILEPATH", "/ssdbomc/csdppd_mysql/cs/web_env/apache-tomcat-6.0/logs/catalina.out");
// params.put("UNIT_ID", "10-10-24:172_21_0_203");
// params.put("DEVICENAME", "172_21_0_203");
// cmd.getFileChange(params);
Vector<TblATO_KPIDETAIL> result = cmd.getFileChange(params);
for (int i = 0; i < result.size(); i++) {
TblATO_KPIDETAIL record = (TblATO_KPIDETAIL) result.get(i);
System.out.println(record.UNIT_ID + "\t" + record.KPI_ID + "\t" + record.KPI_VALUE);
}
}
}