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.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.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   * Base class for command lines that start up various HBase daemons.
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     * Implementing subclasses should return a usage string to print out.
55     */
56    protected abstract String getUsage();
57  
58    /**
59     * Print usage information for this command line.
60     *
61     * @param message if not null, print this message before the usage info.
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     * Log information about the currently running JVM.
74     */
75    public static void logJVMInfo() {
76      // Print out vm stats before starting up.
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     * Logs information about the currently running JVM process including
87     * the environment variables. Logging of env vars can be disabled by
88     * setting {@code "hbase.envvars.logging.disabled"} to {@code "true"}.
89     * <p>If enabled, you can also exclude environment variables containing
90     * certain substrings by setting {@code "hbase.envvars.logging.skipwords"}
91     * to comma separated list of such substrings.
92     */
93    public static void logProcessInfo(Configuration conf) {
94      // log environment variables unless asked not to
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         // exclude variables which may contain skip words
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     // and JVM info
117     logJVMInfo();
118   }
119 
120   /**
121    * Parse and run the given command line. This may exit the JVM if
122    * a nonzero exit code is returned from <code>run()</code>.
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 }