Opentsdb Writing Introduction

Source: Internet
Author: User
Tags cos http request tag name opentsdb in python

Opentsdb supports several write methods. Includes the Telnet API, HTTP API, import method, and client collector (Tcollector/telegraf). Telnet is suitable for testing, Tcollector/telegraf is the log capture client can be used for streaming load logs, import method suitable for bulk import data. Of course, there is a final way to write HBase directly (not recommended on the website), so OPENTSDB can also show the data directly. First, create metric

Two ways, choose one. The metric must be set up regardless of the import method.
1. Create the metric in opentsdb beforehand. For example, generate two metric named Mymetric.data_1 and mymetric.data_2. As follows:

Tsdb mkmetric mymetric.data_1 mymetric.data_2

2. Set the auto-Generate metric. To modify the opentsdb.conf settings:

Tsd.core.auto_create_metrics = True
Second, Telnet Put mode

Reference: http://opentsdb.net/docs/build/html/api_telnet/put.html
Attempts to write data points to the store. The Telnet API is unable to process UTF-8 characters, instead use the/api/put method or directly with the Java API. The command format is as follows:

Put <metric> <timestamp> <value> <tagk_1>=<tagv_1>[<tagk_n>=<tagv_n>]

Terminal Run Example:

echo "Put Mymetric.test.data 1295643636 A=foo" | Nc-w Tsdhost Tsdport
Third, the HTTP API Put method

Reference: http://opentsdb.net/docs/build/html/api_http/put.html
This endpoint allows data to be stored over HTTP in Opentsdb instead of the Telnet interface. Placement requests can only be performed by the content associated with the Post method.
To conserve bandwidth, the put API allows clients to store multiple data points in a single request. There can be no correlation between data points. Each data point is processed separately, and the error of one data will not affect the storage of good data. This means that if your request has 100 data points, 1 of the data points have errors. The error data point is then rejected for writing, but the remaining 99 data points will still be written.
Although the API supports multiple data points per request, the API does not return until each request is processed. This means that you must validate the measure and tag name/value, resolve the value, and queue the data for storage. If your put request has a large number of data points, the API may take a long time to respond, especially if OPENTSDB must assign the UID to the tag name or value. Therefore, it is best to limit the maximum number of data points per request; 50 each request is a good starting point.
Another recommendation is to enable keep-alives on the HTTP client so that you can reuse the connection to the server each time you place data. 1 HTTP Request parsing

2 Data Write sample single point data write

{
    "metric": "Sys.cpu.nice",
    "timestamp": 1346846400,
    "value": "
    Tags": {
       "host": "Web01",
       "DC": "LGA"
    }
}
Multi-point data write
Multi-point data writes must be packaged in an array form
[
    {
        "metric": "Sys.cpu.nice",
        "timestamp": 1346846400,
        "value": "
        Tags": {
           "host": " Web01 ",
           " DC ":" LGA "
        }
    },
    {
        " metric ":" Sys.cpu.nice ",
        " timestamp ": 1346846400,
        " Value ": 9,
        " tags ": {
           " host ":" WEB02 ",
           " DC ":" LGA "
        }
    }
]
3 Return response

By default, if all data points have been successfully stored, the put endpoint responds with an HTTP 204 status code without content. If one or more data points have errors, the API returns a 400 status code with an error message in the content. Response parsing instructions and response examples are shown in the following illustration:
Four, batch import

Reference: http://opentsdb.net/docs/build/html/user_guide/cli/import.html
The Import command allows batch loading of time series data into Opentsdb. can provide one or more files, Opentsdb will parse and load the data. The data must match the data format of the Telnet put, with each row in the text file as a data point. In addition, the file can optionally be compressed with gzip and end with the. gz extension. 1 Input Format

The path can be a relative path or an absolute path.

Import path [... paths]
import/home/hobbes/timeseries1.gz/home/hobbes/timeseries2.gz
2 Parameter Description

The format and the Telnet interface are the same.

<metric> <timestamp> <value> <tagk=tagv> [<tagkn=tagvn>]
Metricis the name of the metric. Note that the metric name is not include spaces. timestampis the absolute timestamp of the data point in seconds or milliseconds valueis the value to store TAGK=TAGVis a pair of one or more space sparate tag name and value pairs. Note that the tags could not be spaces in them.

Example:

Sys.cpu.user 1356998400 host=web01 cpu=0

The code examples are as follows:

./tsdb Import--config=/applications/opentsdb/src/opentsdb.conf/applications/opentsdb/
my_test_data/ Opentsdb.txt

The contents of the file are formatted as follows, where the timestamp must be ascending:

Mymetric.test.data 1479303678 0.841470984808 host=xyd_host
mymetric.test.data 1479303679 0.909297426826 host=xyd _host
mymetric.test.data 1479303680 0.14112000806 host=xyd_host
mymetric.test.data 1479303681- 0.756802495308 host=xyd_host
mymetric.test.data 1479303682-0.958924274663 host=xyd_host
Mymetric.test.data 1479303683-0.279415498199 host=xyd_host
mymetric.test.data 1479303684 0.656986598719 host= Xyd_host
mymetric.test.data 1479303685 0.989358246623 host=xyd_host
mymetric.test.data 1479303686 0.412118485242 host=xyd_host
mymetric.test.data 1479303687-0.544021110889 host=xyd_host
Mymetric.test.data 1479303688-0.999990206551 host=xyd_host
mymetric.test.data 1479303689-0.536572918 host=xyd _host

v. Tcollector/telegraf Client Write

Here we use Tcollect to collect, Telegraf do not do demo. Tcollector is a client program that collects the metrics data for this machine and sends the data to the OPENTSDB.
Tcollector GitHub Address: https://github.com/OpenTSDB/tcollector/
Tcollector Official Address: http://opentsdb.net/docs/build/html/user_guide/utilities/tcollector.html
The installation and introduction of Tcollector is not explained in detail here. Shown here is the official requirement to define a collector directly in Python and send data to the OPENTSDB.
Run:

sudo./tcollector start

Customize a mycollector.py with Python, note that the file permission is 755, or it can be set directly to 777.

#!/usr/bin/env python 

import sys
import time
import math from
collectors.lib import utils

Collection_interval = 5  # seconds

def getValue (num):
    return Math.Cos (NUM)

def main ():
    Utils.drop _privileges ()
    i = +
    + True:
        print ("Xyd.test.cos%d%s"% (int (time.time ()), GetValue (i)))
        i + = 0.1
        Sys.stdout.flush ()
        time.sleep (collection_interval)

if __name__ = = "__main__":
    Sys.stdin.close ()
    sys.exit (Main ())

The execution effect is as follows, generating a COS function with only positive values, seemingly opentsdb not showing negative numbers:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.