未验证 提交 0d1bbd8e 编写于 作者: G gaojun2048 提交者: GitHub

Merge pull request #4814 from CalvinKirs/script_thread

[Improvement][spi-alert]script plugin should contain alert content
......@@ -36,6 +36,6 @@ public class ScriptAlertChannel implements AlertChannel {
if (null == paramsMap) {
return new AlertResult("false", "ding talk params is null");
}
return new ScriptSender(paramsMap).sendScriptAlert(alertData.getTitle());
return new ScriptSender(paramsMap).sendScriptAlert(alertData.getTitle(),alertData.getContent());
}
}
......@@ -37,28 +37,34 @@ public class ScriptSender {
private String userParams;
private static final String ALERT_TITLE_OPTION = " -t ";
private static final String ALERT_CONTENT_OPTION = " -c ";
private static final String ALERT_USER_PARAMS_OPTION = " -p ";
ScriptSender(Map<String, String> config) {
scriptPath = config.get(ScriptParamsConstants.NAME_SCRIPT_PATH);
scriptType = config.get(ScriptParamsConstants.NAME_SCRIPT_TYPE);
userParams = config.get(ScriptParamsConstants.NAME_SCRIPT_USER_PARAMS);
}
AlertResult sendScriptAlert(String msg) {
AlertResult sendScriptAlert(String title, String content) {
AlertResult alertResult = new AlertResult();
if (ScriptType.SHELL.getDescp().equals(scriptType)) {
return executeShellScript(msg);
return executeShellScript(title, content);
}
return alertResult;
}
private AlertResult executeShellScript(String msg) {
private AlertResult executeShellScript(String title, String content) {
AlertResult alertResult = new AlertResult();
alertResult.setStatus("false");
if (Boolean.TRUE.equals(OSUtils.isWindows())) {
alertResult.setMessage("shell script not support windows os");
return alertResult;
}
String[] cmd = {"/bin/sh", "-c", scriptPath + " " + msg + " " + userParams};
String[] cmd = {"/bin/sh", "-c", scriptPath + ALERT_TITLE_OPTION + "'" + title + "'" + ALERT_CONTENT_OPTION + "'" + content + "'" + ALERT_USER_PARAMS_OPTION + "'" + userParams + "'"};
int exitCode = ProcessUtils.executeScript(cmd);
if (exitCode == 0) {
......
......@@ -55,6 +55,13 @@ public class StreamGobbler extends Thread {
}
} catch (IOException e) {
logger.error("I/O error occurs {}", e.getMessage());
} finally {
try {
inputBufferReader.close();
inputStreamReader.close();
} catch (IOException e) {
logger.error("I/O error occurs {}", e.getMessage());
}
}
}
......
......@@ -26,9 +26,9 @@ public class ProcessUtilsTest {
private static final String rootPath = System.getProperty("user.dir");
private static final String shellFilPath = rootPath + "/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/example.sh";
private static final String shellFilPath = rootPath + "/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/test.sh";
private String[] cmd = {"/bin/sh", "-c", shellFilPath + " " + "testMsg" + " " + "userParams"};
private String[] cmd = {"/bin/sh", "-c", shellFilPath + " -t 1"};
@Test
public void testExecuteScript() {
......
......@@ -35,7 +35,7 @@ public class ScriptSenderTest {
private static final String rootPath = System.getProperty("user.dir");
private static final String shellFilPath = rootPath + "/src/test/script/shell/scriptTest.sh";
private static final String shellFilPath = rootPath + "/src/test/script/shell/scriptExample.sh";
@Before
public void initScriptConfig() {
......@@ -49,9 +49,9 @@ public class ScriptSenderTest {
public void testScriptSenderTest() {
ScriptSender scriptSender = new ScriptSender(scriptConfig);
AlertResult alertResult;
alertResult = scriptSender.sendScriptAlert("success");
alertResult = scriptSender.sendScriptAlert("test title Kris", "test content");
Assert.assertEquals("true", alertResult.getStatus());
alertResult = scriptSender.sendScriptAlert("errorMsg");
alertResult = scriptSender.sendScriptAlert("error msg title", "test content");
Assert.assertEquals("false", alertResult.getStatus());
}
......
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
......@@ -15,11 +16,24 @@
# limitations under the License.
#
while getopts t:c:p: opts; do
case $opts in
t) t=$OPTARG ;;
c) c=$OPTARG ;;
p) p=$OPTARG ;;
?) ;;
esac
done
msg=$1
content=$2
# Write your specific logic here
# Set the exit code according to your execution result, and alert needs to use it to judge the status of this alarm result
if [ "$t" = "error msg title" ]
then
exit 12
fi
exit 0
exit 0
\ No newline at end of file
......@@ -15,11 +15,13 @@
# limitations under the License.
#
msg=$1
content=$2
while getopts t: opts; do
case $opts in
t) t=$OPTARG ;;
?) ;;
esac
done
if [ $msg = errorMsg ]
then
exit 12
fi
exit 0
\ No newline at end of file
echo "$t"
exit 0
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册