/
exporter.py
executable file
·99 lines (73 loc) · 3.35 KB
/
exporter.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/bin/python
from prometheus_client import Gauge
from prometheus_client import start_http_server
import threading
import json
import requests
import time
import sys
import os
from alaudacli import service
from alaudacli import commands
def alauda_login(username, password, cloud='cn', endpoint='https://api.alauda.cn/v1/'):
commands.login(username, password, cloud, endpoint)
def alauda_service_list(namespace):
service_list = service.Service.list(namespace, 1)
return service_list
def alauda_instance_list(namespace, name):
service_inst = service.Service.fetch(name, namespace)
instances = service_inst.list_instances()
instance_list = []
for data in instances:
instance = json.loads(data.details)
instance_list.append(instance)
return instance_list
def alauda_get_instance_metrics(namespace, name, instance_uuid, start_time, end_time, interval):
service_inst = service.Service.fetch(name, namespace)
url = service_inst.api_endpoint + 'services/{0}/{1}/instances/{2}/metrics?start_time={3}&end_time={4}&point_per_period={5}'.format(service_inst.namespace, service_inst.name, instance_uuid, start_time, end_time, interval)
r = requests.get(url, headers=service_inst.headers)
print "Response:" + r.text + str(r.status_code)
if r.text:
data = json.loads(r.text)
return data
else:
return None
def gather_data(namespace, run_event):
g_cpu_usage = Gauge("cpu_cumulative_usage", "CPU Cumulative Usage", ["service", "instance"])
g_cpu_utilization = Gauge('cpu_utilization', "CPU utilization", ["service", "instance"])
g_memory_usage = Gauge('memory_usage', "Memory Usage", ["servie", "instance"])
g_memory_utilization = Gauge('memory_utilization', "Memory Utilization", ["service", "instance"])
while run_event.is_set():
service_list = alauda_service_list(namespace)
for service_inst in service_list:
service_name = service_inst.name
instance_list = alauda_instance_list(namespace, service_name)
for instance in instance_list:
end_time = int(time.time()) - 30
start_time = str(end_time - 100) #gather data every 1 minute, ensure we can get at least one metric
end_time = str(end_time)
data = alauda_get_instance_metrics(namespace, service_name, instance['uuid'], start_time, end_time, "1m")
if data:
g_cpu_usage.labels(service_name, instance['instance_name']).set(data['points'][0][1])
g_cpu_utilization.labels(service_name, instance['instance_name']).set(data['points'][0][2])
g_memory_usage.labels(service_name, instance['instance_name']).set(data['points'][0][3])
g_memory_utilization.labels(service_name, instance['instance_name']).set(data['points'][0][4])
time.sleep(20)
if __name__ == "__main__":
username = os.environ.get('ALAUDA_USERNAME')
password = os.environ.get('ALAUDA_PASSWORD')
alauda_login(username, password)
run_event = threading.Event()
run_event.set()
thread = threading.Thread(target=gather_data, args=('darkheaven', run_event))
thread.start()
try:
start_http_server(9104)
while True:
time.sleep(10)
except KeyboardInterrupt:
run_event.clear()
thread.join()
sys.exit(0)
except:
sys.exit(1)