Skip to content

Commit de6a648

Browse files
committed
feat: Add a Google Cloud Platform webhook receiver
1 parent 69d271e commit de6a648

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

webhooks/gcp/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Google Cloud Platform Webhook
2+
==============
3+
4+
Receive [GCP](https://cloud.google.com) alerts via webhook callbacks.
5+
6+
For help, join [![Slack chat](https://img.shields.io/badge/chat-on%20slack-blue?logo=slack)](https://slack.alerta.dev)
7+
8+
Installation
9+
------------
10+
11+
Clone the GitHub repo and run:
12+
13+
$ python setup.py install
14+
15+
Or, to install remotely from GitHub run:
16+
17+
$ pip install git+https://github.com/alerta/alerta-contrib.git#subdirectory=webhooks/atlas
18+
19+
Note: If Alerta is installed in a python virtual environment then plugins
20+
need to be installed into the same environment for Alerta to dynamically
21+
discover them.
22+
23+
Configuration
24+
-------------
25+
26+
The custom webhook will be auto-detected and added to the list of available API endpoints.
27+
28+
Add the Alerta API webhook URL in the GCP webhook section
29+
30+
31+
References
32+
----------
33+
34+
* GCP notifications: https://cloud.google.com/monitoring/support/notification-options#webhooks
35+
36+
License
37+
-------
38+
39+
Copyright (c) 2020 Matthieu Serrepuy. Available under the MIT License.
40+

webhooks/gcp/alerta_gcp.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from flask import request
2+
from alerta.models.alert import Alert
3+
from alerta.webhooks import WebhookBase
4+
import os
5+
import logging
6+
7+
LOG = logging.getLogger('alerta.webhooks.gcp')
8+
9+
class GoogleCloudPlatformWebhook(WebhookBase):
10+
11+
def incoming(self, query_string, payload):
12+
gcp_alert = payload['incident']
13+
14+
if gcp_alert['state'] == 'OPEN':
15+
severity = os.environ.get('GCP_DEFAULT_ALERT_SEVERITY', 'warning')
16+
else:
17+
severity = 'normal'
18+
19+
value = "N/A"
20+
try:
21+
value = gcp_alert['observed_value']
22+
except:
23+
LOG.error("Unable to real alert observed_value")
24+
25+
attributes = {
26+
"incident-id": gcp_alert.get('incident_id'),
27+
"documenation": gcp_alert.get('documentation'),
28+
"Source Alert": gcp_alert.get('url')
29+
}
30+
31+
return Alert(
32+
resource=gcp_alert['resource_display_name'],
33+
event=gcp_alert['policy_name'],
34+
environment='Production',
35+
severity=severity,
36+
service=['GCP'],
37+
group='Cloud',
38+
value=value,
39+
text=gcp_alert['summary'],
40+
origin='gcp',
41+
attributes=attributes,
42+
raw_data=str(payload)
43+
)

webhooks/gcp/setup.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from setuptools import setup, find_packages
2+
3+
version = '0.0.1'
4+
5+
setup(
6+
name="alerta-gcp",
7+
version=version,
8+
description='Alerta webhook for GCP',
9+
url='https://github.com/alerta/alerta-contrib',
10+
license='Apache License 2.0',
11+
author='Matthieu Serrepuy',
12+
author_email='[email protected]',
13+
packages=find_packages(),
14+
py_modules=['alerta_gcp'],
15+
install_requires=[],
16+
include_package_data=True,
17+
zip_safe=True,
18+
entry_points={
19+
'alerta.webhooks': [
20+
'gcp = alerta_gcp:GoogleCloudPlatformWebhook'
21+
]
22+
}
23+
)

0 commit comments

Comments
 (0)