CollSymantecSFWithCMDByCron.java
13.5 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
354
355
356
357
358
359
360
361
362
363
364
365
package com.sitech.ismp.coll.storage.symantec;
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.*;
public class CollSymantecSFWithCMDByCron extends CollBase {
static final String RESULT_PATH = "../result/";
static final String SH_COLL_SFINFO = "getSFInfo.sh";
static final String RESULT_SFINFO = "SFInfoResult";
String PRE_UNITID = "10-14-12"; //赛门铁克标识
public Vector<TblATO_KPIDETAIL> getSFInfo(HashMap<String, String> params) {
logger.info("begin getSymantecSFInfo");
// 保存采集结果,并返回值
CollBase collResult = new CollBase();
// 执行脚本---------因脚本部分指令需root权限,故脚本执行由root用户crontab执行。
// exec(SH_COLL_SFINFO);
// 获得脚本执行结果
List<String> result = getResult(RESULT_SFINFO);
if(null == result || result.isEmpty()){
return collResult.KPISet;
}
List<List<String>> list = parseResult(result, 9);
String deviceId = params.get("DEVICE_ID");
if(null == deviceId || "".equals(deviceId.trim())){
// 获取主机名称 : uname -a
List<String> vHostName = list.get(0);
deviceId = split(vHostName.get(0), 1);
}
/*---------------1.心跳状态检查-----------------*/
try {
String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId) + "-total";
List<String> heartbeat_result = list.get(1);
logger.info("---------------heartbeat start-------------");
if(null == heartbeat_result || heartbeat_result.size() < 1){
throw new Exception("\"/sbin/lltstat -nvv configured\": null");
}
int count = 0;
for (int i=2;i<heartbeat_result.size();i++){
String temp = heartbeat_result.get(i);
if(temp.contains("UP")){
count++;
}
}
//心跳状态检查(UP行数)
collResult.addKPI(memUnitId, "PM-00-06-001-09", Integer.toString(count));
} catch (Exception e) {
logger.error("Exception while getHeartbeatInfo!", e);
}
/*---------------2.Gabconfig端口检查-----------------*/
try {
String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId) + "-total";
List<String> gabconfig_result = list.get(2);
logger.info("---------------gabconfig start-------------");
if(null == gabconfig_result || gabconfig_result.size() < 1){
throw new Exception("\"/sbin/gabconfig -a\": null");
}
int count = 0;
for (int i=2;i<gabconfig_result.size();i++){
String temp = gabconfig_result.get(i);
if(temp.startsWith("Port")){
count++;
}
}
//Gabconfig端口检查(Port个数)
collResult.addKPI(memUnitId, "PM-00-06-001-10", Integer.toString(count));
} catch (Exception e) {
logger.error("Exception while getGabconfigInfo!", e);
}
/*---------------3.磁盘链路状态检查-----------------*/
try {
String memUnitId = PRE_UNITID + "-11:" + Formater.neatenunitid(deviceId);
List<String> dmpnode_result = list.get(3);
logger.info("---------------dmpnode start-------------");
if(null == dmpnode_result || dmpnode_result.size() < 1){
throw new Exception("\"/usr/sbin/vxdmpadm getdmpnode\": null");
}
for (int i=2;i<dmpnode_result.size();i++){
String temp = dmpnode_result.get(i);
if(!split(temp,5).startsWith("0")){
String name = split(temp,0);
collResult.addKPI(memUnitId + "-" +name, "CM-00-06-011-01", name+"#"+split(temp,6));
collResult.addKPI(memUnitId + "-" +name, "PM-00-06-001-11", split(temp,5));
}
}
} catch (Exception e) {
logger.error("Exception while getdmpnodeInfo!", e);
}
/*---------------4.Volume状态检查-----------------*/
try {
String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId) + "-total";
List<String> volume_result = list.get(4);
logger.info("---------------volume start-------------");
if(null == volume_result || volume_result.size() < 1){
throw new Exception("\"vxprint -v\": null");
}
int count = 0;
for (int i=3;i<volume_result.size();i++){
String temp = volume_result.get(i);
if(temp.contains("DISABLE")){
count++;
}
}
//Volume状态检查(disable个数)
collResult.addKPI(memUnitId, "PM-00-06-001-05", Integer.toString(count));
} catch (Exception e) {
logger.error("Exception while getVolumeInfo!", e);
}
/*---------------5.vxconfigd 状态检查-----------------*/
try {
String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId) + "-total";
List<String> vxconfigd_result = list.get(5);
logger.info("-------------vxconfigd start-------------");
if(null == vxconfigd_result || vxconfigd_result.size() < 1){
collResult.addKPI(memUnitId, "PM-00-06-001-08", "0");
}else{
collResult.addKPI(memUnitId, "PM-00-06-001-08", Integer.toString(vxconfigd_result.size()));
}
} catch (Exception e) {
logger.error("Exception while getVxconfigdInfo!", e);
}
/*---------------6.disk group状态检查-----------------*/
try {
String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId) + "-total";
List<String> diskGroup_result = list.get(6);
logger.info("-------------diskGroup start-------------");
if(null == diskGroup_result || diskGroup_result.size() < 1){
throw new Exception("\"vxdg list\": null");
}
int count = 0;
for (int i=1;i<diskGroup_result.size();i++){
String temp = diskGroup_result.get(i);
if(temp.contains("disable")){
count++;
}
}
//DG状态检查(disable个数)
collResult.addKPI(memUnitId, "PM-00-06-001-12", Integer.toString(count));
} catch (Exception e) {
logger.error("Exception while getDiskGroupInfo!", e);
}
/*---------------7.VCS Log检查-----------------*/
try {
String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId);
List<String> VCSLog_result = list.get(7);
logger.info("-------------VCSLog start-------------");
int count = 0;
if(!(null == VCSLog_result || VCSLog_result.size() < 1)){
for (int i=1;i<VCSLog_result.size();i++){
count++;
}
}
//VCS日志当日错误个数
collResult.addKPI(memUnitId, "PM-00-06-001-06", Integer.toString(count));
} catch (Exception e) {
logger.error("Exception while getVCSLog!", e);
}
/*---------------8.DMP eventlog检查-----------------*/
try {
String memUnitId = PRE_UNITID + "-10:" + Formater.neatenunitid(deviceId);
List<String> DMPLog_result = list.get(8);
logger.info("-------------DMPLog start-------------");
int count = 0;
if(!(null == DMPLog_result || DMPLog_result.size() < 1)){
for (int i=1;i<DMPLog_result.size();i++){
count++;
}
}
//event.log日志检查
collResult.addKPI(memUnitId, "PM-00-06-001-07", Integer.toString(count));
} catch (Exception e) {
logger.error("Exception while getDMPLog!", e);
}
return collResult.getKPISet();
}
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("[SymantecSF 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("[SymantecSF 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("[SymantecSF COLL] Timeout, exit.");
break;
}
File[] filelist = file.listFiles();
if (filelist == null || filelist.length == 0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
logger.error("[SymantecSF 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("[SymantecSF COLL] Exception while readFile:" + resultFileName, e);
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
logger.error("[SymantecSF COLL] IOException:", e);
}
}
logger.info("[SymantecSF 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) {
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) {
HashMap<String, String > params = new HashMap<String, String>();
CollSymantecSFWithCMDByCron coll = new CollSymantecSFWithCMDByCron();
Vector<TblATO_KPIDETAIL> result = coll.getSFInfo(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);
}
// System.out.println("v lv_doeridxro fsgen ENABLED 31457280 - ACTIVE - -".contains("ENABLED"));
}
}