prometheus http api 获取数据

prometheus http api 获取数据 参考官方地址

1、curl方式

curl http://192.168.211.21:9990/api/v1/query?query=kafka_topic_partition_current_offset

curl http://192.168.211.21:9990/api/v1/query  -XPOST --header "Content-Type:application/x-www-form-urlencoded" -d 'query=kafka_topic_partition_current_offset{instance="192.168.211.21:9308"}'

2、python方式

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import sys
if len(sys.argv) != 4:
    quit('\n\
     脚本需要3个参数;example: python3 status-prometheus.py 10.76.211.21 logs 1\n \
    第1个参数:哪个kafka集群ip)\n \
    第2个参数:需要统计的kafka topic名称\n \
    第3个参数:统计当前时间前几天的数据\n'
)

ip = sys.argv[1]
topic = sys.argv[2]
days = sys.argv[3]

url = 'http://192.168.211.21:9990/api/v1/query'
headers = {
    'Content-Type':'application/x-www-form-urlencoded'
}
expr = 'sum(kafka_topic_partition_current_offset{instance="%s:9308",topic="%s"} - kafka_topic_partition_current_offset{instance="%s:9308",topic="%s"} offset %sd)' % (ip, topic, ip, topic, days)
#print(expr)
data = {
    'query': expr
}
r = requests.post(url=url, data=data, headers=headers)
#print(r.status_code)
#print(r.text)
res = r.json()
for da in res.get('data').get('result'):
    values = da.get('value')
    print(int(values[1]))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容