diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java index dc6aa27e25..df1cbcc28e 100644 --- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java @@ -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()); } } diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java index 638a500299..8e33b792d8 100644 --- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java @@ -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 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) { diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java index 813e91a729..41aabfe13d 100644 --- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java @@ -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()); + } } } diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtilsTest.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtilsTest.java index 1bf98d2019..1d847a0635 100644 --- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtilsTest.java +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtilsTest.java @@ -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() { diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java index 7f15ed8a7b..e022b9ebf7 100644 --- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSenderTest.java @@ -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()); } diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/example.sh b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/scriptExample.sh similarity index 80% rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/example.sh rename to dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/scriptExample.sh index 708dcd004b..aca9866df0 100755 --- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/example.sh +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/scriptExample.sh @@ -1,3 +1,4 @@ +#!/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 diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/scriptTest.sh b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/test.sh similarity index 87% rename from dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/scriptTest.sh rename to dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/test.sh index 02eba48a81..7c9d163a9e 100755 --- a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/scriptTest.sh +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/test.sh @@ -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