forked from ruanbekker/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushgateway_python.py
More file actions
27 lines (21 loc) · 1.04 KB
/
pushgateway_python.py
File metadata and controls
27 lines (21 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/usr/bin/env python3
import random
import requests
import json
pushgateway_endpoint="http://127.0.0.1:9091"
pushgateway_username=""
pushgateway_password=""
def get_metrics():
cpu_value = random.randint(10,20)
return cpu_value
def post_metric_to_pushgateway(instance_name, metric_category, metric_name, metric_value):
request_url = '{endpoint}/metrics/job/pythontest/server/{server}/category/{category}'.format(endpoint=pushgateway_endpoint, server=instance_name, category=metric_category)
response = requests.post(request_url, data='{_n} {_v}\n'.format(_n=metric_name, _v=metric_value), auth=(pushgateway_username, pushgateway_password))
return response.status_code
# get metric values
cpu_value = get_metrics()
# emit balance metric to pushgateway and let prometheus scrape pushgateway to ingest into prometheus tsdb
server1_status = post_metric_to_pushgateway('server1', 'node-metrics', 'cpu_percentage', cpu_value)
# return response to stdout
payload = {"server1": server1_status}
print(json.dumps(payload, indent=2, default=str))