opentsdb-2-HTTP写入示例

it2025-01-11  5

1 终端写入

默认情况下,如果所有数据存储成功,响应一个204的状态码。如果有一个或多个数据点出错,返回400状态码和错误消息内容。

curl -i -X POST -d '{"metric":"press", "timestamp":1603270945, "value":6, "tags": {"host":"web02"}}' http://localhost:4242/api/put?summary

或者

curl -i -X POST -d '{"metric":"press", "timestamp":1603270941, "value":6, "tags": {"host":"web02"}}' http://localhost:4242/api/put?details

或者

curl -i -X POST -d '{"metric":"press", "timestamp":1603270948, "value":6, "tags": {"host":"10.10.10.10"}}' http://localhost:4242/api/put?summary

-i参数打印出服务器回应的 HTTP 标头。 -X参数指定 HTTP 请求的方法。 -d参数用于发送 POST 请求的数据体。

2 Python Request写入(短连接)

项目中基于python request包实现HTTP API接口,并进行封装写入。代码如下:这里其实是HTTP API的批量写入实现。

import requests data1 = { "metric": "press", "timestamp": '1603271000', "value": '129', "tags": { "host": "web01", "name": "motor" } } data2 = { "metric": "press", "timestamp": '1603272000', "value": '330', "tags": { "host": "web01", "name": "motor" } } datalist = [data1,data2] if __name__ == "__main__": res = requests.post("http://10.23.241.205:4242/api/put?details", json=datalist) print(res.text)

输出 {“success”:2,“failed”:0,“errors”:[]}

3 Python Request写入(长连接)

import time import math import requests if __name__ == "__main__": sess = requests.Session() st = int(time.time()) - 100000 datalist = [] for i in range(1, 100000): data = { "metric": "press", "timestamp": st, "value": math.cos(i)+10, "tags": { "host": "web01", "dc": "lga" } } st += 1 datalist.append(data) if len(datalist) == 50: res = sess.post("http://10.23.241.205:4242/api/put?details", json=datalist) print(res.text) datalist = []

4 查询

# -*- coding: utf-8 -*- import requests if __name__ == "__main__": query = 'start=1603274787&m=sum:press' result = requests.get("http://10.23.241.205:4242/api/query?" + query) print(result.json()[0]) print(result.json()[0]['dps'])

输出如下

{'metric': 'press', 'tags': {'host': 'web01', 'dc': 'lga'}, 'aggregateTags': [], 'dps': {'1603274787': 10.19815731048584, '1603274788': 9.282279968261719, '1603274789': 9.026270866394043}} {'1603274787': 10.19815731048584, '1603274788': 9.282279968261719, '1603274789': 9.026270866394043}

5 ms级写入及查询

(1)写入

import requests data1 = { "metric":"temperature", "timestamp":1567675709879, "value":20, "tags":{ "host":"web1" } } data2 = { "metric":"temperature", "timestamp":1567675709900, "value":60, "tags":{ "host":"web1" } } datalist = [data1,data2] if __name__ == "__main__": res = requests.post("http://10.23.241.205:4242/api/put?details", json=datalist) print(res.text)

(2)查询 To extract data with millisecond resolution, use the /api/query endpoint and specify the msResolution (ms is also okay, but not recommended) JSON parameter or query string flag and it will bypass down sampling (unless specified) and return all timestamps in Unix epoch millisecond resolution.

# -*- coding: utf-8 -*- import requests if __name__ == "__main__": query = 'start=1567675709879&ms&m=sum:temperature' result = requests.get("http://10.23.241.205:4242/api/query?" + query) print(result.json()[0]) print(result.json()[0]['dps'])

输出显示

{'metric': 'temperature', 'tags': {'host': 'web1'}, 'aggregateTags': [], 'dps': {'1567675709879': 20, '1567675709900': 60}} {'1567675709879': 20, '1567675709900': 60}

6 查询过滤条件

start=1356998400&m=sum:sys.cpu.user{host=webserver01,cpu=0}
最新回复(0)