]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/commitdiff
Add some informations for systemd logs
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Wed, 16 May 2018 12:15:05 +0000 (14:15 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Wed, 16 May 2018 20:09:01 +0000 (22:09 +0200)
modules/role/templates/cryptoportfolio/api_logger.py.erb

index ad296e8f429195c7ef55c5cd1c36322c037eb94c..9fe564e38ea0d62562508681a368ef7d47165621 100644 (file)
@@ -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()