关于prometheus:python3调用-prometheus-API

43次阅读

共计 1395 个字符,预计需要花费 4 分钟才能阅读完成。

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))

正文完
 0