小编101*_*101的帖子

使用 python 读取 Prometheus 指标

我正在尝试读取 kubernetes 中 POD 的 Prometheus 指标(CPU 和内存值)。我安装了 Prometheus,并且使用本地主机“ http://localhost:9090/”一切正常。我使用以下代码读取 Pod 的 CPU 和内存,但出现错误 results = response.json()['data']['result'] ,无法解码 JSON 对象。有人可以帮忙吗?

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))
Run Code Online (Sandbox Code Playgroud)

python kubernetes prometheus

5
推荐指数
1
解决办法
3万
查看次数

标签 统计

kubernetes ×1

prometheus ×1

python ×1