1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import java.lang.management.ManagementFactory;
22 import java.lang.management.RuntimeMXBean;
23 import java.util.Arrays;
24 import java.util.HashSet;
25 import java.util.Map.Entry;
26 import java.util.Set;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.hbase.classification.InterfaceAudience;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.conf.Configured;
33 import org.apache.hadoop.hbase.HBaseConfiguration;
34 import org.apache.hadoop.util.Tool;
35 import org.apache.hadoop.util.ToolRunner;
36
37
38
39
40 @InterfaceAudience.Private
41 public abstract class ServerCommandLine extends Configured implements Tool {
42 private static final Log LOG = LogFactory.getLog(ServerCommandLine.class);
43 @SuppressWarnings("serial")
44 private static final Set<String> DEFAULT_SKIP_WORDS = new HashSet<String>() {
45 {
46 add("secret");
47 add("passwd");
48 add("password");
49 add("credential");
50 }
51 };
52
53
54
55
56 protected abstract String getUsage();
57
58
59
60
61
62
63 protected void usage(String message) {
64 if (message != null) {
65 System.err.println(message);
66 System.err.println("");
67 }
68
69 System.err.println(getUsage());
70 }
71
72
73
74
75 public static void logJVMInfo() {
76
77 RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
78 if (runtime != null) {
79 LOG.info("vmName=" + runtime.getVmName() + ", vmVendor=" +
80 runtime.getVmVendor() + ", vmVersion=" + runtime.getVmVersion());
81 LOG.info("vmInputArguments=" + runtime.getInputArguments());
82 }
83 }
84
85
86
87
88
89
90
91
92
93 public static void logProcessInfo(Configuration conf) {
94
95 if (conf == null || !conf.getBoolean("hbase.envvars.logging.disabled", false)) {
96 Set<String> skipWords = new HashSet<String>(DEFAULT_SKIP_WORDS);
97 if (conf != null) {
98 String[] confSkipWords = conf.getStrings("hbase.envvars.logging.skipwords");
99 if (confSkipWords != null) {
100 skipWords.addAll(Arrays.asList(confSkipWords));
101 }
102 }
103
104 nextEnv:
105 for (Entry<String, String> entry : System.getenv().entrySet()) {
106 String key = entry.getKey().toLowerCase();
107 String value = entry.getValue().toLowerCase();
108
109 for(String skipWord : skipWords) {
110 if (key.contains(skipWord) || value.contains(skipWord))
111 continue nextEnv;
112 }
113 LOG.info("env:"+entry);
114 }
115 }
116
117 logJVMInfo();
118 }
119
120
121
122
123
124 public void doMain(String args[]) {
125 try {
126 int ret = ToolRunner.run(HBaseConfiguration.create(), this, args);
127 if (ret != 0) {
128 System.exit(ret);
129 }
130 } catch (Exception e) {
131 LOG.error("Failed to run", e);
132 System.exit(-1);
133 }
134 }
135 }