天天看點

OpenTSDB安裝與使用

1. 安裝環境

1. 系統版本

('centos', '6.4', 'Final')

2. 核心版本

Linux localhost.localdomain 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 22 00:31:26 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

3. 依賴安裝

1. java安裝

1.yum install java-1.6.0-openjdk-devel.x86_64

2. java環境變量設定  

在~/.bash_profile檔案中添加以下行。

# java

export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/

export PATH=$JAVA_HOME/bin:$PATH

export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

2 gnuplot安裝

yum install libotf

yum install gnuplot-common.x86_64

yum install gnuplot.x86_64

2. hbase 安裝

1.  源碼下載下傳

wget ​​http://mirror.bit.edu.cn/apache/hbase/hbase-0.94.27/hbase-0.94.27.tar.gz​​

2. 配置修改

1.修改hbase-env.sh中的如下屬性

export JAVA_HOME=/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/

export HBASE_MANAGES_ZK=true

2.單機配置修改hbase-site.xml:

<configuration>

<property>

        <name>hbase.rootdir</name>

        <value>/root/guochunyang/hbase_fs/</value>

</property>

</configuration>

3. 啟動hbase

./bin/start-hbase.sh

4. 關閉hbase

./bin/stop-hbase.sh

3. opentsdb 安裝

1. 下載下傳源碼

git clone ​​https://github.com/OpenTSDB/opentsdb.git​​

2.編譯

./build.sh

3. 建表

env COMPRESSION=NONE HBASE_HOME=../hbase-0.94.27 src/create_table.sh

4. 配置檔案 opentsdb.conf

[​​[email protected]​​​ opentsdb]# cat /etc/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

# Enables Nagel's algorithm to reduce the number of packets sent over the

# network, default is True

#tsd.network.tcpnodelay = true

# Determines whether or not to send keepalive packets to peers, default 

# is True

#tsd.network.keepalive = true

# Determines if the same socket should be used for new connections, default 

# is True

#tsd.network.reuseaddress = 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 ***

tsd.http.staticroot = build/staticroot

# Where TSD should write it's cache files to

# *** REQUIRED ***

tsd.http.cachedir = /tmp/tsd

# --------- 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

# --------- 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

# 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

5.啟動

帶參數模式

./build/tsdb tsd --port=4242 --staticroot=build/staticroot --cachedir="$tsdtmp" --zkquorum=127.0.0.1:2181

使用配置檔案

./build/tsdb tsd

6. 建立名額

1. 手動建立名額

[​​[email protected]​​​ opentsdb]# ./build/tsdb mkmetric mysql.bytes_received mysql.bytes_sent

metrics mysql.bytes_received: [0, 0, 1]

metrics mysql.bytes_sent: [0, 0, 2]

2.自動建立名額

需要修改配置檔案中tsd.core.auto_create_metrics = true,預設為false。

需要向tsdb的監聽端口發送消息。比如:

put tsd.connectionmgr.connections 1443170747 6 type=open host=localhost.localdomain

3.向OpenTSDB添加資料

(1)telnet或者nc

文法:

put <metric> <timestamp> <value> <tagk1=tagv1[ tagk2=tagv2 ...tagkN=tagvN]>

例如:

put sys.cpu.user 1356998400 42.5 host=webserver01 cpu=0

[[email protected] ~]# telnet 127.0.0.1 4242

Trying 127.0.0.1...

Connected to 127.0.0.1.

Escape character is '^]'.

put proc.test.telnet 1443426262 12 telnet_host=localhost

[[email protected] ~]# nc 127.0.0.1 4242

put proc.test.nc 1443426262 3 nc_host=localhost

(2)HTTP API

curl -H "Content-Type: application/json" -X POST  ​​http://127.0.0.1:4242/api/put/​​ -d '{"metic":"proc.test.curlpost","timestamp":1443427323,"value":18,"tags":{"host":"web01","dc":"lga"}}'

或者通過以下python腳本

[[email protected] opentsdb_test]# cat test_post.py 
#!/usr/bin/env python
#  -*- coding:utf-8 -*-

import httplib
import urllib
import urllib2
import json
import time
import random

def my_post():
    base_url = '10.77.51.10'
    headerdata =  {'Content-type':'application/json'}
    time_cur = int(time.time())
    va = random.randint(1,100)
    values = {
        "metric": "proc.test.httppost",
        "timestamp": time_cur,
        "value": va,
        "tags": {
           "host": "web01",
           "dc": "lga"
        }
    }
    jdata = json.dumps(values)
    conn = httplib.HTTPConnection(base_url,4242)
    conn.request(method="POST",url="/api/put/",body=jdata,headers=headerdata)
    res = conn.getresponse()
    res.read()
    # get response /api/put?details
    conn.request(method="POST",url="/api/put?details",body=jdata,headers=headerdata)
    res = conn.getresponse()
    return res.read()

while(True):
    try:
        resp = my_post()
        print(resp)
        time.sleep(1)
    except Exception as e:
        exit(0)
    except KeyboardInterrupt as e:
        exit(0)
[[email protected] opentsdb_test]# python test_post.py 
{"errors":[],"failed":0,"success":1}
{"errors":[],"failed":0,"success":1}
{"errors":[],"failed":0,"success":1}
{"errors":[],"failed":0,"success":1}
^C[[email protected] opentsdb_test]#      

參考​​http://opentsdb.net/docs/build/html/api_http/put.html​​

(3)批量導入

參考​​http://opentsdb.net/docs/build/html/user_guide/cli/import.html​​

3. tcollector安裝

1. 下載下傳源碼

 ​​https://github.com/OpenTSDB/tcollector​​

2. 啟動

[[email protected] tcollector]# pwd

/root/guochunyang/tcollector

[[email protected] tcollector]# python tcollector.py

4. opentsdb_test

1. 啟動opentsdb,配置支援tsd.core.auto_create_metrics = true,啟動hbase,啟動tcollector。用浏覽器打開,打開後如圖

2. 測試

Metric:test1.test1.test1

測試程式:

[[email protected] opentsdb_test]# cat test2.py

import subprocess

import time

import logging

import random

logging.basicConfig(level=logging.DEBUG,filename="test.log")

def put_data(metric,value,**kw):

        t = int(time.time())

        cmd = "echo put %s %d %d host=test1 tag_k1=tag_v1 | nc -w 30 127.0.0.1 4242" %(metric,t,value)

        subp = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)

random.seed()

while(True):

        i = random.randint(1,100)

        time.sleep(1)

        put_data("test1.test1.test1",i)