ShellCollThread.java
8.44 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
package com.sitech.ismp.coll.busi;
import com.sitech.base.AgentProperties;
import com.sitech.ismp.coll.busi.util.SystemUtil;
import com.sitech.ismp.messageObject.ScheduleLog;
import com.sitech.util.FileUtils;
import com.sitech.util.Formater;
import com.sitech.util.JSONUtil;
import com.sitech.util.mq.MQConstants;
import com.sitech.util.mq.TunnelFactory;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* ClassName:ShellCollThread
* Description: shell采集
*
* @author Linxc
* @version
* @since Ver 1.1
* @Date 2012 Feb 8, 2012 10:58:44 AM
*/
public class ShellCollThread {
private static Logger logger = Logger.getLogger("BUSI_COLL");
public static final String SEQ_0_START = "0";
public static final String SEQ_1_NOTICE = "1";
public static final String OPR_TYPE_SHELL = "6";
public static final String SCRIPT_DIR = SystemUtil.SHELL_SCRIPT_PATH;
public static final String TMP_NOTICE_DIR = AgentProperties.AGENT_HOME + "/script/busi/busi_tmp/";
private ShellCollParam props;
public ShellCollThread(HashMap<String, String> params) {
props = new ShellCollParam(params);
}
public void run() {
logger.info("[" + props.getShellName() + "] Start ShellCollThread...");
sendScheduleLogs("开始执行脚本" + props.getShellName(), props.getGuid(), SEQ_0_START, "", null);
// 1. 变量替换得到最终执行的shell, 并生成文件
writeScriptFile();
// 2. 创建通知执行脚本
writeNoticeFile();
logger.info("[" + props.getShellName() + "] End ShellCollThread...");
}
/**
* 变量替换得到最终执行的shell, 并生成文件
*/
private void writeScriptFile() {
try {
String shellContent = getShellContent();
FileUtils.writeFile(SCRIPT_DIR, props.getShellName(), shellContent, false);
logger.info("[" + props.getShellName() + "] Create file[" + props.getAbsShellName() + "], shellContent=\n" + shellContent);
} catch (Exception e) {
throw new RuntimeException("Exception while createScriptFile " + props.getAbsShellName(), e);
}
}
private void writeNoticeFile() {
try {
String noticeContent = getNoticeContent();
String fileName = "notice_" + props.getGuid() + ".sh";
logger.info("[" + props.getShellName() + "] Create TmpNoticeFile[" + fileName + "]");
FileUtils.writeFile(TMP_NOTICE_DIR, fileName, noticeContent, false);
logger.info("[" + props.getShellName() + "] Create NoticeFile[" + props.getNoticeDir() + fileName + "]");
FileUtils.createNewFile(props.getNoticeDir(), fileName);
} catch (Exception e) {
logger.info("[" + props.getShellName() + "] Exception While Create NoticeFile", e);
}
}
/**
* 变量替换得到最终执行的shell
* @throws
* @since Ver 2.0
*/
private String getShellContent() {
StringBuffer sb = new StringBuffer();
sb.append("UNIT_ID=\"" + props.getKbpClass() + "\"\n");
sb.append("SHELL_NAME=\"" + props.getShellName() + "\"\n");
sb.append("IP_ADDR=\"" + props.getIpAddr() + "\"\n");
// KPI替换
sb.append(parseKpis(props.getKpis()));
// 参数替换
sb.append(parseShellParams(props.getShellParams()));
// 宏替换
sb.append(parseShellMacros(props.getShellMacros()));
// 解析脚本内容
sb.append(parseShellContent(props.getShellContent()));
return sb.toString();
}
/**
* 解析KPI变量
* @param kpis
* JSON字符串,例:[{varName:'KPI1',kpiId:'PM-01-01-001-01'},{varName:'KPI2',kpiId:'PM-01-01-001-02'}]
* @return
* KPI1=PM-01-01-001-01
* KPI2=PM-01-01-001-02
*/
private String parseKpis(String kpis) {
StringBuffer sb = new StringBuffer();
if (null == kpis || "".equals(kpis)) {
return sb.toString();
}
JSONArray jsonArray = (JSONArray) JSONUtil.fromJSON(kpis);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject temp = (JSONObject) jsonArray.get(i);
String paramName = temp.getString("varName");
String paramValue = temp.getString("kpiId");
sb.append(paramName + "=\"" + paramValue + "\"\n");
}
return sb.toString();
}
private String parseShellParams(String params) {
StringBuffer sb = new StringBuffer();
if (null == params || "".equals(params)) {
return sb.toString();
}
JSONArray jsonArray = (JSONArray) JSONUtil.fromJSON(params);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject temp = (JSONObject) jsonArray.get(i);
String paramName = temp.getString("citeName");
String paramValue = temp.getString("testingValue");
sb.append(paramName + "=\"" + paramValue + "\"\n");
}
return sb.toString();
}
private String parseShellMacros(String macros) {
StringBuffer sb = new StringBuffer();
if (null == macros || "".equals(macros)) {
return sb.toString();
}
JSONArray jsonArray = (JSONArray) JSONUtil.fromJSON(macros);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject temp = (JSONObject) jsonArray.get(i);
String paramName = temp.getString("macroVar");
String paramValue = AgentProperties.AGENT_HOME + "/"+ temp.getString("macroResult");
sb.append(paramName + "=\"" + paramValue + "\"\n");
}
return sb.toString();
}
private String parseShellContent(String shellContent) {
StringBuffer sb = new StringBuffer();
if (null == shellContent || "".equals(shellContent)) {
return sb.toString();
}
// 替换#output#及增加采集源、采集周期
String[] line = shellContent.split("\n");
for (String str : line) {
if (str.indexOf("#output#") >= 0) {
str = str + " " + props.getInterval() + " " + props.getExtInfo();
str = str.replace("#output#", SystemUtil.WRITE_SWAP);
}
sb.append(str + "\n");
}
return sb.toString();
}
/**
* 生成notice文件,通知crontab执行脚本
*/
private String getNoticeContent() {
String content = BusiConstants.NOTICE_CONTENT;
content = content.replaceAll("#AGENT_HOME#", AgentProperties.AGENT_HOME);
content = content.replaceAll("#AGENT_ID#", AgentProperties.AGENT_ID);
content = content.replaceAll("#REQUEST_ID#", props.getGuid());
content = content.replaceAll("#SCHEDULE_ID#", props.getSchId());
//1.脚本测试 2.调度任务同步 3.脚本下发 4.附件下发 5.附件验证
content = content.replaceAll("#OPERATE_TYPE#", OPR_TYPE_SHELL);
content = content.replaceAll("#TRRIGGER_TYPE#", "AUTO");
content = content.replaceAll("#SEQ#", SEQ_1_NOTICE);
content = content.replaceAll("#SHELL_NAME#", props.getShellName());
return content;
}
/**
* 上报执行日志
* @param logContent 日志内容
* @param reqId
* @param seq
* @param endTime
* @param extInfo
*/
private void sendScheduleLogs(String logContent, String reqId, String seq, String endTime, Map<String,Object> extInfo){
String oprType = "6";
String agentId = AgentProperties.AGENT_ID;
String schId = props.getSchId();
String nextFireTime = props.getNextFireTime();
ScheduleLog log = new ScheduleLog();
if( "0".equals(seq)){
log.setBeginTime(Formater.datetimeToString(new Date()));
}
if(null!=endTime && !"".equals(endTime.trim())){
log.setEndTime(endTime);
}
log.setOperateType(oprType);
log.setAgentId(agentId);
log.setNextFireTime(nextFireTime);
log.setType("std_out");
log.setSchId(schId);
log.setRequestId(reqId);
log.setSeq(seq);
log.setLogInfo(logContent);
log.setTrigerType("AUTO");
log.setExtInfo(extInfo == null ? new HashMap<String, Object>() : extInfo);
String logType = "0";
log.setLogType(logType);
logger.info("sendScheduleLogs: " + JSONUtil.toJSON(log));
TunnelFactory.getTunnel(MQConstants.Q_ROPORT_FROM_AGENT).writeData(log);
}
}