View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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.Locale;
26  import java.util.Map.Entry;
27  import java.util.Set;
28
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.hbase.classification.InterfaceAudience;
32  import org.apache.hadoop.conf.Configuration;
33  import org.apache.hadoop.conf.Configured;
34  import org.apache.hadoop.hbase.HBaseConfiguration;
35  import org.apache.hadoop.util.Tool;
36  import org.apache.hadoop.util.ToolRunner;
37
38  /**
39   * Base class for command lines that start up various HBase daemons.
40   */
41  @InterfaceAudience.Private
42  public abstract class ServerCommandLine extends Configured implements Tool {
43    private static final Log LOG = LogFactory.getLog(ServerCommandLine.class);
44    @SuppressWarnings("serial")
45    private static final Set<String> DEFAULT_SKIP_WORDS = new HashSet<String>() {
46      {
47        add("secret");
48        add("passwd");
49        add("password");
50        add("credential");
51      }
52    };
53
54    /**
55     * Implementing subclasses should return a usage string to print out.
56     */
57    protected abstract String getUsage();
58
59    /**
60     * Print usage information for this command line.
61     *
62     * @param message if not null, print this message before the usage info.
63     */
64    protected void usage(String message) {
65      if (message != null) {
66        System.err.println(message);
67        System.err.println("");
68      }
69
70      System.err.println(getUsage());
71    }
72
73    /**
74     * Log information about the currently running JVM.
75     */
76    public static void logJVMInfo() {
77      // Print out vm stats before starting up.
78      RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
79      if (runtime != null) {
80        LOG.info("vmName=" + runtime.getVmName() + ", vmVendor=" +
81                 runtime.getVmVendor() + ", vmVersion=" + runtime.getVmVersion());
82        LOG.info("vmInputArguments=" + runtime.getInputArguments());
83      }
84    }
85
86    /**
87     * Logs information about the currently running JVM process including
88     * the environment variables. Logging of env vars can be disabled by
89     * setting {@code "hbase.envvars.logging.disabled"} to {@code "true"}.
90     * <p>If enabled, you can also exclude environment variables containing
91     * certain substrings by setting {@code "hbase.envvars.logging.skipwords"}
92     * to comma separated list of such substrings.
93     */
94    public static void logProcessInfo(Configuration conf) {
95      // log environment variables unless asked not to
96      if (conf == null || !conf.getBoolean("hbase.envvars.logging.disabled", false)) {
97        Set<String> skipWords = new HashSet<String>(DEFAULT_SKIP_WORDS);
98        if (conf != null) {
99          String[] confSkipWords = conf.getStrings("hbase.envvars.logging.skipwords");
100         if (confSkipWords != null) {
101           skipWords.addAll(Arrays.asList(confSkipWords));
102         }
103       }
104
105       nextEnv:
106       for (Entry<String, String> entry : System.getenv().entrySet()) {
107         String key = entry.getKey().toLowerCase(Locale.ROOT);
108         String value = entry.getValue().toLowerCase(Locale.ROOT);
109         // exclude variables which may contain skip words
110         for(String skipWord : skipWords) {
111           if (key.contains(skipWord) || value.contains(skipWord))
112             continue nextEnv;
113         }
114         LOG.info("env:"+entry);
115       }
116     }
117     // and JVM info
118     logJVMInfo();
119   }
120
121   /**
122    * Parse and run the given command line. This may exit the JVM if
123    * a nonzero exit code is returned from <code>run()</code>.
124    */
125   public void doMain(String args[]) {
126     try {
127       int ret = ToolRunner.run(HBaseConfiguration.create(), this, args);
128       if (ret != 0) {
129         System.exit(ret);
130       }
131     } catch (Exception e) {
132       LOG.error("Failed to run", e);
133       System.exit(-1);
134     }
135   }
136 }