zoukankan      html  css  js  c++  java
  • 从 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)

  • 相关阅读:
    CentOS6.5配置MySQL主从同步
    CentOS6.5安装telnet
    linux 下安装Google Chrome (ubuntu 12.04)
    jdk w7环境变量配置
    JDBCConnectionException: could not execute query,数据库连接池问题
    注意开发软件的版本问题!
    linux mysql命令行导入导出.sql文件 (ubuntu 12.04)
    linux 下root用户和user用户的相互切换 (ubuntu 12.04)
    linux 下 vim 的使用 (ubuntu 12.04)
    linux 下安装配置tomcat-7 (ubuntu 12.04)
  • 原文地址:https://www.cnblogs.com/txwsqk/p/9560061.html
Copyright © 2011-2022 走看看