python3调用 prometheus API

import os, json, requests, time


def get_disk_peak(ip):
    # 拼接URL
    pre_url = 'http://10.xx.5.3x:90x0' + '/api/v1/query?query='
    expr = '(node_filesystem_size_bytes{fstype!~"apfs",mountpoint="/",instancehost="ip17"} - node_filesystem_free_bytes{fstype!~"apfs",mountpoint="/",instancehost="ip17"}) / node_filesystem_size_bytes{fstype!~"apfs",mountpoint="/",instancehost="ip17"} * 100'.replace('ip17', ip)
    url = pre_url + expr
    result = {}
    # 请求URL后将Json数据转为字典对象
    res = json.loads(requests.post(url=url).content.decode('utf8', 'ignore'))
    print("###json:", res)
    # 循环取出字典里每个IP的values,排序取最高值,最后存入result字典
    result_list = res.get('data').get('result')
    print(result_list)

    result_dict = result_list[0]
    result_value = result_dict['value']
    return result_value[1]


def get_rule_recovery(ip, recovery_expr):
    # 拼接URL
    pre_url = 'http://10.xx.5.3x:9xx0' + '/api/v1/query?query='
    expr = recovery_expr.replace('ip17', ip)
    url = pre_url + expr
    # 请求URL后将Json数据转为字典对象
    res = json.loads(requests.post(url=url).content.decode('utf8', 'ignore'))
    print("###json:", res)

    result_list = res.get('data').get('result')
    print(result_list)

    result_dict = result_list[0]
    result_value = result_dict['value']
    return result_value[1]


ww = '(node_filesystem_size_bytes{fstype!~"apfs",mountpoint="/",instancehost="ip17"} - node_filesystem_free_bytes{fstype!~"apfs",mountpoint="/",instancehost="ip17"}) / node_filesystem_size_bytes{fstype!~"apfs",mountpoint="/",instancehost="ip17"} * 100'
print(get_rule_recovery(ip='10.31.140.24', recovery_expr=ww))

python3调用 prometheus API_第1张图片

你可能感兴趣的:(python3调用 prometheus API)