]>
Commit | Line | Data |
---|---|---|
6b95d3bd IB |
1 | #!/usr/bin/python |
2 | ||
3 | import time | |
4 | import urllib3 | |
5 | import json | |
6 | from systemd import journal | |
7 | ||
8 | urllib3.disable_warnings() | |
9 | http = urllib3.PoolManager() | |
10 | ||
11 | webhook_url = "<%= @slack_logger %>" | |
12 | webhook_icon = "https://git.immae.eu/releases/logger.png" | |
13 | ||
14 | def send_to_discord(message): | |
15 | def send_chunk(chunk): | |
16 | data = { | |
17 | "avatar_url": webhook_icon, | |
18 | "username": "Logger", | |
19 | "content": "```\n{}\n```".format(chunk), | |
20 | } | |
21 | encoded = json.dumps(data).encode('utf-8') | |
22 | ||
23 | r = http.request("POST", webhook_url, | |
24 | headers={'Content-Type': 'application/json'}, | |
25 | body=encoded) | |
26 | if r.status == 429: | |
27 | b = json.loads(r.data) | |
28 | time.sleep(b["retry_after"] / 900) | |
29 | r = http.request("POST", webhook_url, | |
30 | headers={'Content-Type': 'application/json'}, | |
31 | body=encoded) | |
32 | assert r.status < 299 | |
33 | ||
34 | chunk = "" | |
35 | for line in message.split("\n"): | |
36 | if len(line) + len(chunk) > 2000 - 17: | |
37 | send_chunk(chunk) | |
38 | chunk = line | |
39 | else: | |
40 | chunk += "\n" + line | |
41 | send_chunk(chunk) | |
42 | ||
43 | j = journal.Reader() | |
44 | j.add_match(_SYSTEMD_UNIT="cryptoportfolio-app.service") | |
45 | j.seek_tail() | |
46 | list(j) | |
47 | ||
48 | while True: | |
49 | lines = [entry["MESSAGE"] for entry in list(j)] | |
50 | if len(lines) > 0: | |
51 | send_to_discord("\n".join(lines)) | |
52 | time.sleep(3) |