Repository: ambari Updated Branches: refs/heads/trunk f7e7c8bb3 -> 41906e593
AMBARI-20291 - Script-Based Alert Dispatchers support passing more parameters to script (Yao Lei via jonathanhurley) Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/41906e59 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/41906e59 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/41906e59 Branch: refs/heads/trunk Commit: 41906e593cca9ea5a9fa6d413d2ca1f5a5dcbd32 Parents: f7e7c8b Author: Jonathan Hurley <jhur...@hortonworks.com> Authored: Mon Mar 6 11:13:34 2017 -0500 Committer: Jonathan Hurley <jhur...@hortonworks.com> Committed: Mon Mar 6 11:58:30 2017 -0500 ---------------------------------------------------------------------- .../dispatchers/AlertScriptDispatcher.java | 7 +++-- .../services/AlertNoticeDispatchService.java | 8 ++++++ .../dispatchers/AlertScriptDispatcherTest.java | 28 +++++++++++++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/41906e59/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcher.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcher.java b/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcher.java index 907588d..d65a11d 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcher.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcher.java @@ -259,13 +259,16 @@ public class AlertScriptDispatcher implements NotificationDispatcher { String alertLabel = "\"" + SHELL_ESCAPE.escape(definition.getLabel()) + "\""; String alertText = "\"" + SHELL_ESCAPE.escape(alertInfo.getAlertText()) + "\""; + long alertTimestamp = alertInfo.getAlertTimestamp(); + String hostName = alertInfo.getHostName(); // null if alert do not run against host + Object[] params = new Object[] { script, definitionName, alertLabel, serviceName, - alertState.name(), alertText }; + alertState.name(), alertText, alertTimestamp, hostName}; String foo = StringUtils.join(params, " "); // sh -c '/foo/sys_logger.py ambari_server_agent_heartbeat "Agent Heartbeat" - // AMBARI CRITICAL "Something went wrong with the host"' + // AMBARI CRITICAL "Something went wrong with the host" 1111111 host222' return new ProcessBuilder(shellCommand, shellCommandOption, foo); } http://git-wip-us.apache.org/repos/asf/ambari/blob/41906e59/ambari-server/src/main/java/org/apache/ambari/server/state/services/AlertNoticeDispatchService.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/main/java/org/apache/ambari/server/state/services/AlertNoticeDispatchService.java b/ambari-server/src/main/java/org/apache/ambari/server/state/services/AlertNoticeDispatchService.java index 174f31f..47f3989 100644 --- a/ambari-server/src/main/java/org/apache/ambari/server/state/services/AlertNoticeDispatchService.java +++ b/ambari-server/src/main/java/org/apache/ambari/server/state/services/AlertNoticeDispatchService.java @@ -768,6 +768,14 @@ public class AlertNoticeDispatchService extends AbstractScheduledService { } /** + * Gets the time that the alert was received + * @return + */ + public long getAlertTimestamp() { + return m_history.getAlertTimestamp(); + } + + /** * Gets the state of the alert. * * @return http://git-wip-us.apache.org/repos/asf/ambari/blob/41906e59/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcherTest.java ---------------------------------------------------------------------- diff --git a/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcherTest.java b/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcherTest.java index 9e0e406..78f8ba0 100644 --- a/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcherTest.java +++ b/ambari-server/src/test/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcherTest.java @@ -237,6 +237,8 @@ public class AlertScriptDispatcherTest { final String ALERT_SERVICE_NAME = "FOO_SERVICE"; final String ALERT_TEXT = "Did you know, \"Quotes are hard!!!\""; final String ALERT_TEXT_ESCAPED = "Did you know, \\\"Quotes are hard\\!\\!\\!\\\""; + final String ALERT_HOST = "mock_host"; + final long ALERT_TIMESTAMP = 1111111l; DispatchCallback callback = EasyMock.createNiceMock(DispatchCallback.class); AlertNotification notification = new AlertNotification(); @@ -253,6 +255,9 @@ public class AlertScriptDispatcherTest { history.setAlertText(ALERT_TEXT); history.setAlertState(AlertState.OK); history.setServiceName(ALERT_SERVICE_NAME); + history.setHostName(ALERT_HOST); + history.setAlertTimestamp(ALERT_TIMESTAMP); + AlertInfo alertInfo = new AlertInfo(history); notification.setAlertInfo(alertInfo); @@ -272,9 +277,30 @@ public class AlertScriptDispatcherTest { buffer.append("\"").append(ALERT_DEFINITION_LABEL).append("\"").append(" "); buffer.append(ALERT_SERVICE_NAME).append(" "); buffer.append(AlertState.OK).append(" "); - buffer.append("\"").append(ALERT_TEXT_ESCAPED).append("\""); + buffer.append("\"").append(ALERT_TEXT_ESCAPED).append("\"").append(" "); + buffer.append(ALERT_TIMESTAMP).append(" "); + buffer.append(ALERT_HOST); Assert.assertEquals(buffer.toString(), commands.get(2)); + + //if hostname is null + history.setHostName(null); + alertInfo = new AlertInfo(history); + notification.setAlertInfo(alertInfo); + + processBuilder = dispatcher.getProcessBuilder(SCRIPT_CONFIG_VALUE, notification); + commands = processBuilder.command(); + buffer = new StringBuilder(); + buffer.append(SCRIPT_CONFIG_VALUE).append(" "); + buffer.append(ALERT_DEFINITION_NAME).append(" "); + buffer.append("\"").append(ALERT_DEFINITION_LABEL).append("\"").append(" "); + buffer.append(ALERT_SERVICE_NAME).append(" "); + buffer.append(AlertState.OK).append(" "); + buffer.append("\"").append(ALERT_TEXT_ESCAPED).append("\"").append(" "); + buffer.append(ALERT_TIMESTAMP).append(" "); + buffer.append(""); + Assert.assertEquals(buffer.toString(), commands.get(2)); + } /**