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);
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 }