diff --git a/webhooks/gcp/README.md b/webhooks/gcp/README.md new file mode 100644 index 00000000..e3d89f47 --- /dev/null +++ b/webhooks/gcp/README.md @@ -0,0 +1,40 @@ +Google Cloud Platform Webhook +============== + +Receive [GCP](https://cloud.google.com) 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 GCP webhook section + + +References +---------- + + * GCP notifications: https://cloud.google.com/monitoring/support/notification-options#webhooks + +License +------- + +Copyright (c) 2020 Matthieu Serrepuy. Available under the MIT License. + diff --git a/webhooks/gcp/alerta_gcp.py b/webhooks/gcp/alerta_gcp.py new file mode 100644 index 00000000..e3c82287 --- /dev/null +++ b/webhooks/gcp/alerta_gcp.py @@ -0,0 +1,43 @@ +from flask import request +from alerta.models.alert import Alert +from alerta.webhooks import WebhookBase +import os +import logging + +LOG = logging.getLogger('alerta.webhooks.gcp') + +class GoogleCloudPlatformWebhook(WebhookBase): + + def incoming(self, query_string, payload): + gcp_alert = payload['incident'] + + if gcp_alert['state'].lower() == 'closed': + severity = 'normal' + else: + severity = os.environ.get('GCP_DEFAULT_ALERT_SEVERITY', 'warning') + + value = "N/A" + try: + value = gcp_alert['observed_value'] + except: + LOG.error("Unable to real alert observed_value") + + attributes = { + "incident-id": gcp_alert.get('incident_id'), + "documenation": gcp_alert.get('documentation'), + "Source Alert": gcp_alert.get('url') + } + + return Alert( + resource=gcp_alert.get('resource_display_name','N/A'), + event=gcp_alert.get('policy_name','GCPEvent'), + environment='Production', + severity=severity, + service=['GCP'], + group='Cloud', + value=value, + text=gcp_alert.get('summary', 'Something happened in GCP'), + origin='gcp', + attributes=attributes, + raw_data=str(payload) + ) diff --git a/webhooks/gcp/setup.py b/webhooks/gcp/setup.py new file mode 100644 index 00000000..31d6eedf --- /dev/null +++ b/webhooks/gcp/setup.py @@ -0,0 +1,23 @@ +from setuptools import setup, find_packages + +version = '0.0.1' + +setup( + name="alerta-gcp", + version=version, + description='Alerta webhook for GCP', + 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_gcp'], + install_requires=[], + include_package_data=True, + zip_safe=True, + entry_points={ + 'alerta.webhooks': [ + 'gcp = alerta_gcp:GoogleCloudPlatformWebhook' + ] + } +)