ShellExecutorUtil.java
3.61 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
package com.sitech.ismp.check.mbean;
import com.sitech.ismp.messageObject.ScheduleLog;
import com.sitech.ismp.messageObject.cc.CommandParameters;
import com.sitech.ismp.messageObject.cc.CommandResults;
import com.sitech.util.DateFormater;
import com.sitech.util.JSONUtil;
import com.sitech.util.mq.MQConstants;
import com.sitech.util.mq.TunnelFactory;
import org.apache.log4j.Logger;
import java.io.File;
import java.text.ParseException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* Created with IntelliJ IDEA.
* User: Linxc
* Date: 14-3-13
* Time: 上午10:28
* To change this template use File | Settings | File Templates.
*/
public class ShellExecutorUtil {
private static Logger logger = Logger.getLogger(ShellExecutorUtil.class);
public static final String Exec_Flag_Success = "1";
public static final String Exec_Flag_Failure = "-1";
public static Map<String, CommandParameters> cache = new ConcurrentHashMap<String, CommandParameters>();
public void parseResult(ScheduleLog schLog, ScheduleLog befLog) {
try {
CommandParameters req = cache.get(schLog.getRequestId());
CommandResults rsp = new CommandResults();
rsp.setTASK_ID(schLog.getSchId());
rsp.setTYPE(req.getTYPE());
rsp.setBEGIN_TIME(DateFormater.stringToDateTime((String)schLog.getExtInfo().get("SH_EXEC_TIME")));
rsp.setEND_TIME(DateFormater.stringToDateTime(schLog.getEndTime()));
rsp.setEXEC_LOG("执行指令结束\n");
rsp.setEXEC_RESULT(getExecResult(schLog, befLog));
rsp.setEXEC_FLAG(getExecFlag(schLog, befLog));
rsp.setFAILURE_REASION(getFailureReason(schLog, befLog));
rsp.setCUSTOMPROP(req.getCUSTOMPROP());
logger.info("Send CCCP Exec Result:" + JSONUtil.toJSON(rsp));
TunnelFactory.getTunnel(MQConstants.Q_CCAGENT_RESULT).writeData(rsp);
} catch (ParseException e) {
logger.error("Exception while parseResult:\n log1=" + JSONUtil.toJSON(schLog) + "\n log2=" + JSONUtil.toJSON(befLog), e);
}finally {
cache.remove(schLog.getRequestId());
File tmpScriptFile = new File(ShellExecutorThread.SCRIPT_FILE_DIR, schLog.getRequestId() + ".sh");
if(tmpScriptFile.exists() && tmpScriptFile.canWrite()){
logger.info("Delete Temp Script File " + tmpScriptFile.getAbsolutePath());
tmpScriptFile.delete();
}
}
}
private String getFailureReason(ScheduleLog schLog, ScheduleLog befLog) {
if ("err_out".equals(schLog.getType())) {
return schLog.getLogInfo();
} else if ("err_out".equals(befLog.getType())) {
return befLog.getLogInfo();
} else {
return "";
}
}
private String getExecResult(ScheduleLog schLog, ScheduleLog befLog) {
if ("std_out".equals(schLog.getType())) {
return schLog.getLogInfo();
} else if ("std_out".equals(befLog.getType())) {
return befLog.getLogInfo();
} else {
return "";
}
}
/**
* 脚本执行状态
* @param schLog
* @param befLog
* @return -1:失败 1:成功
*/
private String getExecFlag(ScheduleLog schLog, ScheduleLog befLog) {
if ("err_out".equals(schLog.getType()) && !"".equals(schLog.getLogInfo())) {
return Exec_Flag_Failure;
} else if ("err_out".equals(befLog.getType()) && !"".equals(befLog.getLogInfo())) {
return Exec_Flag_Failure;
} else {
return Exec_Flag_Success;
}
}
}