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);
80        status = HealthCheckerExitStatus.FAILED_WITH_EXIT_CODE;
81      } catch (IOException e) {
82        LOG.warn("Caught exception : " + e);
83        if (!shexec.isTimedOut()) {
84          status = HealthCheckerExitStatus.FAILED_WITH_EXCEPTION;
85          exceptionStackTrace = org.apache.hadoop.util.StringUtils.stringifyException(e);
86        } else {
87          status = HealthCheckerExitStatus.TIMED_OUT;
88        }
89      } finally {
90        if (status == HealthCheckerExitStatus.SUCCESS) {
91          if (hasErrors(shexec.getOutput())) {
92            status = HealthCheckerExitStatus.FAILED;
93          }
94        }
95      }
96      return new HealthReport(status, getHealthReport(status));
97    }
98  
99    private boolean hasErrors(String output) {
100     String[] splits = output.split("\n");
101     for (String split : splits) {
102       if (split.startsWith(ERROR_PATTERN)) {
103         return true;
104       }
105     }
106     return false;
107   }
108 
109   private String getHealthReport(HealthCheckerExitStatus status){
110     String healthReport = null;
111     switch (status) {
112     case SUCCESS:
113       healthReport = "Server is healthy.";
114       break;
115     case TIMED_OUT:
116       healthReport = "Health script timed out";
117       break;
118     case FAILED_WITH_EXCEPTION:
119       healthReport = exceptionStackTrace;
120       break;
121     case FAILED_WITH_EXIT_CODE:
122       healthReport = "Health script failed with exit code.";
123       break;
124     case FAILED:
125       healthReport = shexec.getOutput();
126       break;
127     }
128     return healthReport;
129   }
130 }