open-falcon api相關

本文描述經過被監控endpoint的名稱獲取該endpoint的eid和監控項,從而獲取到該endpoint的監控歷史數據,使用python代碼的 api操做方法python

注:同步open-falcon和agent的時間,否則獲取不到數據數據庫

http://open-falcon.org/falcon-plus/#/endpointsjson

訪問的api有:api

/api/v1/graph/endpoint?q={0}
/api/v1/graph/endpoint_counter?eid={0}
/api/v1/graph/history

 

具體操做:app

# -*- 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)
相關文章
相關標籤/搜索