feat: Add a webhook for Mongodb atlas

This commit is contained in:
Matthieu Serrepuy 2022-02-28 11:07:57 +01:00
parent 69d271ef9f
commit b06b48010e
No known key found for this signature in database
GPG key ID: 27AAEAC3BFDD5A76
3 changed files with 134 additions and 0 deletions

40
webhooks/atlas/README.md Normal file
View file

@ -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.

View file

@ -0,0 +1,71 @@
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": "<a href=https://cloud.mongodb.com/api/atlas/v1.0/groups/%s/alerts/%s>%s</a>" % (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')
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)
)

23
webhooks/atlas/setup.py Normal file
View file

@ -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'
]
}
)