-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathscrape_prometheus.py
128 lines (113 loc) · 5.23 KB
/
scrape_prometheus.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import urllib.request
import json
import time
def load_prometheus_query(query):
req = urllib.request.Request(url=query)
with urllib.request.urlopen(req) as f:
return json.loads(f.read().decode('utf-8'))
def clean_trailing_slash(url):
if url[-1] == '/':
return url[:-1]
return url
while True:
time.sleep(5)
instances = {}
credits = {
'INSTITUTION': set(),
'COMPANY': set(),
'PERSON': set(),
'ASSOCIATION': set(),
}
jitsi_required_labels = [
'instance',
'jitsi_hosted_by',
'jitsi_hosted_by_url',
'jitsi_url',
'jitsi_hosted_by_kind',
'software',
'available_bandwidth_mbps',
'core_count',
]
mm_required_labels = [
'instance',
'url',
'hosted_by',
'hosted_by_url',
'hosted_by_kind',
'available_bandwidth_mbps',
'core_count',
'software',
]
participants_data = load_prometheus_query('http://prometheus:9090/api/v1/query?query=jitsi_participants')
cpu_data = load_prometheus_query('http://prometheus:9090/api/v1/query?query=jitsi_cpu_usage')
static_mm_data = load_prometheus_query('http://prometheus:9090/api/v1/query?query=probe_success{software="MM"}')
mm_data = load_prometheus_query('http://prometheus:9090/api/v1/query?query=edumeet_cpu_usage')
mm_peers_data = load_prometheus_query('http://prometheus:9090/api/v1/query?query=edumeet_peers')
for server in participants_data['data']['result']:
if not all(key in server['metric'] for key in jitsi_required_labels):
continue
if server['metric']['software'] == 'JITSI':
d = {}
d['name'] = clean_trailing_slash(server['metric']['jitsi_url'].replace('https://', ''))
d['user_count'] = int(server['value'][1])
d['by'] = server['metric']['jitsi_hosted_by']
d['by_url'] = clean_trailing_slash(server['metric']['jitsi_hosted_by_url'])
d['url'] = clean_trailing_slash(server['metric']['jitsi_url'])
d['by_kind'] = server['metric']['jitsi_hosted_by_kind']
d['software'] = server['metric']['software']
d['available_bandwidth_mbps'] = server['metric']['available_bandwidth_mbps']
d['core_count'] = server['metric']['core_count']
credits[d['by_kind']].add((d['by'], d['by_url']))
instances[d['name']] = d
for server in cpu_data['data']['result']:
if not all(key in server['metric'] for key in jitsi_required_labels):
continue
if server['metric']['software'] == 'JITSI':
name = clean_trailing_slash(server['metric']['jitsi_url'].replace('https://', ''))
instances[name]['cpu_usage'] = round(float(server['value'][1]), ndigits=2)
for server in static_mm_data['data']['result']:
if not all(key in server['metric'] for key in mm_required_labels):
continue
if server['metric'].get('software') == 'MM' and server['value'][1] == '1':
d = {}
d['name'] = clean_trailing_slash(server['metric']['jitsi_url'].replace('https://', ''))
d['url'] = clean_trailing_slash(server['metric']['url'])
d['by'] = server['metric']['hosted_by']
d['by_url'] = clean_trailing_slash(server['metric']['hosted_by_url'])
d['by_kind'] = server['metric']['hosted_by_kind']
d['software'] = server['metric']['software']
d['available_bandwidth_mbps'] = server['metric']['available_bandwidth_mbps']
d['core_count'] = server['metric']['core_count']
credits[d['by_kind']].add((d['by'], d['by_url']))
instances[d['name']] = d
for server in mm_data['data']['result']:
if not all(key in server['metric'] for key in mm_required_labels):
continue
if server['metric'].get('software') == 'MM':
d = {}
d['name'] = clean_trailing_slash(server['metric']['url'].replace('https://', ''))
d['url'] = clean_trailing_slash(server['metric']['url'])
d['by'] = server['metric']['hosted_by']
d['by_url'] = clean_trailing_slash(server['metric']['hosted_by_url'])
d['by_kind'] = server['metric']['hosted_by_kind']
d['software'] = server['metric']['software']
d['available_bandwidth_mbps'] = server['metric']['available_bandwidth_mbps']
d['core_count'] = server['metric']['core_count']
d['cpu_usage'] = round(float(server['value'][1]), ndigits=2)
credits[d['by_kind']].add((d['by'], d['by_url']))
instances[d['name']] = d
for server in mm_peers_data['data']['result']:
if not all(key in server['metric'] for key in mm_required_labels):
continue
if server['metric']['software'] == 'MM':
name = clean_trailing_slash(server['metric']['url'].replace('https://', ''))
instances[name]['user_count'] = int(server['value'][1])
new_credits = {}
for key, item in credits.items():
new_credits[key] = list(item)
result = {
'instances': list(instances.values()),
'credits': new_credits,
}
with open('/hosts.json', 'w') as f:
f.write(json.dumps(result, indent=2))