-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserverboards-prometheus.py
executable file
·331 lines (276 loc) · 9.31 KB
/
serverboards-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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!env/bin/python3
import serverboards_aio as serverboards
import sys
import asks
import time
import json
import urllib
import curio
from serverboards_aio import print
from pcolor import printc
asks.init('curio')
IGNORE_METRIC_NAMES = set(['instance', 'job'])
td_to_s_multiplier = [
("ms", 0.001),
("s", 1),
("m", 60),
("h", 60 * 60),
("d", 24 * 60 * 60),
]
uuid_to_timer = {}
def time_description_to_seconds(td):
if type(td) in (int, float):
return float(td)
for sufix, multiplier in td_to_s_multiplier:
if td.endswith(sufix):
return float(td[:-len(sufix)]) * multiplier
return float(td)
def decorate_serie(serie, name=None):
"""
Returns the series decorated as Serverboards likes it, not as Prometheus
returns it.
"""
metric = serie.get("metric", {})
name = name or metric.get("__name__", None)
if not name:
name = ', '.join("%s: %s" % (k, v)
for k, v in metric.items()
if k not in IGNORE_METRIC_NAMES)
return {
"name": name,
"values": serie.get("values", [])
}
ssh = serverboards.Plugin("serverboards.core.ssh/daemon")
@serverboards.cache_ttl(3000)
async def port_tunnel(via, hostname, port):
newport = await ssh.open_port(service=via, hostname=hostname, port=port)
await serverboards.debug("Opened new port: %s" % newport)
return newport
@serverboards.cache_ttl(300)
async def service_get(service_id):
if not service_id:
return {"config": {}}
return await serverboards.service.get(service_id)
@serverboards.rpc_method
async def get(expression, service=None, start=None, end=None, step=None):
if not expression:
raise Exception("An expression is required")
service = await service_get(service)
url = service.get("config", {}).get("url", "http://localhost:9090")
via = service.get("config", {}).get("via")
if via:
url = urllib.parse.urlparse(url)
port = await port_tunnel(via, url.hostname, url.port)
url = "http://localhost:%d" % port
now = int(time.time())
if not start:
start = now - 600
if not end:
end = now
if not step:
step = 14
ret = []
# maybe several expresions, one per line
for expr in expression.split('\n'):
expr = expr.strip()
name = None
if ':' in expr:
d = expr.split(':')
name = d[0]
expr = d[1].strip()
if not expr:
continue
params = {
"query": expr,
"start": start,
"end": end,
"step": step,
"_": now
}
# serverboards.debug("Get data from %s, %s: %s"%(url,repr(via), expr))
try:
res = await asks.get(url + "/api/v1/query_range", params=params)
except Exception:
raise Exception(
"Coult not connect to the Prometheus server. Is it running?")
if res.status_code != 200:
raise Exception(res.text)
js = res.json()
if js.get("status") != "success":
raise Exception("Unknown response from prometheus")
for x in js.get("data", {}).get("result", []):
ret.append(decorate_serie(x, name=name))
return ret
async def get_points(via=None, url=None, expression=None):
if not url:
url = "http://localhost:9090"
if via:
url = urllib.parse.urlparse(url)
port = await port_tunnel(via, url.hostname, url.port)
url = "http://localhost:%d" % port
now = int(time.time())
params = {
"query": expression,
"time": now,
"_": now
}
printc(url + "/api/v1/query", params)
res = await asks.get(url + "/api/v1/query", params=params)
return res.json()["data"]["result"]
watch_tasks = {}
@serverboards.rpc_method
async def watch_start(id=None, period=None, service=None, expression=None, **kwargs):
state = None
via = service.get("config", {}).get("via")
url = service.get("config", {}).get("url")
period_s = time_description_to_seconds(period or "5m")
nstate = None
async def check_ok():
while True:
await serverboards.debug("Checking expression: %s" % (expression))
p = await get_points(via=via, url=url, expression=expression)
if state != nstate:
await serverboards.rpc.event("trigger", {"id": id, "value": p})
await curio.sleep(period_s)
await serverboards.info("Start Prometheus watch %s" % timer_id)
watch_tasks[id] = curio.spawn(check_ok)
return id
@serverboards.rpc_method
async def watch_stop(id):
await serverboards.info("Stop Prometheus watch %s" % (id))
await watch_tasks[id].cancel()
watch_tasks[id].join()
del watch_tasks[id]
return "ok"
@serverboards.cache_ttl(30)
async def get_values(via=None, url=None):
if not url:
url = "http://localhost:9090"
if via:
url = urllib.parse.urlparse(url)
port = await port_tunnel(via, url.hostname, url.port)
url = "http://localhost:%d" % port
res = await asks.get(url + "/api/v1/label/__name__/values")
return res.json()["data"]
@serverboards.cache_ttl(30)
async def get_tags(via=None, url=None, value="", tag=None):
if not url:
url = "http://localhost:9090"
if via:
url = urllib.parse.urlparse(url)
port = await port_tunnel(via, url.hostname, url.port)
url = "http://localhost:%d" % port
res = await asks.get(url + "/api/v1/series?match[]=%s" % value)
data = res.json()["data"]
print("value", value, data)
if not tag:
ret = set()
for d in data:
ret.update(d.keys())
return sorted([x for x in ret if not x.startswith('__')])
else:
ret = set()
for d in data:
ret.add(d.get(tag))
return [x for x in ret if x and not x.startswith('__')]
BUILTINS = ["sum(", "min(", "max(", "avg(", "stddev(", "stdvar(",
"count(", "count_values(", "bottomk(", "topk(", "quantile("]
@serverboards.rpc_method
async def autocomplete_values(current="", via=None, url=None, **kwargs):
if not current:
return []
if '=' in current:
prefix, suffix = current.split('{')
tag, suffix = suffix.split('=')
if suffix.startswith('"'):
suffix = suffix[1:]
options = ['%s{%s="%s"}' % (prefix, tag, x)
for x in (await get_tags(via, url, prefix, tag))
if x.startswith(suffix)
]
elif '{' in current:
prefix, suffix = current.split('{')
options = ['%s{%s="' % (prefix, x)
for x in (await get_tags(via, url, prefix))
if x.startswith(suffix)
]
else:
options = (await get_values(via, url)) + BUILTINS
for cpart in \
current.lower().replace('{', '_').replace('=', '_').split('_'):
options = [x for x in options if cpart in x.lower()]
return sorted(options)
async def connect_url_via_status(url, via):
# print("Check ", url, via)
if via:
url = urllib.parse.urlparse(url)
print("Tunel to", url.hostname, url.port)
try:
port = await port_tunnel(via, url.hostname, url.port)
except Exception:
return "ssh-proxy-error"
url = "http://localhost:%d" % port
# print("Check url", url)
try:
res = await asks.get(url)
except Exception as e:
return "down"
if res.status_code == 200:
return "ok"
else:
return "nok"
@serverboards.rpc_method
async def prometheus_is_up(service):
return await connect_url_via_status(
url=service["config"].get("url") or "http://localhost:9090",
via=service["config"]["via"])
@serverboards.rpc_method
async def agent_is_up(service):
return await connect_url_via_status(
url=service["config"].get("url") or "http://localhost:9090",
via=service["config"]["via"])
@serverboards.rpc_method
async def node_exporter_is_up(service):
return await connect_url_via_status(
url=service["config"].get("url") or "http://localhost:9100",
via=service["config"]["server"])
async def test():
# res=get(expression="prometheus_rule_evaluation_failures_total")
# print(json.dumps(res, indent=2))
printc("START")
try:
res = await autocomplete_values("up")
printc(json.dumps(res, indent=2))
res = await get_points(expression="up == 1")
assert res
res = await get(service="XXX", expression="up")
assert res
res = await node_exporter_is_up({
"config": {
"url": None,
"server": None,
}
})
printc(res)
assert res == "ok"
res = await node_exporter_is_up({
"config": {
"url": "https://127.255.255.255:1000/",
"server": None,
}
})
printc(res)
assert res == "down"
except Exception as e:
import traceback
traceback.print_exc()
sys.exit(1)
printc("Success")
if __name__ == '__main__':
if len(sys.argv) == 2 and sys.argv[1] == 'test':
import yaml
serverboards.test_mode(test, yaml.load(open("mock.yaml")))
print("Failed!")
sys.exit(1)
else:
serverboards.loop()