-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathweblate
executable file
·99 lines (88 loc) · 2.88 KB
/
weblate
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
#!/usr/bin/env python3
# fmt: off
#%# family=auto
#%# capabilities=autoconf
# fmt: on
import json
import os
import sys
import time
import urllib.request
def fetch_data(server, token):
req = urllib.request.Request(f"{server}api/metrics/")
req.add_header("Authorization", f"Token {token}")
with urllib.request.urlopen(req) as response:
content = response.read().decode("utf-8")
return json.loads(content)
NAME = os.path.basename(sys.argv[0])
try:
CMD = sys.argv[1]
except IndexError:
CMD = "fetch"
ATTRIBS = {
"celery_search": {"warning": ":10000", "critical": ":20000"},
"celery_backup": {"warning": ":10", "critical": ":100"},
"celery_celery": {"warning": ":200", "critical": ":1000"},
"celery_notify": {"warning": ":1000", "critical": ":2000"},
"celery_memory": {"warning": ":10000", "critical": ":20000"},
"celery_translate": {"warning": ":200", "critical": ":1000"},
"configuration_errors": {"critical": ":0"},
"changes": {"type": "DERIVE"},
"response": {"warning": ":2", "critical": ":10"},
}
GROUPS = (
("Units", ("units", "units_translated", "checks", "suggestions")),
("Projects", ("projects", "components", "languages")),
("Translations", ("translations",)),
("Changes", ("changes",)),
("Errors", ("configuration_errors",)),
("Users", ("users",)),
("Celery", "CELERY"),
("Response", ("response",)),
)
server = os.environ["SERVER"]
key = os.environ["KEY"]
start = time.time()
data = fetch_data(server, key)
response_time = time.time() - start
if CMD == "config":
for group, items in GROUPS:
print()
print(f"multigraph {NAME.lower()}_{group.lower()}")
print()
if NAME == "weblate":
title = f"Weblate {group}"
else:
title = "{} {}".format(data["name"], group)
print(f"graph_title {title}")
print("graph_args --base 1000")
print("graph_scale no")
print("graph_category weblate")
print()
if items == "CELERY":
items = [f"celery_{queue}" for queue in sorted(data["celery_queues"])]
for key in items:
print(f"{key}.label {key}")
if key in ATTRIBS:
for attrib, value in ATTRIBS[key].items():
print(f"{key}.{attrib} {value}")
elif CMD == "autoconf":
print("yes")
else:
for group, items in GROUPS:
print()
print(f"multigraph {NAME.lower()}_{group.lower()}")
print()
prefix = ""
if items == ("response",):
print(f"response.value {response_time}")
continue
if items == "CELERY":
itemdata = data["celery_queues"]
items = sorted(data["celery_queues"])
prefix = "celery_"
else:
itemdata = data
for key in items:
if key in itemdata:
print(f"{prefix}{key}.value {itemdata[key]}")