summaryrefslogtreecommitdiff
path: root/ambari-server/src/main/java/org/apache/ambari/server/notifications/dispatchers/AlertScriptDispatcher.java
blob: 092aaf4ba4ec65f76418ed0c381a30e1ae2f22fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
 * 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.ambari.server.notifications.dispatchers;

import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.ambari.server.configuration.Configuration;
import org.apache.ambari.server.notifications.Notification;
import org.apache.ambari.server.notifications.NotificationDispatcher;
import org.apache.ambari.server.notifications.TargetConfigurationResult;
import org.apache.ambari.server.orm.entities.AlertDefinitionEntity;
import org.apache.ambari.server.state.AlertState;
import org.apache.ambari.server.state.alert.AlertNotification;
import org.apache.ambari.server.state.alert.TargetType;
import org.apache.ambari.server.state.services.AlertNoticeDispatchService.AlertInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Inject;

/**
 * The {@link AlertScriptDispatcher} is used to dispatch
 * {@link AlertNotification}s to a script via a {@link Process} and command line
 * arguments. This dispatcher does not know how to work with any other types of
 * {@link Notification}.
 * <p/>
 * This dispatcher only deals with non-digest notifications.
 */
public class AlertScriptDispatcher implements NotificationDispatcher {

  /**
   * The key in {@code ambari.properties} which contains the script to execute.
   */
  public static final String SCRIPT_CONFIG_DEFAULT_KEY = "notification.dispatch.alert.script";

  /**
   * The key in {@code ambari.properties} which contains the timeout, in
   * milliseconds, of any script run by this dispatcher.
   */
  public static final String SCRIPT_CONFIG_TIMEOUT_KEY = "notification.dispatch.alert.script.timeout";

  /**
   * A dispatch property that instructs this dispatcher to read a different key
   * from {@link Configuration}. If this value is not a part of the
   * {@link Notification#DispatchProperties} then
   * {@link #SCRIPT_CONFIG_DEFAULT_KEY} will be used.
   */
  public static final String DISPATCH_PROPERTY_SCRIPT_CONFIG_KEY = "ambari.dispatch-property.script";

  /**
   * Logger.
   */
  private static final Logger LOG = LoggerFactory.getLogger(AlertScriptDispatcher.class);

  /**
   * Default script timeout is 5s
   */
  private static final long DEFAULT_SCRIPT_TIMEOUT = 5000L;

  /**
   * Configuration data from the ambari.properties file.
   */
  @Inject
  protected Configuration m_configuration;

  /**
   * The executor responsible for dispatching.
   */
  private final Executor m_executor = new ThreadPoolExecutor(0, 1, 5L, TimeUnit.MINUTES,
      new LinkedBlockingQueue<Runnable>(), new ScriptDispatchThreadFactory(),
      new ThreadPoolExecutor.CallerRunsPolicy());

  /**
   * Gets the key that will be used to lookup the script to execute from
   * {@link Configuration}.
   *
   * @return the key that will be used to lookup the script value from
   *         {@link Configuration} (never {@code null}).
   */
  public String getScriptConfigurationKey( Notification notification ) {
    if( null == notification || null == notification.DispatchProperties ){
      return SCRIPT_CONFIG_DEFAULT_KEY;
    }

    if (null == notification.DispatchProperties.get(DISPATCH_PROPERTY_SCRIPT_CONFIG_KEY)) {
      return SCRIPT_CONFIG_DEFAULT_KEY;
    }

    return notification.DispatchProperties.get(DISPATCH_PROPERTY_SCRIPT_CONFIG_KEY);
  }

  /**
   * Gets the timeout value for the script, in milliseconds, as set in
   * {@link Configuration}. If there is no setting, then
   * {@link #DEFAULT_SCRIPT_TIMEOUT} will be returned.
   *
   * @return the timeout value in milliseconds
   */
  public long getScriptConfigurationTimeout(){
    String scriptTimeout = m_configuration.getProperty(SCRIPT_CONFIG_TIMEOUT_KEY);
    if (null == scriptTimeout) {
      return DEFAULT_SCRIPT_TIMEOUT;
    }

    return Long.parseLong(scriptTimeout);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public final String getType() {
    return TargetType.ALERT_SCRIPT.name();
  }

  /**
   * {@inheritDoc}
   * <p/>
   * Returns {@code false} always.
   */
  @Override
  public final boolean isNotificationContentGenerationRequired() {
    return false;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void dispatch(Notification notification) {
    String scriptKey = getScriptConfigurationKey(notification);
    String script = m_configuration.getProperty(scriptKey);

    // this dispatcher requires a script to run
    if (null == script) {
      LOG.warn(
          "Unable to dispatch notification because the {} configuration property was not found",
          scriptKey);

      if (null != notification.Callback) {
        notification.Callback.onFailure(notification.CallbackIds);
      }

      return;
    }

    // this dispatcher can only handler alert notifications
    if (notification.getType() != Notification.Type.ALERT) {
      LOG.warn("The {} dispatcher is not able to dispatch notifications of type {}", getType(),
          notification.getType());

      if (null != notification.Callback) {
        notification.Callback.onFailure(notification.CallbackIds);
      }

      return;
    }

    // execute the script asynchronously
    long timeout = getScriptConfigurationTimeout();
    TimeUnit timeUnit = TimeUnit.MILLISECONDS;
    AlertNotification alertNotification = (AlertNotification) notification;
    ProcessBuilder processBuilder = getProcessBuilder(script, alertNotification);

    AlertScriptRunnable runnable = new AlertScriptRunnable(alertNotification, script,
        processBuilder,
        timeout, timeUnit);

    m_executor.execute(runnable);
  }

  /**
   * {@inheritDoc}
   * <p/>
   * Returns {@code false} always.
   */
  @Override
  public final boolean isDigestSupported() {
    return false;
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public final TargetConfigurationResult validateTargetConfig(Map<String, Object> properties) {

    // there's no setup required for this dispatcher; always return valid
    return TargetConfigurationResult.valid();
  }

  /**
   * Gets a {@link ProcessBuilder} initialized with a script command to run with
   * the parameters from the notification.
   *
   * @param script
   *          the absolute path to the script (not {@code null}).
   * @param notification
   *          the notification to parameterie (not {@code null}).
   * @return
   */
  ProcessBuilder getProcessBuilder(String script, AlertNotification notification) {
    final String shellCommand;
    final String shellCommandOption;
    if (SystemUtils.IS_OS_WINDOWS) {
      shellCommand = "cmd";
      shellCommandOption = "/c";
    } else {
      shellCommand = "sh";
      shellCommandOption = "-c";
    }

    AlertInfo alertInfo = notification.getAlertInfo();
    AlertDefinitionEntity definition = alertInfo.getAlertDefinition();
    String definitionName = definition.getDefinitionName();
    AlertState alertState = alertInfo.getAlertState();
    String serviceName = alertInfo.getServiceName();

    // these could have spaces in them, so quote them so they don't mess up the
    // command line
    String alertLabel = "\"" + definition.getLabel() + "\"";
    String alertText = "\"" + alertInfo.getAlertText() + "\"";

    Object[] params = new Object[] { script, definitionName, alertLabel, serviceName,
        alertState.name(), alertText };

    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"'
    return new ProcessBuilder(shellCommand, shellCommandOption, foo);
  }

  /**
   * The {@link AlertScriptRunnable} is used to invoke a script with alert data
   * inside of an {@link Executor}.
   */
  private final static class AlertScriptRunnable implements Runnable {
    /**
     * Logger.
     */
    private static final Logger LOG = LoggerFactory.getLogger(AlertScriptRunnable.class);

    private final ProcessBuilder m_processBuilder;
    private final long m_timeout;
    private final TimeUnit m_timeoutUnits;
    private final Notification m_notification;
    private final String m_script;

    /**
     * Constructor.
     *
     * @param notification
     * @param script
     * @param processBuilder
     * @param timeout
     * @param timeoutUnits
     */
    private AlertScriptRunnable(Notification notification, String script,
        ProcessBuilder processBuilder,
        long timeout, TimeUnit timeoutUnits) {
      m_notification = notification;
      m_script = script;
      m_processBuilder = processBuilder;
      m_timeout = timeout;
      m_timeoutUnits = timeoutUnits;
    }

    /**
     *
     */
    @Override
    public void run() {
      boolean isDispatchSuccessful = true;

      try {
        Process process = m_processBuilder.start();
        int exitCode = execute(process, m_timeout, TimeUnit.MILLISECONDS);

        if (exitCode != 0) {
          LOG.warn("Unable to dispatch {} notification because {} terminated with exit code {}",
              TargetType.ALERT_SCRIPT, m_script, exitCode);

          isDispatchSuccessful = false;
        }
      } catch (TimeoutException timeoutException) {
        isDispatchSuccessful = false;

        LOG.warn("Unable to dispatch notification with {} in under {}ms", m_script,
            m_timeoutUnits.toMillis(m_timeout), timeoutException);
      } catch (Exception exception) {
        isDispatchSuccessful = false;

        LOG.warn("Unable to dispatch notification with {}", m_script, exception);
      }

      // callback
      if (null != m_notification.Callback) {
        if (isDispatchSuccessful) {
          m_notification.Callback.onSuccess(m_notification.CallbackIds);
        } else {
          m_notification.Callback.onFailure(m_notification.CallbackIds);
        }
      }
    }

    /**
     * Executes the specified process within the supplied timeout value. If the
     * process terminates normally within the timeout period, then the exit code
     * is returned. If the process did not succeed within the specified timeout
     * value, then a {@link TimeoutException} is thrown.
     * <p/>
     * If the process did not finish within the specified timeout, then the
     * process will be destroyed via {@link Process#destroy()}.
     *
     * @param process
     *          the process to execute (not {@code null}).
     * @param timeout
     *          the timeout to apply to the process.
     * @param unit
     *          the time units for the timeout
     * @return the exit code of the process
     * @throws TimeoutException
     *           if the process did not finish within the specified time.
     */
    public int execute(Process process, long timeout, TimeUnit unit) throws TimeoutException,
        InterruptedException {
      long timeRemaining = unit.toMillis(timeout);
      long startTime = System.currentTimeMillis();

      while (timeRemaining > 0) {
        try {
          return process.exitValue();
        } catch (IllegalThreadStateException ex) {
          if (timeRemaining > 0) {
            Thread.sleep(Math.min(timeRemaining, 500));
          }
        }

        long timeElapsed = System.currentTimeMillis() - startTime;
        timeRemaining = unit.toMillis(timeout) - timeElapsed;
      }

      process.destroy();

      throw new TimeoutException();
    }
  }

  /**
   * A custom {@link ThreadFactory} for the threads that will handle dispatching
   * to scripts. Threads created will have slightly reduced priority since
   * {@link AlertNotification} instances are not critical to the system.
   */
  private static final class ScriptDispatchThreadFactory implements ThreadFactory {

    private static final AtomicInteger s_threadIdPool = new AtomicInteger(1);

    /**
     * {@inheritDoc}
     */
    @Override
    public Thread newThread(Runnable r) {
      Thread thread = new Thread(r, "script-dispatcher-" + s_threadIdPool.getAndIncrement());

      thread.setDaemon(false);
      thread.setPriority(Thread.NORM_PRIORITY - 1);

      return thread;
    }
  }
}