alertify/alertify.py

259 lines
7.3 KiB
Python
Raw Normal View History

2020-10-13 09:19:35 +01:00
#!/usr/bin/env python3
"""
2020-10-20 15:39:55 +01:00
Module to act as a bridge between Prometheus Alertmanager and Gotify
2020-10-13 09:19:35 +01:00
"""
import argparse
import functools
2020-10-13 09:19:35 +01:00
import http.client
import json
import os
import sys
from distutils.util import strtobool
2020-10-13 09:19:35 +01:00
from http.server import HTTPServer, SimpleHTTPRequestHandler
import yaml
DEFAULTS = {
'disable_resolved': bool(False),
'gotify_key': str(),
'gotify_port': int(80),
'gotify_server': str('localhost'),
'listen_port': int(8080),
'verbose': bool(False),
}
2020-10-13 09:19:35 +01:00
class HTTPHandler(SimpleHTTPRequestHandler):
"""
2020-10-20 15:39:55 +01:00
Class to handle the HTTP requests from a client
2020-10-13 09:19:35 +01:00
"""
config = None
@staticmethod
def set_config(config):
"""
Method
"""
HTTPHandler.config = config
2020-10-13 09:19:35 +01:00
# Override built-in method
# pylint: disable=invalid-name
def do_GET(self):
"""
Method to handle GET requests
"""
if self.path == '/healthcheck':
if not healthy(self.config):
2020-10-15 09:08:23 +01:00
print('ERROR: Check requirements')
self._respond(500, 'ERR')
self._respond(200, 'OK')
2020-10-13 09:19:35 +01:00
# Override built-in method
# pylint: disable=invalid-name
def do_POST(self):
"""
Method to handle POST requests from AlertManager
"""
if self.path == '/alert':
self._alerts()
2020-10-15 09:08:23 +01:00
def _respond(self, status, message):
"""
Method to output a simple HTTP status and string to the client
"""
2020-10-15 09:08:23 +01:00
self.send_response(int(status) or 500)
2020-10-13 09:19:35 +01:00
self.end_headers()
2020-10-15 09:08:23 +01:00
self.wfile.write(bytes(str(message).encode()))
2020-10-13 09:19:35 +01:00
def _alerts(self):
"""
Method to handle the request for alerts
"""
if not healthy(self.config):
2020-10-15 09:08:23 +01:00
print('ERROR: Check requirements')
self._respond(500, 'Server not configured correctly')
2020-10-13 09:19:35 +01:00
return
2020-10-13 15:31:33 +01:00
2020-10-13 09:19:35 +01:00
content_length = int(self.headers['Content-Length'])
rawdata = self.rfile.read(content_length)
2020-10-15 09:08:23 +01:00
try:
alert = json.loads(rawdata.decode())
except json.decoder.JSONDecodeError as error:
print(f'ERROR: Bad JSON: {error}')
self._respond(400, f'Bad JSON: {error}')
return
if self.config.get('verbose'):
2020-10-15 09:08:23 +01:00
print('Received from Alertmanager:')
print(json.dumps(alert, indent=2))
try:
if alert['status'] == 'resolved':
if self.config.get('disable_resolved'):
print('Ignoring resolved messages')
self._respond(200, 'Ignored. "resolved" messages are disabled')
return
2020-10-15 09:08:23 +01:00
prefix = 'Resolved'
else:
prefix = alert['commonLabels'].get(
'severity', 'default').capitalize()
gotify_msg = {
'title': '{}: {}'.format(
alert['receiver'],
alert['commonLabels'].get('instance', 'Unknown')
),
'message': '{}: {}'.format(
prefix,
alert['commonAnnotations'].get('description', '...')
),
'priority': int(alert['commonLabels'].get('priority', 5))
}
except KeyError as error:
print(f'ERROR: KeyError: {error}')
self._respond(400, f'Missing field: {error}')
return
if self.config.get('verbose'):
2020-10-15 09:08:23 +01:00
print('Sending to Gotify:')
print(json.dumps(gotify_msg, indent=2))
response = gotify_send(
self.config.get('gotify_server'),
self.config.get('gotify_port'),
self.config.get('gotify_key'),
gotify_msg
2020-10-13 09:19:35 +01:00
)
if self.config.get('verbose'):
print('Status: {status}, Reason: {reason}'.format(**response))
self._respond(response['status'], response['reason'])
2020-10-13 09:19:35 +01:00
def gotify_send(server, port, authkey, payload):
"""
Function to POST data to a Gotify server
"""
gotify = http.client.HTTPConnection(server, port)
headers = {
'X-Gotify-Key': authkey,
'Content-type': 'application/json',
}
try:
gotify.request('POST', '/message', json.dumps(payload), headers)
response = gotify.getresponse()
except ConnectionRefusedError as error:
print(f'ERROR: {error}')
return {
'status': error.errno,
'reason': error.strerror
}
2020-10-13 09:19:35 +01:00
2020-10-15 09:08:23 +01:00
return {
'status': response.status,
'reason': response.reason
}
2020-10-13 09:19:35 +01:00
def healthy(config):
2020-10-13 09:19:35 +01:00
"""
Simple function to return if all the requirements are met
2020-10-13 09:19:35 +01:00
"""
return all([
len(config.get('gotify_key', ''))
2020-10-13 09:19:35 +01:00
])
@functools.lru_cache
def parse_config(configfile):
"""
Function to parse a configuration file
"""
config = {}
try:
with open(configfile, 'r') as file:
parsed = yaml.safe_load(file.read())
except FileNotFoundError as error:
print(f'WARN: {error}')
parsed = {}
# Iterate over the DEFAULTS dictionary and check for environment variables
# of the same name, then check for any items in the YAML config, otherwise
# use the default values.
# Ensure the types are adhered to.
for key, val in DEFAULTS.items():
config[key] = os.environ.get(key.upper(), parsed.get(key, val))
if isinstance(val, bool):
config[key] = strtobool(str(config[key]))
else:
config[key] = type(val)(config[key])
if config['verbose']:
print(f'Config:\n{yaml.dump(config, explicit_start=True, default_flow_style=False)}')
return config
2020-10-13 09:19:35 +01:00
if __name__ == '__main__':
def parse_cli():
2020-10-13 09:19:35 +01:00
"""
Function to parse the CLI
"""
maxlen = max([len(key) for key in DEFAULTS])
defaults = [
f' * {key.upper().ljust(maxlen)} (default: {val if val != "" else "None"})'
for key, val in DEFAULTS.items()
]
2020-10-13 17:26:14 +01:00
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description='Bridge between Prometheus Alertmanager and Gotify\n',
epilog='The following environment variables will override any config or default:\n' +
'\n'.join(defaults)
)
parser.add_argument(
'-c', '--config',
default=f'{os.path.splitext(__file__)[0]}.yaml',
help=f'path to config YAML. (default: {os.path.splitext(__file__)[0]}.yaml)',
2020-10-13 17:26:14 +01:00
)
2020-10-13 09:19:35 +01:00
parser.add_argument(
'-H', '--healthcheck',
action='store_true',
help='simply exit with 0 for healthy or 1 when unhealthy',
2020-10-13 09:19:35 +01:00
)
return parser.parse_args()
def main():
"""
main()
"""
args = parse_cli()
config = parse_config(args.config)
2020-10-13 09:19:35 +01:00
if args.healthcheck:
# Invert the sense of 'healthy' for Unix CLI usage
return not healthy(config)
listen_port = config.get('listen_port')
2020-10-13 09:19:35 +01:00
print(f'Starting web server on port {listen_port}')
2020-10-15 09:08:23 +01:00
try:
with HTTPServer(('', listen_port), HTTPHandler) as webserver:
HTTPHandler.set_config(config)
webserver.serve_forever()
2020-10-15 09:08:23 +01:00
except KeyboardInterrupt:
print('Exiting')
2020-10-13 09:19:35 +01:00
return 0
sys.exit(main())