aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modules/role/templates/cryptoportfolio/api_logger.py.erb79
1 files changed, 67 insertions, 12 deletions
diff --git a/modules/role/templates/cryptoportfolio/api_logger.py.erb b/modules/role/templates/cryptoportfolio/api_logger.py.erb
index ad296e8..9fe564e 100644
--- a/modules/role/templates/cryptoportfolio/api_logger.py.erb
+++ b/modules/role/templates/cryptoportfolio/api_logger.py.erb
@@ -10,15 +10,48 @@ http = urllib3.PoolManager()
10 10
11webhook_url = "<%= @slack_logger %>" 11webhook_url = "<%= @slack_logger %>"
12webhook_username = "<%= @slack_logger_username %>" 12webhook_username = "<%= @slack_logger_username %>"
13
14# These icons are made by CSSCREME.COM
15# http://csscreme.com/freeicons/
16# License: GNU (GENERAL PUBLIC LICENSE)
13webhook_icon = "https://git.immae.eu/releases/logger.png" 17webhook_icon = "https://git.immae.eu/releases/logger.png"
14 18
15def send_to_discord(message): 19priorities = {
16 def send_chunk(chunk): 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):
17 data = { 32 data = {
18 "avatar_url": webhook_icon, 33 "avatar_url": webhook_icon,
19 "username": webhook_username, 34 "username": webhook_username,
20 "content": "```\n{}\n```".format(chunk), 35 "embeds": [
36 {
37 "description": "```\n{}\n```".format(chunk[0:2000]),
38 "color": priorities[priority],
39 "fields": [],
40 }
41 ],
21 } 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
22 encoded = json.dumps(data).encode('utf-8') 55 encoded = json.dumps(data).encode('utf-8')
23 56
24 r = http.request("POST", webhook_url, 57 r = http.request("POST", webhook_url,
@@ -30,24 +63,46 @@ def send_to_discord(message):
30 r = http.request("POST", webhook_url, 63 r = http.request("POST", webhook_url,
31 headers={'Content-Type': 'application/json'}, 64 headers={'Content-Type': 'application/json'},
32 body=encoded) 65 body=encoded)
33 assert r.status < 299 66 if r.status >= 300:
67 print("Error when sending message: {}".format(r.status))
34 68
35 chunk = "" 69 chunk = ""
36 for line in message.split("\n"): 70 priority = ""
37 if len(line) + len(chunk) > 2000 - 17: 71 for message in messages:
38 send_chunk(chunk) 72 m_priority = int(message.get("PRIORITY", 5))
39 chunk = line 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)
40 else: 82 else:
41 chunk += "\n" + line 83 priority = m_priority
42 send_chunk(chunk) 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)
43 96
44j = journal.Reader() 97j = journal.Reader()
45j.add_match(_SYSTEMD_UNIT="cryptoportfolio-app.service") 98j.add_match(_SYSTEMD_UNIT="cryptoportfolio-app.service")
99j.add_disjunction()
100j.add_match(UNIT="cryptoportfolio-app.service")
46j.seek_tail() 101j.seek_tail()
47list(j) 102list(j)
48 103
49while True: 104while True:
50 lines = [entry["MESSAGE"] for entry in list(j)] 105 lines = list(j)
51 if len(lines) > 0: 106 if len(lines) > 0:
52 send_to_discord("\n".join(lines)) 107 send_to_discord(lines)
53 j.wait() 108 j.wait()