From f4ef86c50bc25cb39d48565025651b775433b934 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E8=93=A0?= Date: Fri, 13 Nov 2020 11:16:05 +0800 Subject: [PATCH] [FEATURE#4029] alert spi support script alert this closes #4029 --- .../dolphinscheduler-alert-script/pom.xml | 75 +++++++++++++++++++ .../plugin/alert/script/OSUtils.java | 25 +++++++ .../plugin/alert/script/ProcessUtils.java | 57 ++++++++++++++ .../alert/script/ScriptAlertChannel.java | 40 ++++++++++ .../script/ScriptAlertChannelFactory.java | 69 +++++++++++++++++ .../alert/script/ScriptAlertPlugin.java | 35 +++++++++ .../alert/script/ScriptParamsConstants.java | 36 +++++++++ .../plugin/alert/script/ScriptSender.java | 75 +++++++++++++++++++ .../plugin/alert/script/ScriptType.java | 63 ++++++++++++++++ .../plugin/alert/script/StreamGobbler.java | 62 +++++++++++++++ .../script/ScriptAlertChannelFactoryTest.java | 49 ++++++++++++ .../src/test/script/shell/example.sh | 28 +++++++ dolphinscheduler-alert-plugin/pom.xml | 1 + 13 files changed, 615 insertions(+) create mode 100644 dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/pom.xml create mode 100644 dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java create mode 100644 dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java create mode 100644 dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java create mode 100644 dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactory.java create mode 100644 dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertPlugin.java create mode 100644 dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptParamsConstants.java create mode 100644 dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java create mode 100644 dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptType.java create mode 100644 dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java create mode 100644 dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java create mode 100644 dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/example.sh diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/pom.xml b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/pom.xml new file mode 100644 index 0000000000..911e28d84d --- /dev/null +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/pom.xml @@ -0,0 +1,75 @@ + + + + + dolphinscheduler-alert-plugin + org.apache.dolphinscheduler + 1.3.2-SNAPSHOT + + 4.0.0 + + org.apache.dolphinscheduler + dolphinscheduler-alert-script + + + + + + org.apache.dolphinscheduler + dolphinscheduler-spi + provided + + + + com.google.guava + guava + + + + ch.qos.logback + logback-classic + + + + org.slf4j + slf4j-api + + + + com.fasterxml.jackson.core + jackson-annotations + provided + + + + junit + junit + test + + + + org.mockito + mockito-core + jar + test + + + + \ No newline at end of file diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java new file mode 100644 index 0000000000..88f6c3970b --- /dev/null +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/OSUtils.java @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.plugin.alert.script; + +public class OSUtils { + + public static Boolean isWindows() { + return System.getProperty("os.name").startsWith("Windows"); + } +} diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java new file mode 100644 index 0000000000..d3e8fae3f5 --- /dev/null +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ProcessUtils.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.plugin.alert.script; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +/** + * ProcessUtils + */ +public class ProcessUtils { + + + public static Integer executeScript(String... cmd) { + + int exitCode = -1; + ProcessBuilder processBuilder = new ProcessBuilder(cmd); + try { + Process process = processBuilder.start(); + + InputStream in = process.getErrorStream(); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in)); + StringBuilder result = new StringBuilder(); + + StreamGobbler inputStreamGobbler = + new StreamGobbler(process.getInputStream()); + StreamGobbler errorStreamGobbler = + new StreamGobbler(process.getErrorStream()); + + inputStreamGobbler.start(); + errorStreamGobbler.start(); + return process.waitFor(); + + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + + return exitCode; + } +} 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 new file mode 100644 index 0000000000..2a0021277d --- /dev/null +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannel.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.plugin.alert.script; + +import org.apache.dolphinscheduler.spi.alert.AlertChannel; +import org.apache.dolphinscheduler.spi.alert.AlertData; +import org.apache.dolphinscheduler.spi.alert.AlertInfo; +import org.apache.dolphinscheduler.spi.alert.AlertResult; +import org.apache.dolphinscheduler.spi.params.PluginParamsTransfer; + +import java.util.Map; + +/** + * ScriptAlertChannel + */ +public class ScriptAlertChannel implements AlertChannel { + + @Override + public AlertResult process(AlertInfo alertinfo) { + AlertData alertData = alertinfo.getAlertData(); + String alertParams = alertinfo.getAlertParams(); + Map paramsMap = PluginParamsTransfer.getPluginParamsMap(alertParams); + return new ScriptSender(paramsMap).sendScriptAlert(alertData.getTitle()); + } +} diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactory.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactory.java new file mode 100644 index 0000000000..fc338625c1 --- /dev/null +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactory.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.plugin.alert.script; + +import java.util.Arrays; +import java.util.List; + +import org.apache.dolphinscheduler.spi.alert.AlertChannel; +import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory; +import org.apache.dolphinscheduler.spi.params.InputParam; +import org.apache.dolphinscheduler.spi.params.RadioParam; +import org.apache.dolphinscheduler.spi.params.base.ParamsOptions; +import org.apache.dolphinscheduler.spi.params.base.PluginParams; +import org.apache.dolphinscheduler.spi.params.base.Validate; + +/** + * ScriptAlertChannelFactory + */ +public class ScriptAlertChannelFactory implements AlertChannelFactory { + + @Override + public String getName() { + return "script alert"; + } + + @Override + public List getParams() { + + InputParam scriptUserParam = InputParam.newBuilder(ScriptParamsConstants.NAME_SCRIPT_USER_PARAMS, ScriptParamsConstants.SCRIPT_USER_PARAMS) + .addValidate(Validate.newBuilder() + .setRequired(false) + .build()) + .build(); + // need check file type and file exist + InputParam scriptPathParam = InputParam.newBuilder(ScriptParamsConstants.NAME_SCRIPT_PATH, ScriptParamsConstants.SCRIPT_PATH) + .addValidate(Validate.newBuilder() + .setRequired(true) + .build()) + .build(); + + RadioParam scriptTypeParams = RadioParam.newBuilder(ScriptParamsConstants.NAME_SCRIPT_TYPE, ScriptParamsConstants.SCRIPT_TYPE) + .addParamsOptions(new ParamsOptions(ScriptType.SHELL.getDescp(), ScriptType.SHELL.getDescp(), false)) + .setValue(ScriptType.SHELL.getDescp()) + .addValidate(Validate.newBuilder().setRequired(true).build()) + .build(); + + return Arrays.asList(scriptUserParam, scriptPathParam, scriptTypeParams); + } + + @Override + public AlertChannel create() { + return new ScriptAlertChannel(); + } +} diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertPlugin.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertPlugin.java new file mode 100644 index 0000000000..f2ba0e87d0 --- /dev/null +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertPlugin.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.plugin.alert.script; + +import org.apache.dolphinscheduler.spi.DolphinSchedulerPlugin; +import org.apache.dolphinscheduler.spi.alert.AlertChannelFactory; + +import com.google.common.collect.ImmutableList; + +/** + * ScriptAlertPlugin + */ +public class ScriptAlertPlugin implements DolphinSchedulerPlugin { + + @Override + public Iterable getAlertChannelFactorys() { + return ImmutableList.of(new ScriptAlertChannelFactory()); + } + +} diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptParamsConstants.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptParamsConstants.java new file mode 100644 index 0000000000..67193c853d --- /dev/null +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptParamsConstants.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.plugin.alert.script; + +/** + * ScriptParamsConstants + */ +public class ScriptParamsConstants { + + static final String SCRIPT_TYPE = "script.type"; + + static final String NAME_SCRIPT_TYPE = "scriptType"; + + static final String SCRIPT_PATH = "script.path"; + + static final String NAME_SCRIPT_PATH = "scriptPath"; + + static final String SCRIPT_USER_PARAMS = "script.user.params"; + + static final String NAME_SCRIPT_USER_PARAMS = "scriptUserParams"; +} 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 new file mode 100644 index 0000000000..df99a4f1a9 --- /dev/null +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptSender.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.plugin.alert.script; + +import org.apache.dolphinscheduler.spi.alert.AlertResult; + +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * ScriptSender + */ +public class ScriptSender { + + private static final Logger logger = LoggerFactory.getLogger(ScriptSender.class); + + + private String scriptPath; + + private Integer scriptType; + + private String userParams; + + public ScriptSender(Map config) { + scriptPath = config.get(ScriptParamsConstants.NAME_SCRIPT_PATH); + scriptType = Integer.parseInt(config.get(ScriptParamsConstants.NAME_SCRIPT_TYPE)); + userParams = config.get(ScriptParamsConstants.NAME_SCRIPT_USER_PARAMS); + } + + + public AlertResult sendScriptAlert(String msg) { + AlertResult alertResult = new AlertResult(); + if (ScriptType.of(scriptType).equals(ScriptType.SHELL)) { + executeShellScript(msg); + } + return alertResult; + } + + private AlertResult executeShellScript(String msg) { + AlertResult alertResult = new AlertResult(); + alertResult.setStatus("false"); + if (OSUtils.isWindows()) { + alertResult.setMessage("shell script not support windows os"); + return alertResult; + } + String[] cmd = {"/bin/sh", "-c", scriptPath + " " + msg + " " + userParams}; + int exitCode = ProcessUtils.executeScript(cmd); + + if (exitCode == 0) { + alertResult.setStatus("true"); + alertResult.setMessage("send script alert msg success"); + return alertResult; + } + alertResult.setMessage("send script alert msg error,exitCode is " + exitCode); + return alertResult; + } + +} diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptType.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptType.java new file mode 100644 index 0000000000..a7544cc46a --- /dev/null +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptType.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.plugin.alert.script; + +import java.util.HashMap; +import java.util.Map; + +/** + * ScriptType + */ +public enum ScriptType { + + + SHELL(0, "SHELL"), + ; + + ScriptType(int code, String descp) { + this.code = code; + this.descp = descp; + } + + private final int code; + private final String descp; + + public int getCode() { + return code; + } + + public String getDescp() { + return descp; + } + + private static final Map SCRIPT_TYPE_MAP = new HashMap<>(); + + static { + for (ScriptType scriptType : ScriptType.values()) { + SCRIPT_TYPE_MAP.put(scriptType.code, scriptType); + } + } + + + public static ScriptType of(Integer code) { + if (SCRIPT_TYPE_MAP.containsKey(code)) { + return SCRIPT_TYPE_MAP.get(code); + } + throw new IllegalArgumentException("invalid code : " + code); + } +} 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 new file mode 100644 index 0000000000..13f8790cbc --- /dev/null +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/main/java/org/apache/dolphinscheduler/plugin/alert/script/StreamGobbler.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.plugin.alert.script; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * StreamGobbler + */ +public class StreamGobbler extends Thread { + + private static final Logger logger = LoggerFactory.getLogger(StreamGobbler.class); + + InputStream inputStream; + + + StreamGobbler(InputStream inputStream) { + this.inputStream = inputStream; + } + + + public void run() { + InputStreamReader inputStreamReader = new InputStreamReader(inputStream); + BufferedReader inputBufferReader = new BufferedReader(inputStreamReader); + + try { + String line; + StringBuilder output = new StringBuilder(); + while ((line = inputBufferReader.readLine()) != null) { + output.append(line); + output.append(System.getProperty("line.separator")); + } + if (output.length() > 0) { + logger.info(output.toString()); + } + } catch (IOException e) { + logger.error("I/O error occurs %S", e.getMessage()); + } + } + +} \ No newline at end of file diff --git a/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java new file mode 100644 index 0000000000..53127f7cf5 --- /dev/null +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/java/org/apache/dolphinscheduler/plugin/alert/script/ScriptAlertChannelFactoryTest.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.dolphinscheduler.plugin.alert.script; + +import org.apache.dolphinscheduler.spi.alert.AlertChannel; +import org.apache.dolphinscheduler.spi.params.base.PluginParams; +import org.apache.dolphinscheduler.spi.utils.JSONUtils; + +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +/** + * ScriptAlertChannelFactoryTest + */ +public class ScriptAlertChannelFactoryTest { + + + @Test + public void testGetParams() { + ScriptAlertChannelFactory scriptAlertChannelFactory = new ScriptAlertChannelFactory(); + List params = scriptAlertChannelFactory.getParams(); + JSONUtils.toJsonString(params); + Assert.assertEquals(3, params.size()); + } + + @Test + public void testCreate() { + ScriptAlertChannelFactory scriptAlertChannelFactory = new ScriptAlertChannelFactory(); + AlertChannel alertChannel = scriptAlertChannelFactory.create(); + Assert.assertNotNull(alertChannel); + } +} 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/example.sh new file mode 100644 index 0000000000..6f215b011a --- /dev/null +++ b/dolphinscheduler-alert-plugin/dolphinscheduler-alert-script/src/test/script/shell/example.sh @@ -0,0 +1,28 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +content=$1 + +msg=$2 + +# Write your specific logic here +echo $content + +echo $msg + +# Set the exit code according to your execution result, and alert needs to use it to judge the status of this alarm result +exit 888 diff --git a/dolphinscheduler-alert-plugin/pom.xml b/dolphinscheduler-alert-plugin/pom.xml index d2fbca305d..0f25f1f19b 100644 --- a/dolphinscheduler-alert-plugin/pom.xml +++ b/dolphinscheduler-alert-plugin/pom.xml @@ -33,6 +33,7 @@ dolphinscheduler-alert-email dolphinscheduler-alert-wechat dolphinscheduler-alert-dingtalk + dolphinscheduler-alert-script