[Feat] Query Generic Webhook ()

Co-authored-by: pvillaverde <pvillaverde@qualigy.com>
This commit is contained in:
Fedello 2021-10-18 23:39:37 +02:00 committed by GitHub
parent a9799587af
commit 21a80d1f1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 141 additions and 0 deletions

37
webhooks/query/README.md Normal file
View file

@ -0,0 +1,37 @@
Query Webhook
==============
Receive Alerts using only query parameters by webhook. For those apps or use cases when you can't specify a payload or body to the query and just one to send all by query parameters.
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/query
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
-------------
You can specify all the parameters on the query except Attributes for the moment.
https://[ALERTA-URL]/api/webhooks/query?api-key=[YOUR-API-KEY]&service=TheForce&severity=critical&resource=Alderaan&event=SomethingTerribleHappened&text=AGreatDisturbanceInTheForce&tags=Force,Alderaan,DeathStar&origin=ObiWanKenobi&group=Jedi&timeout=300
References
----------
* Based on the other Alerta-Contrib plugins.
License
-------
Copyright (c) 2021 Pablo Villaverde. Available under the MIT License.

View file

@ -0,0 +1,80 @@
from alerta.models.alert import Alert
from alerta.webhooks import WebhookBase
class QueryWebhook(WebhookBase):
def incoming(self, query_string, payload):
# Load variables from querystring
# resource
try:
resource = query_string['resource']
except:
resource = 'QueryDefaultResource'
# environment
try:
environment = query_string['environment']
except:
environment = 'Production'
# severity
try:
severity = query_string['severity']
except:
severity = 'major'
# group
try:
group = query_string['group']
except:
group = 'QueryDefaultGroup'
# event
try:
event = query_string['event']
except:
event = 'QueryDefaultEvent'
# service (Must be an array)
try:
service = [query_string['service']]
except:
service = ['QueryDefaultService']
# value
try:
value = query_string['value']
except:
value = ''
# text
try:
text = query_string['text']
except:
text = ''
# tags (Must be an array)
try:
tags = query_string['tags'].split(',')
except:
tags = []
# origin
try:
origin = query_string['origin']
except:
origin = 'QueryDefaultOrigin'
# timeout
try:
timeout = int(query_string['timeout'])
except:
timeout = 86400
return Alert(
resource=resource,
event=event,
environment=environment,
severity=severity,
service=service,
group=group,
value=value,
text=text,
tags=tags,
origin=origin,
timeout=timeout,
raw_data=query_string
)

24
webhooks/query/setup.py Normal file
View file

@ -0,0 +1,24 @@
from setuptools import setup, find_packages
version = '1.0.0'
setup(
name="alerta-query",
version=version,
description='Alerta Generic Webhook by query parameters',
url='https://github.com/alerta/alerta-contrib',
license='MIT',
author='Pablo Villaverde',
author_email='pvillaverdecastro@gmail.com',
packages=find_packages(),
py_modules=['alerta_query'],
install_requires=[],
include_package_data=True,
zip_safe=True,
entry_points={
'alerta.webhooks': [
'query = alerta_query:QueryWebhook'
]
}
)