]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/blob - modules/role/templates/cryptoportfolio/api_logger.py.erb
Change release url
[perso/Immae/Projets/Puppet.git] / modules / role / templates / cryptoportfolio / api_logger.py.erb
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_username = "<%= @slack_logger_username %>"
13
14 # These icons are made by CSSCREME.COM
15 # http://csscreme.com/freeicons/
16 # License: GNU (GENERAL PUBLIC LICENSE)
17 webhook_icon = "https://release.immae.eu/logger.png"
18
19 priorities = {
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
30 def send_to_discord(messages):
31 def send_chunk(chunk, priority, fields=None):
32 data = {
33 "avatar_url": webhook_icon,
34 "username": webhook_username,
35 "embeds": [
36 {
37 "description": "```\n{}\n```".format(chunk[0:2000]),
38 "color": priorities[priority],
39 "fields": [],
40 }
41 ],
42 }
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
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)
66 if r.status >= 300:
67 print("Error when sending message: {}".format(r.status))
68
69 chunk = ""
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)
82 else:
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)
96
97 j = journal.Reader()
98 j.add_match(_SYSTEMD_UNIT="cryptoportfolio-app.service")
99 j.add_disjunction()
100 j.add_match(UNIT="cryptoportfolio-app.service")
101 j.seek_tail()
102 list(j)
103
104 while True:
105 lines = list(j)
106 if len(lines) > 0:
107 send_to_discord(lines)
108 time.sleep(5)
109 j.wait()