X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FPuppet.git;a=blobdiff_plain;f=modules%2Frole%2Ftemplates%2Fcryptoportfolio%2Fapi_logger.py.erb;h=2b6615193256bf65d719bd6888839694dab481ef;hp=e5f0b3048c4194d97080958ec1d73849f3d8c53b;hb=19c467dccfd00193a66f1341f068987da7bca14b;hpb=6b95d3bd38307c1bd320a16368d0a3753601730d diff --git a/modules/role/templates/cryptoportfolio/api_logger.py.erb b/modules/role/templates/cryptoportfolio/api_logger.py.erb index e5f0b30..2b66151 100644 --- a/modules/role/templates/cryptoportfolio/api_logger.py.erb +++ b/modules/role/templates/cryptoportfolio/api_logger.py.erb @@ -9,15 +9,49 @@ urllib3.disable_warnings() http = urllib3.PoolManager() webhook_url = "<%= @slack_logger %>" -webhook_icon = "https://git.immae.eu/releases/logger.png" +webhook_username = "<%= @slack_logger_username %>" -def send_to_discord(message): - def send_chunk(chunk): +# These icons are made by CSSCREME.COM +# http://csscreme.com/freeicons/ +# License: GNU (GENERAL PUBLIC LICENSE) +webhook_icon = "https://release.immae.eu/logger.png" + +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": "Logger", - "content": "```\n{}\n```".format(chunk), + "username": webhook_username, + "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, @@ -29,24 +63,47 @@ 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)) - time.sleep(3) + send_to_discord(lines) + time.sleep(5) + j.wait()