python3调用|python3调用 prometheus API

import os, json, requests, timedef 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 = https://www.it610.com/article/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 = https://www.it610.com/article/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调用|python3调用 prometheus API】python3调用|python3调用 prometheus API
文章图片

    推荐阅读