【发布时间】:2020-02-04 04:17:17
【问题描述】:
我正在尝试读取 kubernetes 中 POD 的 Prometheus 指标(cpu 和内存值)。我安装了 Prometheus,一切都使用本地主机'http://localhost:9090/。我使用以下代码读取 pod 的 CPU 和内存,但出现错误 results = response.json()['data']['result'] , No JSON object could be decoded 。有人可以帮忙吗?
import datetime
import time
import requests
PROMETHEUS = 'http://localhost:9090/'
end_of_month = datetime.datetime.today().replace(day=1).date()
last_day = end_of_month - datetime.timedelta(days=1)
duration = '[' + str(last_day.day) + 'd]'
response = requests.get(PROMETHEUS + '/metrics',
params={
'query': 'sum by (job)(increase(process_cpu_seconds_total' + duration + '))',
'time': time.mktime(end_of_month.timetuple())})
results = response.json()['data']['result']
print('{:%B %Y}:'.format(last_day))
for result in results:
print(' {metric}: {value[1]}'.format(**result))
【问题讨论】:
标签: python kubernetes prometheus