# -*- coding:utf-8 -*- # import requests import time ''' 采样范围太大就会提示 {"status":"error","errorType":"execution", "error":"query processing would load too many samples into memory in query execution"} ''' check_address = { "test1":"http://192.168.1.1:9090/", "test2":"http://192.168.1.2:9090/", "test3":"http://192.168.1.3:9090/" } check_target= {"tikv_disk_used":"sum(tikv_engine_size_bytes{} ) by (instance) /1000/1000/1000", "tikv_mem":"process_resident_memory_bytes{job='tikv'}/1024/1024/1024", "region_nums":"pd_cluster_status{type='region_count'}", "tikv_cpu_used":"sum(rate(tikv_thread_cpu_seconds_total{job='tikv'}[30m])) by (instance) * 100"} for project,address in check_address.items(): print("---------------项目{}---------------".format(project)) for key,expr in check_target.items(): print("[{}]".format(key)) url = address + '/api/v1/query?query=' + expr response = requests.get(url,params={'query':'expr'}) try: for i in response.json()['data']['result']: check_time = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(i['value'][0])) print("实例{} 采集时间{} 当前数值{}".format(i['metric']['instance'],check_time,i['value'][1])) except Exception as e: print('Error:', e) continue
使用http://192.168.68.63:9090 进行测试
[tikv_disk_used] 实例192.168.68.63:20181 采集时间2021-01-08 17:47:11 当前数值59.627338347000006 实例192.168.68.63:20182 采集时间2021-01-08 17:47:11 当前数值59.726639739999996 实例192.168.68.63:20183 采集时间2021-01-08 17:47:11 当前数值59.644997088 [tikv_mem] 实例192.168.68.63:20181 采集时间2021-01-08 17:47:11 当前数值9.317237854003906 实例192.168.68.63:20182 采集时间2021-01-08 17:47:11 当前数值8.64874267578125 实例192.168.68.63:20183 采集时间2021-01-08 17:47:11 当前数值7.052970886230469 [region_nums] 实例192.168.68.60:2379 采集时间2021-01-08 17:47:11 当前数值43290 [tikv_cpu_used] 实例192.168.68.63:20181 采集时间2021-01-08 17:47:11 当前数值6.6705882352943116 实例192.168.68.63:20182 采集时间2021-01-08 17:47:11 当前数值7.445378151253479 实例192.168.68.63:20183 采集时间2021-01-08 17:47:11 当前数值6.232496488793929