001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.hbtop;
019
020import java.util.Objects;
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.conf.Configured;
023import org.apache.hadoop.hbase.HBaseConfiguration;
024import org.apache.hadoop.hbase.HBaseInterfaceAudience;
025import org.apache.hadoop.hbase.hbtop.mode.Mode;
026import org.apache.hadoop.hbase.hbtop.screen.Screen;
027import org.apache.hadoop.util.Tool;
028import org.apache.hadoop.util.ToolRunner;
029import org.apache.yetus.audience.InterfaceAudience;
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine;
034import org.apache.hbase.thirdparty.org.apache.commons.cli.DefaultParser;
035import org.apache.hbase.thirdparty.org.apache.commons.cli.HelpFormatter;
036import org.apache.hbase.thirdparty.org.apache.commons.cli.Options;
037
038
039/**
040 * A real-time monitoring tool for HBase like Unix top command.
041 */
042@InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
043public class HBTop extends Configured implements Tool {
044
045  private static final Logger LOGGER = LoggerFactory.getLogger(HBTop.class);
046
047  public HBTop() {
048    this(HBaseConfiguration.create());
049  }
050
051  public HBTop(Configuration conf) {
052    super(Objects.requireNonNull(conf));
053  }
054
055  @Override
056  public int run(String[] args) throws Exception {
057    long initialRefreshDelay = 3 * 1000;
058    Mode initialMode = Mode.REGION;
059    try {
060      // Command line options
061      Options opts = new Options();
062      opts.addOption("h", "help", false,
063        "Print usage; for help while the tool is running press 'h'");
064      opts.addOption("d", "delay", true,
065        "The refresh delay (in seconds); default is 3 seconds");
066      opts.addOption("m", "mode", true,
067        "The mode; n (Namespace)|t (Table)|r (Region)|s (RegionServer)"
068          + ", default is r (Region)");
069
070      CommandLine commandLine = new DefaultParser().parse(opts, args);
071
072      if (commandLine.hasOption("help")) {
073        printUsage(opts);
074        return 0;
075      }
076
077      if (commandLine.hasOption("delay")) {
078        int delay = 0;
079        try {
080          delay = Integer.parseInt(commandLine.getOptionValue("delay"));
081        } catch (NumberFormatException ignored) {
082        }
083
084        if (delay < 1) {
085          LOGGER.warn("Delay set too low or invalid, using default");
086        } else {
087          initialRefreshDelay = delay * 1000L;
088        }
089      }
090
091      if (commandLine.hasOption("mode")) {
092        String mode = commandLine.getOptionValue("mode");
093        switch (mode) {
094          case "n":
095            initialMode = Mode.NAMESPACE;
096            break;
097
098          case "t":
099            initialMode = Mode.TABLE;
100            break;
101
102          case "r":
103            initialMode = Mode.REGION;
104            break;
105
106          case "s":
107            initialMode = Mode.REGION_SERVER;
108            break;
109
110          default:
111            LOGGER.warn("Mode set invalid, using default");
112            break;
113        }
114      }
115    } catch (Exception e) {
116      LOGGER.error("Unable to parse options", e);
117      return 1;
118    }
119
120    try (Screen screen = new Screen(getConf(), initialRefreshDelay, initialMode)) {
121      screen.run();
122    }
123
124    return 0;
125  }
126
127  private void printUsage(Options opts) {
128    new HelpFormatter().printHelp("hbase hbtop [opts] [-D<property=value>]*", opts);
129    System.out.println("");
130    System.out.println(" Note: -D properties will be applied to the conf used.");
131    System.out.println("  For example:");
132    System.out.println("   -Dhbase.client.zookeeper.quorum=<zookeeper quorum>");
133    System.out.println("   -Dzookeeper.znode.parent=<znode parent>");
134    System.out.println("");
135  }
136
137  public static void main(String[] args) throws Exception {
138    int res = ToolRunner.run(new HBTop(), args);
139    System.exit(res);
140  }
141}