From b4b06c874404f3214168f0a78e21c032061426f8 Mon Sep 17 00:00:00 2001 From: "D. Berge" Date: Mon, 24 Aug 2020 20:02:12 +0200 Subject: [PATCH] Instrument alerts for deferred imports. In case of errors (or anything else of note), send_alert.py can be used to push information to a GitLab alerts endpoint. It is generic enough that it can be used with anything else, though. --- bin/send_alert.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++ etc/config.yaml | 10 +++++++++ 2 files changed, 66 insertions(+) create mode 100755 bin/send_alert.py diff --git a/bin/send_alert.py b/bin/send_alert.py new file mode 100755 index 0000000..fce2e2c --- /dev/null +++ b/bin/send_alert.py @@ -0,0 +1,56 @@ +#!/usr/bin/python3 + +""" +Send alerts if configured to do so +""" + +import os +from glob import glob +import configuration +import requests +import argparse + +if __name__ == '__main__': + + ap = argparse.ArgumentParser() + ap.add_argument("-t", "--title", required=True, default=None, help="The title of the incident.") + ap.add_argument("-d", "--description", required=False, default=None, help="A high-level summary of the problem.") + ap.add_argument("-s", "--service", required=False, default=None, help="The affected service.") + ap.add_argument("-l", "--severity", required=False, default="critical", help="The severity of the alert. Must be one of critical, high, medium, low, info, unknown. Default is critical.") + ap.add_argument("-I", "--stdin", required=False, default=None, help="Contents of standard input") + ap.add_argument("-O", "--stdout", required=False, default=None, help="Contents of standard output") + ap.add_argument("-E", "--stderr", required=False, default=None, help="Contents of standard error") + + + args = vars(ap.parse_args()) + + cfg = configuration.read() + + try: + + url = cfg["webhooks"]["alert"]["url"] + + except KeyError: + print("Alerts endpoint not configured") + exit(1) + + authkey = os.environ["GITLAB_ALERTS_AUTHKEY"] if "GITLAB_ALERTS_AUTHKEY" in os.environ else cfg["webhooks"]["alert"]["authkey"] + + headers = { + "Authorization": "Bearer " + authkey + } + + data = { + "title": args["title"], + "description": args["description"], + "service": args["service"], + "hosts": os.environ["HOST"], + "severity": args["severity"], + "input": args["stdin"], + "output": args["stdout"], + "error": args["stderr"] + } + + requests.post(url, headers=headers, json=data) + + print("Done") diff --git a/etc/config.yaml b/etc/config.yaml index 14ad17b..484bce4 100644 --- a/etc/config.yaml +++ b/etc/config.yaml @@ -2,3 +2,13 @@ db: connection_string: "host=localhost port=5432 dbname=dougal user=postgres" +webhooks: + alert: + url: https://gitlab.com/wgp/dougal/software/alerts/notify.json + authkey: "" + # The authorisation key can be provided here or read from the + # environment variable GITLAB_ALERTS_AUTHKEY. The environment + # variable has precedence. It can be saved under the user's + # Bash .profile. This is the recommended way to avoid accidentally + # committing a security token into the git repository. +