View Javadoc

1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.hadoop.util.Shell.ExitCodeException;
28  import org.apache.hadoop.util.Shell.ShellCommandExecutor;
29  
30  /**
31   * A utility for executing an external script that checks the health of
32   * the node. An example script can be found at
33   * <tt>src/main/sh/healthcheck/healthcheck.sh</tt> in the
34   * <tt>hbase-examples</tt> module.
35   */
36  class HealthChecker {
37  
38    private static Log LOG = LogFactory.getLog(HealthChecker.class);
39    private ShellCommandExecutor shexec = null;
40    private String exceptionStackTrace;
41  
42    /** Pattern used for searching in the output of the node health script */
43    static private final String ERROR_PATTERN = "ERROR";
44  
45    private String healthCheckScript;
46    private long scriptTimeout;
47  
48    enum HealthCheckerExitStatus {
49      SUCCESS,
50      TIMED_OUT,
51      FAILED_WITH_EXIT_CODE,
52      FAILED_WITH_EXCEPTION,
53      FAILED
54    }
55  
56    /**
57     * Initialize.
58     *
59     * @param configuration
60     */
61    public void init(String location, long timeout) {
62      this.healthCheckScript = location;
63      this.scriptTimeout = timeout;
64      ArrayList<String> execScript = new ArrayList<String>();
65      execScript.add(healthCheckScript);
66      this.shexec = new ShellCommandExecutor(execScript.toArray(new String[execScript.size()]), null,
67          null, scriptTimeout);
68      LOG.info("HealthChecker initialized with script at " + this.healthCheckScript +
69        ", timeout=" + timeout);
70    }
71  
72    public HealthReport checkHealth() {
73      HealthCheckerExitStatus status = HealthCheckerExitStatus.SUCCESS;
74      try {
75        // Calling this execute leaves around running executor threads.
76        shexec.execute();
77      } catch (ExitCodeException e) {
78        // ignore the exit code of the script
79        LOG.warn("Caught exception : " + e + ",exit code:" + e.getExitCode());
80        status = HealthCheckerExitStatus.FAILED_WITH_EXIT_CODE;
81      } catch (IOException e) {
82        LOG.warn("Caught exception : " + e);
83        status = HealthCheckerExitStatus.FAILED_WITH_EXCEPTION;
84        exceptionStackTrace = org.apache.hadoop.util.StringUtils.stringifyException(e);
85      } finally {
86        if (shexec.isTimedOut()) {
87          status = HealthCheckerExitStatus.TIMED_OUT;
88        }
89        if (status == HealthCheckerExitStatus.SUCCESS) {
90          if (hasErrors(shexec.getOutput())) {
91            status = HealthCheckerExitStatus.FAILED;
92          }
93        }
94      }
95      return new HealthReport(status, getHealthReport(status));
96    }
97  
98    private boolean hasErrors(String output) {
99      String[] splits = output.split("\n");
100     for (String split : splits) {
101       if (split.startsWith(ERROR_PATTERN)) {
102         return true;
103       }
104     }
105     return false;
106   }
107 
108   private String getHealthReport(HealthCheckerExitStatus status){
109     String healthReport = null;
110     switch (status) {
111     case SUCCESS:
112       healthReport = "Server is healthy.";
113       break;
114     case TIMED_OUT:
115       healthReport = "Health script timed out";
116       break;
117     case FAILED_WITH_EXCEPTION:
118       healthReport = exceptionStackTrace;
119       break;
120     case FAILED_WITH_EXIT_CODE:
121       healthReport = "Health script failed with exit code.";
122       break;
123     case FAILED:
124       healthReport = shexec.getOutput();
125       break;
126     }
127     return healthReport;
128   }
129 }