]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blame - modules/role/templates/cryptoportfolio/api_logger.py.erb
Change release url
[perso/Immae/Projets/Puppet.git] / modules / role / templates / cryptoportfolio / api_logger.py.erb
CommitLineData
6b95d3bd
IB
1#!/usr/bin/python
2
3import time
4import urllib3
5import json
6from systemd import journal
7
8urllib3.disable_warnings()
9http = urllib3.PoolManager()
10
11webhook_url = "<%= @slack_logger %>"
123ee097 12webhook_username = "<%= @slack_logger_username %>"
b07fbcf4
IB
13
14# These icons are made by CSSCREME.COM
15# http://csscreme.com/freeicons/
16# License: GNU (GENERAL PUBLIC LICENSE)
19c467dc 17webhook_icon = "https://release.immae.eu/logger.png"
6b95d3bd 18
b07fbcf4
IB
19priorities = {
20 0: "0", # black
21 1: "6684672", # dark dark red
22 2: "13369344", # dark red
23 3: "16711680", # red
24 4: "16737792", # orange
25 5: "3407616", # green
26 6: "255", # blue
27 7: "16777215", # white
28 }
29
30def send_to_discord(messages):
31 def send_chunk(chunk, priority, fields=None):
6b95d3bd
IB
32 data = {
33 "avatar_url": webhook_icon,
123ee097 34 "username": webhook_username,
b07fbcf4
IB
35 "embeds": [
36 {
37 "description": "```\n{}\n```".format(chunk[0:2000]),
38 "color": priorities[priority],
39 "fields": [],
40 }
41 ],
6b95d3bd 42 }
b07fbcf4
IB
43 if fields is not None:
44 data["content"] = "@everyone"
45
46 for field in ["CLIENT_IP", "PATH", "METHOD", "STATUS_CODE", "USER_ID", "FLAG", "LATENCY"]:
47 value = fields.get(field)
48 if value is not None:
49 data["embeds"][-1]["fields"].append({
50 "name": field.replace("_", " ").capitalize(),
51 "value": value,
52 "inline": True,
53 })
54
6b95d3bd
IB
55 encoded = json.dumps(data).encode('utf-8')
56
57 r = http.request("POST", webhook_url,
58 headers={'Content-Type': 'application/json'},
59 body=encoded)
60 if r.status == 429:
61 b = json.loads(r.data)
62 time.sleep(b["retry_after"] / 900)
63 r = http.request("POST", webhook_url,
64 headers={'Content-Type': 'application/json'},
65 body=encoded)
b07fbcf4
IB
66 if r.status >= 300:
67 print("Error when sending message: {}".format(r.status))
6b95d3bd
IB
68
69 chunk = ""
b07fbcf4
IB
70 priority = ""
71 for message in messages:
72 m_priority = int(message.get("PRIORITY", 5))
73 if m_priority > 6:
74 continue
75
76 if len(chunk) > 0 and m_priority != priority:
77 send_chunk(chunk, priority)
78 chunk = ""
79
80 if m_priority <= 4:
81 send_chunk(message["MESSAGE"], m_priority, fields=message)
6b95d3bd 82 else:
b07fbcf4
IB
83 priority = m_priority
84
85 if "SYSLOG_IDENTIFIER" in message:
86 line = "[{}] {}".format(message["SYSLOG_IDENTIFIER"], message["MESSAGE"])
87 else:
88 line = message["MESSAGE"]
89 if len(line) + len(chunk) > 2000 - 17:
90 send_chunk(chunk, priority)
91 chunk = line
92 else:
93 chunk += "\n" + line
94 if len(chunk) > 0:
95 send_chunk(chunk, priority)
6b95d3bd
IB
96
97j = journal.Reader()
98j.add_match(_SYSTEMD_UNIT="cryptoportfolio-app.service")
b07fbcf4
IB
99j.add_disjunction()
100j.add_match(UNIT="cryptoportfolio-app.service")
6b95d3bd
IB
101j.seek_tail()
102list(j)
103
104while True:
b07fbcf4 105 lines = list(j)
6b95d3bd 106 if len(lines) > 0:
b07fbcf4 107 send_to_discord(lines)
b9fb13d2 108 time.sleep(5)
a859ab30 109 j.wait()