prometheus http api 獲取數(shù)據(jù) 參考官方地址
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個參數(shù);example: python3 status-prometheus.py 10.76.211.21 logs 1\n \
第1個參數(shù):哪個kafka集群ip)\n \
第2個參數(shù):需要統(tǒng)計的kafka topic名稱\n \
第3個參數(shù):統(tǒng)計當(dāng)前時間前幾天的數(shù)據(jù)\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]))