• 从 falcon api 中获取数据


    import json
    import time
    
    import requests
    
    openfalcon = 'http://ip:port/api/v1'
    user = 'user'
    password = 'password'
    
    
    def get_sig(user=user, password=password):
        url = '%s/user/login' % openfalcon
        response = requests.post(url, data=dict(name=user, password=password), timeout=2)
        return response.json()
    
    
    def get_graph_history(hostname, category, start_time, end_time):
        url = '%s/graph/history' % openfalcon
        sig = get_sig()
        headers = {
            'Apitoken': json.dumps({'name': sig['name'], 'sig': sig['sig']}),
            'Content-type': 'application/json',
            'X-Forwarded-For': '127.0.0.1',
        }
    
        counters = [
            "cpu.idle",
            "cpu.iowait",
            "cpu.user",
            "cpu.system",
            "cpu.iowait",
            "cpu.irq",
            "cpu.softirq",
            "mem.memfree.percent",
            "mem.swapfree.percent",
            "df.bytes.free.percent/fstype=ext4,mount=/",
            "df.bytes.free.percent/fstype=ext4,mount=/data",
            "net.if.in.bytes/iface=eth0",
            "net.if.out.bytes/iface=eth0",
            "net.if.in.packets/iface=eth0",
            "net.if.out.packets/iface=eth0",
            "net.if.in.dropped/iface=eth0",
            "net.if.out.dropped/iface=eth0",
            "net.if.in.errors/iface=eth0",
            "net.if.out.errors/iface=eth0",
            "ss.estab",
            "load.1min",
            "load.5min",
            "load.15min",
            "disk.io.read_bytes/device=vda",
            "disk.io.read_bytes/device=vdb",
            "disk.io.write_bytes/device=vda",
            "disk.io.write_bytes/device=vdb",
            "disk.io.msec_total/device=vda",
            "disk.io.msec_total/device=vdb",
            "disk.io.avgrq_sz/device=vdb",
            "disk.io.avgqu-sz/device=vdb",
            "disk.io.await/device=vdb",
            "disk.io.svctm/device=vdb",
            "disk.io.util/device=vdb",
        ]
    
        if 'cpu' == category:
            counters = counters[0:7]
        elif 'memory' == category:
            counters = counters[7:9]
        elif 'disk' == category:
            counters = counters[9:11]
        elif 'net' == category:
            counters = counters[11:20]
        elif 'io' == category:
            counters = counters[20:23]
        else:
            counters = counters[23:]
    
        data = {
            "step": 60,
            "start_time": start_time,
            "hostnames": [hostname],
            "end_time": end_time,
            "counters": counters,
            "consol_fun": "AVERAGE"
        }
        response = requests.post(url, headers=headers, data=json.dumps(data))
        return response.json()
    
    
    if __name__ == '__main__':
        end_time = int(time.time())
        start_time = end_time - 300
        print get_graph_history('host-001', 'io', start_time, end_time)

  • 相关阅读:
    Unix IPC之共享内存区(1)
    linux下的二进制文件的编辑和查看
    Posix 信号量
    Unix IPC之Posix信号量实现生产者消费者
    整型信号量与记录型信号量
    C++之友元
    C++之异常处理
    C++之STL(标准模板库)
    C++之继承
    C++之封装
  • 原文地址:https://www.cnblogs.com/txwsqk/p/9560061.html
Copyright © 2020-2023  润新知