Skip to content

Commit

Permalink
feat: Add a Google Cloud Platform webhook receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
lotooo committed Mar 30, 2022
1 parent 69d271e commit 4f0abd6
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
40 changes: 40 additions & 0 deletions webhooks/gcp/README.md
Original file line number Diff line number Diff line change
@@ -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.

43 changes: 43 additions & 0 deletions webhooks/gcp/alerta_gcp.py
Original file line number Diff line number Diff line change
@@ -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)
)
23 changes: 23 additions & 0 deletions webhooks/gcp/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-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'
]
}
)

0 comments on commit 4f0abd6

Please sign in to comment.