From 6b95d3bd38307c1bd320a16368d0a3753601730d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Sun, 13 May 2018 14:42:51 +0200 Subject: Add logger for cryptoportfolio --- .../templates/cryptoportfolio/api_logger.py.erb | 52 ++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 modules/role/templates/cryptoportfolio/api_logger.py.erb (limited to 'modules/role/templates/cryptoportfolio/api_logger.py.erb') diff --git a/modules/role/templates/cryptoportfolio/api_logger.py.erb b/modules/role/templates/cryptoportfolio/api_logger.py.erb new file mode 100644 index 0000000..e5f0b30 --- /dev/null +++ b/modules/role/templates/cryptoportfolio/api_logger.py.erb @@ -0,0 +1,52 @@ +#!/usr/bin/python + +import time +import urllib3 +import json +from systemd import journal + +urllib3.disable_warnings() +http = urllib3.PoolManager() + +webhook_url = "<%= @slack_logger %>" +webhook_icon = "https://git.immae.eu/releases/logger.png" + +def send_to_discord(message): + def send_chunk(chunk): + data = { + "avatar_url": webhook_icon, + "username": "Logger", + "content": "```\n{}\n```".format(chunk), + } + encoded = json.dumps(data).encode('utf-8') + + r = http.request("POST", webhook_url, + headers={'Content-Type': 'application/json'}, + body=encoded) + if r.status == 429: + b = json.loads(r.data) + time.sleep(b["retry_after"] / 900) + r = http.request("POST", webhook_url, + headers={'Content-Type': 'application/json'}, + body=encoded) + assert r.status < 299 + + chunk = "" + for line in message.split("\n"): + if len(line) + len(chunk) > 2000 - 17: + send_chunk(chunk) + chunk = line + else: + chunk += "\n" + line + send_chunk(chunk) + +j = journal.Reader() +j.add_match(_SYSTEMD_UNIT="cryptoportfolio-app.service") +j.seek_tail() +list(j) + +while True: + lines = [entry["MESSAGE"] for entry in list(j)] + if len(lines) > 0: + send_to_discord("\n".join(lines)) + time.sleep(3) -- cgit v1.2.3