OpenTsdb安装使用
一、简介
OpenTSDB是一个时间序列数据库,由一个 Time Series Daemon (TSD) 和一系列命令行实用程序组成。与OpenTSDB交互主要通过运行一个或者多个 TSD 来实现。每个 TSD 是独立的。没有master,没有共享状态,所以你可以运行尽可能多的 TSD 来处理工作负载。
每个 TSD 使用开源数据库 HBase 或托管Google Bigtable服务来存储和检索时间序列数据。数据模式针对类似时间序列的快速聚合进行了高度优化,以最大限度的减少存储空间。TSD 的用户不需要直接访问底层仓库。你可以通过简单的telnet样式协议,HTTP API 或者简单的内置 GUI 与 TSD 进行通信。所有的通信都发生在同一个端口上(TSD 通过查看接收到的前几个字节来计算出客户端的协议)。
其主要用于处理带时间标签(按照时间的顺序变化,即时间序列化)的数据,带时间标签的数据也称为时间序列数据。时间序列数据主要由电力行业、化工行业、物联网行业等各类型实时监测、检查与分析设备所采集、产生的数据,这些数据的典型特点是:产生频率快(每一个监测点一秒钟内可产生多条数据)、严重依赖于采集时间(每一条数据均要求对应唯一的时间)、测点多信息量大(常规的实时监测系统均有成千上万的监测点,监测点每秒钟都产生数据,每天产生几十GB的数据量)。
OpenTSDB存储的一些核心概念
1)Metric:即平时我们所说的监控项。譬如上面的CPU使用率
2)Tags:就是一些标签,在OpenTSDB里面,Tags由tagk和tagv组成,即tagk=takv。标签是用来描述Metric的,譬如上面为了标记是服务器A的CpuUsage,tags可为hostname=qatest
3)Value:一个Value表示一个metric的实际数值,譬如上面的99%
4)Timestamp:即时间戳,用来描述Value是什么时候的;譬如上面的21:00
5)Data Point:即某个Metric在某个时间点的数值。
Data Point包括以下部分:Metric、Tags、Value、Timestamp。
比如:服务器hadoop-server-001在21:00时候的cpu使用率,就是1个DataPoint。
二、安装准备
1.操作环境
要求A Linux system (or Windows with manual building):Centos7
要求Java Runtime Environment 1.6 or later:java1.8
2.hadoop生态部署
要求有zk、hadoop、hbase
HBase 0.92 or later:hbase2.2.6
3.安装gnuplot
要求:GnuPlot 4.2 or later
Opentsdb依赖Gnuplot,它 是一个命令行的交互式绘图工具。用户通过输入命令,可以逐步设置或修改绘图环境,并以图形描述数据或函数,使我们可以借由图形做更进一步的分析。
yum install gnuplot
此处有个问题,不要选择下载gnuplot安装包手动安装,手动安装后面opentsdb的gui界面会出现展示问题,如:unknown or ambiguous terminal type; type just ‘set terminal‘ for a list
4.下载opentsdb安装包
官网:http://opentsdb.net/overview.html
可以选择rpm方式安装、或是源码编译安装。
本文选择源码编译安装,这样好控制安装路径。下载:opentsdb-2.4.0.tar.gz
三、安装
1.解压安装包
#进入安装包所在目录
cd /usr/local/apps/
#解压安装包
tar -zxvf opentsdb-2.4.0.tar.gz
#重命名
mv opentsdb-2.4.0 opentsdb
2.编译
#进入opentsdb源码目录
cd opentsdb
#创建目录
mkdir build
#拷贝opentsdb下的third_party目录到build目录中去(具体原因见下文注意部分)
cp -r third_party ./build
#编译
./build.sh
#等待其编译安装完成即可,编译会有一段时间
#编译成功的标志是在openTSDB父目录下的build文件夹中会生成一个jar包:tsdb-2.4.0.jar
注意:
不拷贝opentsdb下的third_party目录到build目录中再编译的话会出错: make[1]: *** [.javac-stamp] 错误 2 make[1]: 离开目录“/usr/local/apps/opentsdb/build” make: *** [all] 错误 2
3.执行创建hbase表脚本
若是第一次搭建opentsdb则执行脚本,若是已经创建过,则跳过此步骤
#进入opentsdb目录下的src目录中,其下有创建hbase表的脚本
cd /usr/local/apps/opentsdb/src
#执行脚本
env COMPRESSION=NONE HBASE_HOME=/usr/local/apps/hbase ./create_table.sh
COMPRESSION参数指定压缩方式,可选值是 NONE,LZO,GZIP,或者 SNAPPY 。这个命令将在指定的HBase中创建四张表:tsdb, tsdb-uid, tsdb-tree 和 tsdb-meta。如果你只是评估OpenTSDB,现在就不用关心压缩方式。在生产环境中,你要使用一个最合适的有效压缩库。
#执行完查看hbase
hbase(main):004:0> list
TABLE
tsdb
tsdb-meta
tsdb-tree
tsdb-uid
4.配置
#进入opentsdb源码父目录下的src目录
cd /usr/local/apps/opentsdb/src
#拷贝目录下面的opentsdb.conf配置文件到编译出的build目录
cp opentsdb.conf /usr/local/apps/opentsdb/build/
#进入build目录
cd /usr/local/apps/opentsdb/build/
#编辑opentsdb.conf配置文件
vi opentsdb.conf
加入或修改以下内容:
# --------- NETWORK ----------
# The TCP port TSD should use for communications
# *** REQUIRED ***
tsd.network.port = 4242
# The IPv4 network address to bind to, defaults to all addresses
# tsd.network.bind = 0.0.0.0
# Disable Nagel's algorithm, default is True
#tsd.network.tcp_no_delay = true
# Determines whether or not to send keepalive packets to peers, default
# is True
#tsd.network.keep_alive = true
# Determines if the same socket should be used for new connections, default
# is True
#tsd.network.reuse_address = true
# Number of worker threads dedicated to Netty, defaults to # of CPUs * 2
#tsd.network.worker_threads = 8
# Whether or not to use NIO or tradditional blocking IO, defaults to True
#tsd.network.async_io = true
# ----------- HTTP -----------
# The location of static files for the HTTP GUI interface.
# *** REQUIRED ***
#静态资源路径,在build目录下的staticroot
tsd.http.staticroot = /usr/local/apps/opentsdb/build/staticroot
# Where TSD should write it's cache files to
# *** REQUIRED ***
#目录缓存,运行过程中发现会产生一堆文件,建议该目录设置在/tmp下,让Linux可以清理
tsd.http.cachedir = /srv/opentsdb/tmp
# --------- CORE ----------
# Whether or not to automatically create UIDs for new metric types, default
# is False
#自动创建标签
#tsd.core.auto_create_metrics = false
tsd.core.auto_create_metrics = true
# Whether or not to enable the built-in UI Rpc Plugins, default
# is True
#tsd.core.enable_ui = true
# Whether or not to enable the built-in API Rpc Plugins, default
# is True
#tsd.core.enable_api = true
# --------- STORAGE ----------
# Whether or not to enable data compaction in HBase, default is True
#tsd.storage.enable_compaction = true
# How often, in milliseconds, to flush the data point queue to storage,
# default is 1,000
# tsd.storage.flush_interval = 1000
# Max number of rows to be returned per Scanner round trip
# tsd.storage.hbase.scanner.maxNumRows = 128
# Name of the HBase table where data points are stored, default is "tsdb"
#tsd.storage.hbase.data_table = tsdb
# Name of the HBase table where UID information is stored, default is "tsdb-uid"
#tsd.storage.hbase.uid_table = tsdb-uid
# Path under which the znode for the -ROOT- region is located, default is "/hbase"
#tsd.storage.hbase.zk_basedir = /hbase
# A comma separated list of Zookeeper hosts to connect to, with or without
# port specifiers, default is "localhost"
#tsd.storage.hbase.zk_quorum = localhost
#zk地址
tsd.storage.hbase.zk_quorum = hadoop-server-001:2181,hadoop-server-002:2181,hadoop-server-003:2181
#解析具有重复时间戳的数据点时是否接受最后写入的值。结合压缩启用时,将使用最新数据点写入压缩列,默认false
tsd.storage.fix_duplicates = true
#是否启用对HTTP RPC的传入块支持
tsd.http.request.enable_chunked = true
#启用分块时支持传入HTTP请求的最大请求正文大小
tsd.http.request.max_chunk = 65536
#每个数据点允许的最大标签数。注意请注意过度使用标签写入的性能影响。默认8
tsd.storage.max_tags = 16
#当查询包含尚未分配UID且可能不存在的标记值时是否继续查询
tsd.query.skip_unresolved_tagvs = true
#写入数据点时是否将数据追加到列,而不是为每个值创建新列。避免每个小时后都进行压缩,但是可以在HBase上使用更多资源。(有待商讨)
tsd.storage.enable_appends = true
#一个本地化时区标识字符串,用于在执行查询时将绝对时间转换为UTC时覆盖本地系统时区。这不会影响传入的数据时间戳。例如America/Los_Angeles
tsd.core.timezone=Asia/Shanghai
#tag值的uid宽度,默认为3
tsd.storage.uid.width.tagv = 4
# --------- COMPACTIONS ---------------------------------
# Frequency at which compaction thread wakes up to flush stuff in seconds, default 10
# tsd.storage.compaction.flush_interval = 10
# Minimum rows attempted to compact at once, default 100
# tsd.storage.compaction.min_flush_threshold = 100
# Maximum number of rows, compacted concirrently, default 10000
# tsd.storage.compaction.max_concurrent_flushes = 10000
# Compaction flush speed multiplier, default 2
# tsd.storage.compaction.flush_speed = 2
# ------Kerberos配置---------
hbase.security.auth.enable = true
hbase.security.authentication = kerberos
#替换为实际的hbase账号
hbase.kerberos.regionserver.principal = hbase/_HOST@HADOOP.COM
hbase.sasl.clientconfig = Client
5.在build目录下创建jaas.conf
#进入build
cd /usr/local/apps/opentsdb/build
#创建编辑jaas.conf
vi jaas.conf
#加入以下内容,根据实际kerberos账号来
Client {
com.sun.security.auth.module.Krb5LoginModule required
useKeyTab=true
keyTab="/usr/data/kerberos/keytab/hbase.keytab"
storeKey=true
useTicketCache=false
principal="hbase/hadoop-server-001@HADOOP.COM";
};
6.在build目录下创建启动脚本start_tsdb.sh
#进入build
cd /usr/local/apps/opentsdb/build
#创建编辑start_tsdb.sh,脚本名称自定义
vi start_tsdb.sh
#加入以下内容
OPENTSDB_HOME=/usr/local/apps/opentsdb/build
JVMARGS="${JVMARGS} -Djava.security.auth.login.config=${OPENTSDB_HOME}/jaas.conf -Dzookeeper.sasl.client=false" ./tsdb tsd --config=${OPENTSDB_HOME}/opentsdb.conf
#编辑好脚本后对其赋予可执行权限
chmod +x start_tsdb.sh
7.修改日志目录
日志的路径配置在解压包下的src目录(注意是源码解压包)的logback.xml,即/usr/local/apps/opentsdb/src/
下的logback.xml
中配置
修改以下部分中路径指定到你想要的路径即可:
<!-- Appender to write OpenTSDB data to a set of rotating log files -->
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/var/log/opentsdb/opentsdb.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>/home/y/logs/opentsdb2/opentsdb.log.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>4</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>512MB</maxFileSize>
</triggeringPolicy>
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%date{ISO8601} [%thread] %-5level [%logger{0}.%M] - %msg%n</pattern>
</encoder>
</appender>
<!-- Appender for writing full and completed queries to a log file. To use it, make
sure to set the "level" to "INFO" in QueryLog below. -->
<appender name="QUERY_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/var/log/opentsdb/queries.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<fileNamePattern>/var/log/opentsdb/queries.log.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>4</maxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<maxFileSize>128MB</maxFileSize>
</triggeringPolicy>
<encoder>
<pattern>%date{ISO8601} [%logger.%M] %msg%n</pattern>
</encoder>
</appender>
本文均更改前缀到/srv/opentsdb/log
下
注意:要求之后启动时启动用户有权限操作读取该日志目录
8.启动
8.1直接启动
#要求保证/srv/opentsdb/log路径存在
nohup ./start_tsdb.sh > /srv/opentsdb/log/info.log 2>&1 &
#启动好后查看,会发现存在进程TSDMain
jps
访问web界面:ip:4242
8.2编写脚本启动
为了更方便的进行启动停止,在/usr/local/apps/opentsdb/build
目录下编写脚本:
vi release.sh
,并添加如下内容:
#!/bin/sh
## 基础变量
SERVICE_DIR="${PWD}"
SERVICE_NAME=opentsdb
SERVICE_PORT=4242
##启动、关闭、重启服务配置
case "$1" in
start)
#确定服务是否已启动,从端口号、服务名等参数来区分
procedure=`ps -ef | grep -w "${SERVICE_NAME}" |grep -w "java"| grep -v "grep" | awk '{print $2}'`
echo "${procedure}..."
if [ "${procedure}" = "" ];
then
echo "start ..."
#执行服务启动
echo "port=${SERVICE_PORT}"
exec nohup ${SERVICE_DIR}/start_tsdb.sh > /srv/opentsdb/log/info.log 2>&1 &
echo "${SERVICE_NAME} start success"
else
echo "${SERVICE_NAME} is started"
fi
;;
stop)
procedure=`ps -ef | grep -w "${SERVICE_NAME}" |grep -w "java"| grep -v "grep" | awk '{print $2}'`
if [ "${procedure}" = "" ];
then
echo "${SERVICE_NAME} is stopped"
else
kill -9 ${procedure}
sleep 1
argprocedure=`ps -ef | grep -w "${SERVICE_NAME}" |grep -w "java"| grep -v "grep" | awk '{print $2}'`
if [ "${argprocedure}" = "" ];
then
echo "${SERVICE_NAME} stop success"
else
kill -9 ${argprocedure}
echo "${SERVICE_NAME} stop error"
fi
fi
;;
restart)
$0 stop
sleep 1
$0 start $2
;;
*)
echo "usage: $0 [start|stop|restart] "
;;
esac
之后启动opentsdb,则直接使用脚本:
#进入目录
cd /usr/local/apps/opentsdb/build
#赋予可执行权限
chmod +x release.sh
#启动
./release.sh start
#重启
./release.sh restart
#停止
./release.sh stop
9.集群高可用
其本身没有集群等概念,openTSDB本身没有分布式的实现方案,而是借助于HBase的分布式集群方案,也就是说,不同节点之上的openTSDB访问同一个HBase集群,返回相同的数据镜像。
在其他节点准备好安装环境后:
可以在不同节点部署opentsdb时重复以上步骤,用同一个hbase集群即可。
注意:发送整体opentsdb包到其他节点,会出现各种问题,所以最好还是按照步骤重新安装一遍。
四、初步验证
1.插入数据
在命令行中输入:
#插入两条数据:
curl -X 'POST' 'http://hadoop-server-001:4242/api/put?summary' -d '
{
"metric": "sys.cpu.nice",
"timestamp": 1346846400,
"value": 19,
"tags": {
"host": "web01",
"dc": "lga"
}
}'
curl -X 'POST' 'http://hadoop-server-001:4242/api/put?summary' -d '
{
"metric": "sys.cpu.nice",
"timestamp": 1346846500,
"value": 20,
"tags": {
"host": "web01",
"dc": "lga"
}
}'
#运行后返回值如下:
{"success":1,"failed":0}
2.web页面查看
选好相应时间、标签等即可看到:
3.查询数据
#执行如下
curl -X 'POST' 'http://hadoop-server-001:4242/api/query' -d '
{
"start": "1346846300",
"end": "1346846500",
"queries": [{
"aggregator": "sum",
"metric": "sys.cpu.nice",
"tags": {"host": "web01","dc": "lga"}
}]
}'
#返回如下:
[{"metric":"sys.cpu.nice","tags":{"host":"web01","dc":"lga"},"aggregateTags":[],"dps":{"1346846400":19,"1346846500":20}}]