diff --git a/webhooks/atlas/README.md b/webhooks/atlas/README.md new file mode 100644 index 00000000..b275099e --- /dev/null +++ b/webhooks/atlas/README.md @@ -0,0 +1,40 @@ +MongoDB Atlas Webhook +============== + +Receive [MongoDB Atlas](https://www.mongodb.com/atlas/database) alerts via webhook callbacks. + +For help, join [![Slack chat](https://img.shields.io/badge/chat-on%20slack-blue?logo=slack)](https://slack.alerta.dev) + +Installation +------------ + +Clone the GitHub repo and run: + + $ python setup.py install + +Or, to install remotely from GitHub run: + + $ pip install git+https://github.com/alerta/alerta-contrib.git#subdirectory=webhooks/atlas + +Note: If Alerta is installed in a python virtual environment then plugins +need to be installed into the same environment for Alerta to dynamically +discover them. + +Configuration +------------- + +The custom webhook will be auto-detected and added to the list of available API endpoints. + +Add the Alerta API webhook URL in the MongoDB Atlas webhook section + + +References +---------- + + * MongoDB Atlas Webhook Integration: https://docs.atlas.mongodb.com/tutorial/third-party-service-integrations/ + +License +------- + +Copyright (c) 2020 Matthieu Serrepuy. Available under the MIT License. + diff --git a/webhooks/atlas/alerta_atlas.py b/webhooks/atlas/alerta_atlas.py new file mode 100644 index 00000000..ccee0b8d --- /dev/null +++ b/webhooks/atlas/alerta_atlas.py @@ -0,0 +1,73 @@ +from flask import request +from alerta.models.alert import Alert +from alerta.webhooks import WebhookBase +from alerta.exceptions import RejectException +import os +from hashlib import sha1 +import hmac +import logging +from base64 import b64decode + +LOG = logging.getLogger('alerta.webhooks.atlas') + +class MongodbAtlasWebhook(WebhookBase): + + def incoming(self, query_string, payload): + atlas_alert = payload + + # If the webhook secret is provider, + # We can validate that the webhook call is valid + atlas_secret = os.environ.get('MONGODB_ATLAS_VALIDATION_SECRET') + + decoded_received_signature = b64decode(request.headers.get('X-MMS-Signature')) + + if atlas_secret: + signed_body = hmac.new(atlas_secret.encode('utf-8'), request.get_data(), sha1).digest() + LOG.info(signed_body) + if not hmac.compare_digest(signed_body, decoded_received_signature): + raise RejectException("Webhook signature doesn't match") + + if atlas_alert['status'] == 'OPEN': + if request.headers.get('X-MMS-Event') == 'alert.inform': + severity = "informational" + else: + severity = os.environ.get('MONGODB_ATLAS_DEFAULT_ALERT_SEVERITY', 'warning') + else: + severity = 'normal' + + value = "N/A" + try: + if 'number' in atlas_alert['currentValue']: + value = round(atlas_alert['currentValue']['number'],2) + except: + LOG.error("Unable to real alert currentValue") + + attributes = { + "group-id": atlas_alert.get('groupId'), + "metric-type": atlas_alert.get('typeName'), + "host": atlas_alert.get('hostnameAndPort'), + "Full Text": atlas_alert.get('humanReadable'), + "Source Alert": "%s" % (atlas_alert.get('groupId'), atlas_alert.get('id'), atlas_alert.get('id')) + } + + event = 'AtlasEvent' + if atlas_alert.get('typeName') == "HOST_METRIC": + event = atlas_alert.get('metricName','AtlasEvent') + elif atlas_alert.get('typeName') == "HOST": + event = atlas_alert.get('eventTypeName','AtlasEvent') + else: + event = atlas_alert.get('metricName','AtlasEvent') + + return Alert( + resource=atlas_alert['clusterName'], + event=event, + environment='Production', + severity=severity, + service=['MongoDBAtlas'], + group='Databases', + value=value, + text="Cluster %s triggered %s" % (atlas_alert['clusterName'], event), + origin='mongodb-atlas', + attributes=attributes, + raw_data=str(payload) + ) diff --git a/webhooks/atlas/setup.py b/webhooks/atlas/setup.py new file mode 100644 index 00000000..32ea3b8c --- /dev/null +++ b/webhooks/atlas/setup.py @@ -0,0 +1,23 @@ +from setuptools import setup, find_packages + +version = '0.0.1' + +setup( + name="alerta-atlas", + version=version, + description='Alerta webhook for mongodb atlas', + url='https://github.com/alerta/alerta-contrib', + license='Apache License 2.0', + author='Matthieu Serrepuy', + author_email='matthieu@serrepuy.fr', + packages=find_packages(), + py_modules=['alerta_atlas'], + install_requires=[], + include_package_data=True, + zip_safe=True, + entry_points={ + 'alerta.webhooks': [ + 'atlas = alerta_atlas:MongodbAtlasWebhook' + ] + } +)