From b07fbcf4c474fefeac20d1cac6f417ef705880b4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Wed, 16 May 2018 14:15:05 +0200 Subject: [PATCH] Add some informations for systemd logs --- .../cryptoportfolio/api_logger.py.erb | 79 ++++++++++++++++--- 1 file 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() webhook_url = "<%= @slack_logger %>" webhook_username = "<%= @slack_logger_username %>" + +# These icons are made by CSSCREME.COM +# http://csscreme.com/freeicons/ +# License: GNU (GENERAL PUBLIC LICENSE) webhook_icon = "https://git.immae.eu/releases/logger.png" -def send_to_discord(message): - def send_chunk(chunk): +priorities = { + 0: "0", # black + 1: "6684672", # dark dark red + 2: "13369344", # dark red + 3: "16711680", # red + 4: "16737792", # orange + 5: "3407616", # green + 6: "255", # blue + 7: "16777215", # white + } + +def send_to_discord(messages): + def send_chunk(chunk, priority, fields=None): data = { "avatar_url": webhook_icon, "username": webhook_username, - "content": "```\n{}\n```".format(chunk), + "embeds": [ + { + "description": "```\n{}\n```".format(chunk[0:2000]), + "color": priorities[priority], + "fields": [], + } + ], } + if fields is not None: + data["content"] = "@everyone" + + for field in ["CLIENT_IP", "PATH", "METHOD", "STATUS_CODE", "USER_ID", "FLAG", "LATENCY"]: + value = fields.get(field) + if value is not None: + data["embeds"][-1]["fields"].append({ + "name": field.replace("_", " ").capitalize(), + "value": value, + "inline": True, + }) + encoded = json.dumps(data).encode('utf-8') r = http.request("POST", webhook_url, @@ -30,24 +63,46 @@ def send_to_discord(message): r = http.request("POST", webhook_url, headers={'Content-Type': 'application/json'}, body=encoded) - assert r.status < 299 + if r.status >= 300: + print("Error when sending message: {}".format(r.status)) chunk = "" - for line in message.split("\n"): - if len(line) + len(chunk) > 2000 - 17: - send_chunk(chunk) - chunk = line + priority = "" + for message in messages: + m_priority = int(message.get("PRIORITY", 5)) + if m_priority > 6: + continue + + if len(chunk) > 0 and m_priority != priority: + send_chunk(chunk, priority) + chunk = "" + + if m_priority <= 4: + send_chunk(message["MESSAGE"], m_priority, fields=message) else: - chunk += "\n" + line - send_chunk(chunk) + priority = m_priority + + if "SYSLOG_IDENTIFIER" in message: + line = "[{}] {}".format(message["SYSLOG_IDENTIFIER"], message["MESSAGE"]) + else: + line = message["MESSAGE"] + if len(line) + len(chunk) > 2000 - 17: + send_chunk(chunk, priority) + chunk = line + else: + chunk += "\n" + line + if len(chunk) > 0: + send_chunk(chunk, priority) j = journal.Reader() j.add_match(_SYSTEMD_UNIT="cryptoportfolio-app.service") +j.add_disjunction() +j.add_match(UNIT="cryptoportfolio-app.service") j.seek_tail() list(j) while True: - lines = [entry["MESSAGE"] for entry in list(j)] + lines = list(j) if len(lines) > 0: - send_to_discord("\n".join(lines)) + send_to_discord(lines) j.wait() -- 2.41.0