package com.hansight.atom.custom.abilityPower.util;
import org.apache.commons.lang.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
public class JaveShellUtil {
private static final Logger logger = LoggerFactory.getLogger(JaveShellUtil.class);
public static int ExecCommand(String command) {
int retCode = 0;
try {
ProcessBuilder builder1 = new ProcessBuilder("/bin/chmod", "755", AbilityPowerConfigReader.shellPath);
Process process1 = builder1.start();
process1.waitFor();
Process process = Runtime.getRuntime().exec(new String[] { "/bin/sh", "-c", command+" 1234,5678"+" taskid"}, null, null);
retCode = process.waitFor();
ExecOutput(process);
logger.info("能力上台调用告警补全shell脚本成功》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》");
} catch (Exception e) {
retCode = -1;
e.printStackTrace();
logger.error("能力上台调用告警补全shell脚本报错:::",e);
}
return retCode;
}
public static boolean ExecOutput(Process process) throws Exception {
if (process == null) {
return false;
} else {
InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
String line;
String output = "";
while ((line = input.readLine()) != null) {
output += line + "\n";
}
input.close();
ir.close();
if (output.length() > 0) {
System.out.println(output);
}
}
return true;
}
}
shell脚本
#!/usr/bin/env bash
: '
Date: 2019-06-09 14:11:00
Description: 补全告警
'
indexs=$1
taskId=$2
source "$(
cd $(dirname $0)
pwd
)/env.sh"
PYTHON_COMMAND="${CONDA_ENV_HOME}/bin/python"
(
cd ${PROGRAM_ENTRY_PATH}
echo ${indexs}
echo ${taskId}
if [ `whoami` == "root" ]; then
su soar -c "${PYTHON_COMMAND} main_domain_ability.py $indexs $taskId>> ${LOG_HOME}/alarm_completion_industry_ability.log"
elif [ `whoami` == "soar" ]; then
${PYTHON_COMMAND} main_domain_ability.py $indexs $taskId>> ${LOG_HOME}/alarm_completion_industry_ability.log
else
echo `whoami` + "用户没有执行当前脚本权限,请切换至root或soar用户执行!"
fi
)
python脚本
#!/usr/bin/env python3
# -*- encoding:utf-8 -*-
"""
@Filename: main.py
@Time: 5/11/19 12:12 AM
@Description: 补全告警
"""
import time
import sys
indexs = sys.argv[1]
taskId = sys.argv[2]
from zyxa.service.tip_completion_alarm_ability import TipCompletionAlarmByAbility
from zyxa.utils.logger import Logger
log = Logger("main_domain_ability").get_logger()
if __name__ == "__main__":
try:
log.info("shell传过来的参数》》》》》》》》》》》》》》》》》》》》》》::"+indexs+"::::::::::::::::::::taskId::::::::::::::::::::::"+taskId)
t1 = str(round(time.time() * 1000))
TipCompletionAlarmByAbility().query_alarm(indexs,taskId)
t2 = str(round(time.time() * 1000))
except Exception as e:
log.error("能力上台补全告警异常,异常信息如下:", e)