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.
This commit is contained in:
D. Berge
2020-08-24 20:02:12 +02:00
parent bc784989f6
commit b4b06c8744
2 changed files with 66 additions and 0 deletions

56
bin/send_alert.py Executable file
View File

@@ -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")

View File

@@ -2,3 +2,13 @@
db: db:
connection_string: "host=localhost port=5432 dbname=dougal user=postgres" 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.