使用python读取普罗米修斯公制 [英] Reading Prometheus metric using python

查看:16
本文介绍了使用python读取普罗米修斯公制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取Kubernetes中POD的普罗米修斯指标(CPU和内存值)。我已经安装了Prometheus,并且使用本地主机‘http://localhost:9090/一切正常。我使用以下代码来读取Pod的CPU和内存,但出现错误Results=Response.json()[‘data’][‘Result’],No 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))

推荐答案

代码看起来是真的,但是,您的响应命令中的查询是错误的。真正的格式是:

response =requests.get(PROMETHEUS + '/api/v1/query', params={'query': 'container_cpu_user_seconds_total'}) 

您可以将"CONTAINER_CPU_USER_SECONDS_TOTAL"更改为您想要读取的任何查询。 。。 祝你好运

这篇关于使用python读取普罗米修斯公制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆