package com.wgcloud.util;
|
|
import cn.hutool.core.text.UnicodeUtil;
|
import com.wgcloud.common.ApplicationContextHelper;
|
import com.wgcloud.config.MailConfig;
|
import com.wgcloud.util.staticvar.StaticKeys;
|
import org.apache.commons.lang3.StringUtils;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
public class ExecUtil {
|
|
private static final Logger logger = LoggerFactory.getLogger(ExecUtil.class);
|
|
private static MailConfig mailConfig = (MailConfig) ApplicationContextHelper.getBean(MailConfig.class);
|
|
/**
|
* 执行告警脚本
|
*
|
* @param path 脚本路径
|
* @param content 告警内容
|
* @param accountKey 用户标识,一般是第三方用户ID,可以是空
|
*/
|
public static void runScript(String path, String content, String accountKey) {
|
if (StringUtils.isEmpty(path)) {
|
return;
|
}
|
try {
|
//告警脚本发送 去掉html标签
|
content = content.replace("<font color='red'>", "");
|
content = content.replace("</font>", "");
|
content = content.replace("</br>", "");
|
|
if (StaticKeys.TRUE_VAL.equals(mailConfig.getWarnToUnicode())) {
|
content = UnicodeUtil.toUnicode(content, false);
|
}
|
String execCmdStr = path + " \"" + content + "\"";
|
if (!StringUtils.isEmpty(accountKey)) {
|
execCmdStr = execCmdStr + " \"" + accountKey + "\"";
|
}
|
Process process = Runtime.getRuntime().exec(execCmdStr);
|
// BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream(), Charset.forName("GBK")));
|
// String line = null;
|
// while ((line = br.readLine()) != null) {
|
// System.out.println(line);
|
// }
|
} catch (Exception e) {
|
logger.error("执行告警脚本错误:", e);
|
}
|
}
|
|
|
}
|