1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
32
33
34
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
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
58
59
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
76 shexec.execute();
77 } catch (ExitCodeException e) {
78
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 }