本文描述通过被监控endpoint的名称获取该endpoint的eid和监控项,从而获取到该endpoint的监控历史数据,使用python代码的 api操作方法
注:同步open-falcon和agent的时间,不然获取不到数据
http://open-falcon.org/falcon-plus/#/endpoints
访问的api有:
/api/v1/graph/endpoint?q={0}
/api/v1/graph/endpoint_counter?eid={0}
/api/v1/graph/history
具体操作:
# -*- coding: UTF-8 -*- #!/usr/bin/env python # Created by Administrator on 2017/12/15 import json import time import requests in_ip = 'localhost.localdomain' user = 'root' sig = '78d70632d20311e7bf7d000c298269bc' # 注:sig为数据库uic表中用户对应的sig domain = 'http://192.168.67.129:8080' # api对应端口为8080
api_token = '{"name":"' + user + '", "sig":"' + sig + '"}' directiry = "/api/v1/graph/endpoint?q={0}".format(in_ip) falcon_header = { "Apitoken": api_token, "X-Forwarded-For": "127.0.0.1", "Content-Type": "application/json", "name": user, "sig": sig } params = { 'url': domain + directiry, 'headers': falcon_header, 'timeout': 30 } res1 = requests.get(**params) data1 = json.loads(res1.text) print('得到eid',data1) #========================================================================= point_id = data1[0]["id"] directiry="/api/v1/graph/endpoint_counter?eid={0}".format(point_id) params = { 'url': domain + directiry, 'headers': falcon_header, 'timeout': 30 } res2 = requests.get(**params) data2 = json.loads(res2.text) print('得到具体监控项',data2) #========================================================================= counters = [ counter["counter"] for counter in data2 ] end_time = int(time.time()) # 必须要整形 start_time = end_time - 1800 # 30分钟 directiry="/api/v1/graph/history" params = { 'url': domain + directiry, 'headers': falcon_header, 'timeout': 30 } payload = { "step": 60, "start_time": start_time, "hostnames": [in_ip, ], "end_time": end_time, "counters": counters, "consol_fun": "AVERAGE" } params['data'] = json.dumps(payload) res3 = requests.post(**params) data3 = json.loads(res3.text) # print('得到指定监控项的历史记录',data3) data = dict([(iter["counter"], iter["Values"]) for iter in data3]) #===============格式化数据========================================== for key in data: values = data[key] data[key] = [{"timestamp": time.strftime('%H:%M', time.localtime(k["timestamp"])), "value": k["value"]} for k in values if k["value"]] data["in_ip"] = in_ip print('得到指定监控项的历史记录',data)
文章来源: open-falcon api相关