运维监控 | 为HBase集成hbtop
1. 前言
hbtop 是一种基于 Unix 的"top"命令建模的 HBase 实时监控工具,其模仿 Unix 的top命令,可以显示集群的摘要信息以及每个 RegionServer/Region/Namespace/Table 的度量指标。使用此工具,您可以查看按选定字段排序的指标,并且可以过滤指标,以及可以查看您感兴趣的内容。此外,借助向下钻取的功能,您可以挖掘热点区域。
根据(https://issues.apache.org/jira/browse/HBASE-11062)中所说,hbase hbtop 命令在 hbase2.1.7、2.2.2 和 2.3.0 版本中可以直接使用,并且社区正在积极地把这个特性移植到 branch-1 中。
目前,hbase 1.4.11 版本中已经加入了 hbase-hbtop 模块,若想在 hbase1.x 中使用这个功能,也可以借鉴以下步骤进行集成。
2. 快速体验
3. 为 HBase2.1.0-cdh6.3.2 集成 hbtop 的功能
我们线上HBase集群使用的版本是 HBase2.1.0-cdh6.3.2,同时也有 HBase2.1.0-cdh6.3.1的测试环境,6.3.2的源码编译的jar包一样可以在6.3.1集群中使用。
线上当前版本的HBase虽然没有直接包含hbase hbtop的命令,但是,按照如下步骤操作之后,同样可以体验这个功能。
3.1 源码准备
从 github 上下载 HBase2.1.0-cdh6.3.2 源码和 Apache HBase 2.1.7 源码。鉴于 github 的网速,可以选择在 gitee 上下载源码。
https://gitee.com/leojie/cloudera_hbase/tree/cdh6.3.2-release/
https://github.com/cloudera/hbase/tree/cdh6.3.2
https://gitee.com/leojie/hbase/tree/rel%2F2.1.7/
https://github.com/apache/hbase/releases/tag/rel%2F2.1.7
把 Apache HBase 的 hbase-hbtop moudle 拷贝到 CDH HBase 里。
在 CDH HBase 的 pom.xml 添加:
<module>hbase-hbtop</module>
<dependency>
  <artifactId>hbase-hbtop</artifactId>
  <groupId>org.apache.hbase</groupId>
  <version>${project.version}</version>
</dependency>
「新增 hbase-hbtop 的 module」
「新增 hbase-hbtop 的依赖」
「修改 hbase-hbtop 模块 pom 文件的父依赖版本号」
「最终 hbase 的项目结构」
3.2 编译 hbase-hbtop 模块
<profiles>
 <profile>
    <id>aliyun</id>
    <repositories>
      <repository>
        <id>myRepository1_1</id>
        <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
        <releases>
          <enabled>true</enabled>
        </releases>
        <snapshots>
          <enabled>true</enabled>
          <updatePolicy>always</updatePolicy>
        </snapshots>
      </repository>
    </repositories>
  </profile>
  <profile>
    <id>cloudera-release</id>
    <repositories>
      <repository>
        <id>cloudera-lib</id>
        <name>cloudera repository</name>
        <url>https://repository.cloudera.com/artifactory/libs-release-local</url>
        <releases>
          <enabled>true</enabled>
        </releases>
        <snapshots>
          <enabled>true</enabled>
          <updatePolicy>always</updatePolicy>
        </snapshots>
      </repository>
    </repositories>
  </profile>
  <profile>
    <id>cloudera-lib</id>
    <repositories>
      <repository>
        <id>cloudera-lib</id>
        <name>cloudera repository</name>
        <url>https://repository.cloudera.com/artifactory/cloudera-repos</url>
        <releases>
          <enabled>true</enabled>
        </releases>
        <snapshots>
          <enabled>true</enabled>
          <updatePolicy>always</updatePolicy>
        </snapshots>
      </repository>
    </repositories>
  </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>aliyun</activeProfile>
    <activeProfile>cloudera-release</activeProfile>
    <activeProfile>cloudera-lib</activeProfile>
  </activeProfiles>
进入 hbase-hbtop 项目下执行 maven 的打包命令:
cd ~/other_project/cloudera/hbase/hbase-cdh6.3.2-release/hbase-hbtop
mvn clean package -DskipTests=true
编译成功后的命令提示:
编译成功后的项目结构:
3.3 安装 hbase hbtop 命令
上传hbase-hbtop-2.1.0-cdh6.3.2.jar到任意一台 RegionServer 上,当然你也可以在所有 HBase 节点上安装此命令,这样你就可以在任意一台机器上使用它。
将 hbase-hbtop-2.1.0-cdh6.3.2.jar 拷贝到/opt/cloudera/parcels/CDH/lib/hbase/lib 目录下
修改 /opt/cloudera/parcels/CDH/lib/hbase/bin/hbase
elif [ "$COMMAND" = "hbtop" ] ; then
  CLASS='org.apache.hadoop.hbase.hbtop.HBTop'
  if [ -n "${shaded_jar}" ] ; then
    for f in "${HBASE_HOME}"/lib/hbase-hbtop*.jar; do
      if [ -f "${f}" ]; then
        CLASSPATH="${CLASSPATH}:${f}"
        break
      fi
    done
    for f in "${HBASE_HOME}"/lib/commons-lang3*.jar; do
      if [ -f "${f}" ]; then
        CLASSPATH="${CLASSPATH}:${f}"
        break
      fi
    done
  fi
  HBASE_OPTS="${HBASE_OPTS} -Dlog4j.configuration=file:${HBASE_HOME}/conf/log4j-hbtop.properties"
添加位置如图所示 629 行~ 646 行
接着在/opt/cloudera/parcels/CDH/lib/hbase/conf 目录下添加一个 log4j-hbtop.properties 文件,用来设置 hbtop 的日志级别。
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
log4j.rootLogger=WARN,console
log4j.threshold=WARN
# console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.err
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ISO8601} %-5p [%t] %c{2}: %m%n
# ZooKeeper will still put stuff at WARN
log4j.logger.org.apache.zookeeper=ERROR
3.4 启动 hbtop
在终端启动 hbtop,hbase hbtop
4. 开始使用
运行 hbtop 之后,您将看到类似下面的内容:
顶部屏幕包含“摘要和指标”部分。在摘要部分,你可以看到 HBase Version,Cluster ID,The number of region servers,Region count,Average Cluster Load 和 Aggregated Request/s。在指标部分中,您可以查看每个 Region / Namespace / Table / RegionServer 的指标,具体取决于所选模式。顶部屏幕会定期刷新-默认情况下为 3 秒。
您可以在“指标”部分中滚动指标记录。
「命令行参数」
| 选项 | 描述 | 
|---|---|
| -d,-delay | 刷新延迟(以秒为单位);默认是 3 秒 | 
| -h,-帮助 | 打印使用情况;在工具运行时寻求帮助,请 h 按键 | 
| -m,-模式 | 模式 n(namespace) | 
您可以通过按 m 顶部屏幕中的键来更改模式。
通过按 d 键更改刷新率。
通过按 f 键来更改现场屏幕。在字段屏幕中,可以通过选择一个字段并 d 按键或 space 键来更改显示的字段。
图片上传失败,请查看文末参考链接。
通过按 f 键移至字段屏幕。在字段屏幕中,可以通过选择一个字段并按s键来更改排序字段。同样,您可以通过按R键更改排序顺序(升序或降序)。
图片上传失败 ,请查看文末 参考链接 。通过按 f 键移至字段屏幕。在字段屏幕中,您可以更改字段的顺序。
图片上传失败,请查看文末参考链接。
使用过滤器功能过滤度量标准记录。通过 o 按键忽略大小写或 O 按键区分大小写来添加过滤器。
图片上传失败,请查看文末参考链接。
过滤器的语法如下:
<Field><Operator><Value>
筛选示例语句如下:
NAMESPACE==default
REQ/S>1000
比较运算符
| 运算符 | 描述 | 
|---|---|
| = | Partial match | 
| == | Exact match | 
| > | Greater than | 
| >= | Greater than or equal to | 
| < | Less than | 
| <= | Less than and equal to | 
通过按^o 键查看当前过滤器,通过按=键清除它们,如需帮助,请按 h
5. 总结
hbase hbtop 命令可以帮助我们很方便的查看每张表、每个region的请求量以及其他指标信息,有利于我们排查集群读写的热点问题。
hbtop的命令使用非常简单,参考HBase官网文档或者下方链接,稍微练习就能熟练上手。
以上演示图片如果有上传失败不能看的,可以自行进行测试或者访问文末的参考链接。
6. 参考链接
-  
   HBase 集成 hbtop 
-  
   介绍一个 HBase 的监控工具 
