Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add a webhook for Mongodb atlas #382

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions webhooks/atlas/README.md
Original file line number Diff line number Diff line change
@@ -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.

73 changes: 73 additions & 0 deletions webhooks/atlas/alerta_atlas.py
Original file line number Diff line number Diff line change
@@ -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": "<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')
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)
)
23 changes: 23 additions & 0 deletions webhooks/atlas/setup.py
Original file line number Diff line number Diff line change
@@ -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='[email protected]',
packages=find_packages(),
py_modules=['alerta_atlas'],
install_requires=[],
include_package_data=True,
zip_safe=True,
entry_points={
'alerta.webhooks': [
'atlas = alerta_atlas:MongodbAtlasWebhook'
]
}
)