1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.hbtop;
19
20 import java.util.Objects;
21
22 import org.apache.commons.cli.BasicParser;
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.HelpFormatter;
25 import org.apache.commons.cli.Options;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.conf.Configured;
30 import org.apache.hadoop.hbase.HBaseConfiguration;
31 import org.apache.hadoop.hbase.HBaseInterfaceAudience;
32 import org.apache.hadoop.hbase.classification.InterfaceAudience;
33 import org.apache.hadoop.hbase.hbtop.mode.Mode;
34 import org.apache.hadoop.hbase.hbtop.screen.Screen;
35 import org.apache.hadoop.util.Tool;
36 import org.apache.hadoop.util.ToolRunner;
37
38
39
40
41 @InterfaceAudience.LimitedPrivate(HBaseInterfaceAudience.TOOLS)
42 public class HBTop extends Configured implements Tool {
43
44 private static final Log LOG = LogFactory.getLog(HBTop.class);
45
46 public HBTop() {
47 this(HBaseConfiguration.create());
48 }
49
50 public HBTop(Configuration conf) {
51 super(Objects.requireNonNull(conf));
52 }
53
54 @Override
55 public int run(String[] args) throws Exception {
56 long initialRefreshDelay = 3 * 1000;
57 Mode initialMode = Mode.REGION;
58 try {
59
60 Options opts = new Options();
61 opts.addOption("h", "help", false,
62 "Print usage; for help while the tool is running press 'h'");
63 opts.addOption("d", "delay", true,
64 "The refresh delay (in seconds); default is 3 seconds");
65 opts.addOption("m", "mode", true,
66 "The mode; n (Namespace)|t (Table)|r (Region)|s (RegionServer)"
67 + ", default is r (Region)");
68
69 CommandLine commandLine = new BasicParser().parse(opts, args);
70
71 if (commandLine.hasOption("help")) {
72 printUsage(opts);
73 return 0;
74 }
75
76 if (commandLine.hasOption("delay")) {
77 int delay = 0;
78 try {
79 delay = Integer.parseInt(commandLine.getOptionValue("delay"));
80 } catch (NumberFormatException ignored) {
81 }
82
83 if (delay < 1) {
84 LOG.warn("Delay set too low or invalid, using default");
85 } else {
86 initialRefreshDelay = delay * 1000L;
87 }
88 }
89
90 if (commandLine.hasOption("mode")) {
91 String mode = commandLine.getOptionValue("mode");
92 switch (mode) {
93 case "n":
94 initialMode = Mode.NAMESPACE;
95 break;
96
97 case "t":
98 initialMode = Mode.TABLE;
99 break;
100
101 case "r":
102 initialMode = Mode.REGION;
103 break;
104
105 case "s":
106 initialMode = Mode.REGION_SERVER;
107 break;
108
109 default:
110 LOG.warn("Mode set invalid, using default");
111 break;
112 }
113 }
114 } catch (Exception e) {
115 LOG.error("Unable to parse options", e);
116 return 1;
117 }
118
119 try (Screen screen = new Screen(getConf(), initialRefreshDelay, initialMode)) {
120 screen.run();
121 }
122
123 return 0;
124 }
125
126 private void printUsage(Options opts) {
127 new HelpFormatter().printHelp("hbase hbtop [opts] [-D<property=value>]*", opts);
128 System.out.println("");
129 System.out.println(" Note: -D properties will be applied to the conf used.");
130 System.out.println(" For example:");
131 System.out.println(" -Dhbase.client.zookeeper.quorum=<zookeeper quorum>");
132 System.out.println(" -Dzookeeper.znode.parent=<znode parent>");
133 System.out.println("");
134 }
135
136 public static void main(String[] args) throws Exception {
137 int res = ToolRunner.run(new HBTop(), args);
138 System.exit(res);
139 }
140 }