zoukankan      html  css  js  c++  java
  • open-falcon api相关

    本文描述通过被监控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)
  • 相关阅读:
    论频谱中负频率成分的物理意义(转载)
    VS2008的glaux库
    通过域名显示IP列表
    Shader errorX3205的解决
    Curl, Divergence, Circulation
    关于FIONREAD命令的作用
    Cairngorm的结构及开发使用(2)(转)
    结合Flex Builder和Flash CS4制作一个中国地图的应用(转)
    大型高并发高负载网站的系统架构(转)
    Cairngorm的结构及开发使用(4)(转)
  • 原文地址:https://www.cnblogs.com/dylan-wu/p/8056869.html
Copyright © 2011-2022 走看看