feat: goalert plugin (#363)
* goalert: [impr] init commit * goalert: [impr] expired added
This commit is contained in:
parent
40acf3b7d4
commit
72fff0a366
3 changed files with 155 additions and 0 deletions
plugins/goalert
33
plugins/goalert/README.md
Normal file
33
plugins/goalert/README.md
Normal file
|
@ -0,0 +1,33 @@
|
|||
Goalert Plugin
|
||||
================
|
||||
|
||||
Send goalert messages for new alerts.
|
||||
|
||||
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=plugins/goalert
|
||||
|
||||
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
|
||||
-------------
|
||||
|
||||
Add `goalert` to the list of enabled `PLUGINS` in `alertad.conf` server
|
||||
configuration file and set plugin-specific variables either in the
|
||||
server configuration file or as environment variables.
|
||||
|
||||
Add some configs to `alertad.conf`:
|
||||
```
|
||||
GOALERT_URL = 'https://alerta.example'
|
||||
GOALERT_TOKEN = 'secret key'
|
||||
GOALERT_VERIFY = '/usr/local/share/ca-certificates/ca.crt' # or False
|
||||
```
|
96
plugins/goalert/alerta_goalert.py
Normal file
96
plugins/goalert/alerta_goalert.py
Normal file
|
@ -0,0 +1,96 @@
|
|||
import logging
|
||||
import os
|
||||
import re
|
||||
import requests
|
||||
|
||||
try:
|
||||
from alerta.plugins import app # alerta >= 5.0
|
||||
except ImportError:
|
||||
from alerta.app import app # alerta < 5.0
|
||||
from alerta.plugins import PluginBase
|
||||
|
||||
LOG = logging.getLogger('alerta.plugins.goalert')
|
||||
LOG.info('Initializing')
|
||||
|
||||
GOALERT_URL = os.environ.get('GOALERT_URL') or app.config['GOALERT_URL']
|
||||
GOALERT_TOKEN = os.environ.get('GOALERT_TOKEN') or app.config['GOALERT_TOKEN']
|
||||
GOALERT_VERIFY = os.environ.get('GOALERT_VERIFY') or app.config['GOALERT_VERIFY']
|
||||
DASHBOARD_URL = os.environ.get('DASHBOARD_URL') or app.config['DASHBOARD_URL']
|
||||
|
||||
class TriggerEvent(PluginBase):
|
||||
|
||||
def goalerts_endpoint(self):
|
||||
return '%s%s' % (GOALERT_URL, '/api/v2/generic/incoming')
|
||||
|
||||
def goalert_close_alert(self, alert, why):
|
||||
closeUrl = self.goalerts_endpoint()
|
||||
|
||||
json = {
|
||||
"token": GOALERT_TOKEN,
|
||||
"dedup": alert.id,
|
||||
"action": "close",
|
||||
"details": why
|
||||
}
|
||||
LOG.debug('goalert close %s: %s %s' % (why, alert.id, closeUrl))
|
||||
|
||||
try:
|
||||
r = requests.post(closeUrl, json, timeout=2, verify=GOALERT_VERIFY)
|
||||
except Exception as e:
|
||||
raise RuntimeError("goalert connection error: %s" % e)
|
||||
return r
|
||||
|
||||
# def goalert_ack_alert(self, alert, why):
|
||||
|
||||
# ackUrl = goalert_EVENTS_ACK_URL % alert.id
|
||||
# LOG.debug('goalert ack %s: %s %s' % (why, alert.id, ackUrl))
|
||||
|
||||
# try:
|
||||
# r = requests.post(ackUrl, json={}, headers=headers, timeout=2)
|
||||
# except Exception as e:
|
||||
# raise RuntimeError("goalert connection error: %s" % e)
|
||||
# return r
|
||||
|
||||
def pre_receive(self, alert):
|
||||
return alert
|
||||
|
||||
def post_receive(self, alert):
|
||||
LOG.debug('Alert receive %s: %s' % (alert.id, alert.get_body(history=False)))
|
||||
if alert.repeat:
|
||||
LOG.debug('Alert repeating; ignored')
|
||||
return
|
||||
|
||||
if (alert.severity in ['cleared', 'normal', 'ok']) or (alert.status == 'closed'):
|
||||
r = self.goalert_close_alert(alert, 'CREATE-CLOSE')
|
||||
else:
|
||||
body = alert.get_body(history=False)
|
||||
json = {
|
||||
"token": GOALERT_TOKEN,
|
||||
"dedup": alert.id,
|
||||
"summary": alert.resource,
|
||||
"details": "[%s] %s: %s" % (alert.environment, alert.resource, alert.value)
|
||||
}
|
||||
LOG.debug('goalert CREATE payload: %s' % json)
|
||||
endpoint = self.goalerts_endpoint()
|
||||
|
||||
try:
|
||||
r = requests.post(endpoint, json, timeout=2, verify=GOALERT_VERIFY)
|
||||
except Exception as e:
|
||||
raise RuntimeError("goalert connection error: %s" % e)
|
||||
|
||||
LOG.debug('goalert response: %s - %s' % (r.status_code, r.text))
|
||||
|
||||
def status_change(self, alert, status, text):
|
||||
LOG.debug('Alert change %s to %s: %s' % (alert.id, status, alert.get_body(history=False)))
|
||||
|
||||
if status not in ['ack', 'assign', 'closed', 'expired']:
|
||||
LOG.debug('Not sending status change to goalert: %s to %s' % (alert.id, status))
|
||||
return
|
||||
|
||||
if status == 'closed':
|
||||
r = self.goalert_close_alert(alert, 'STATUS-CLOSE')
|
||||
elif status == 'expired':
|
||||
r = self.goalert_close_alert(alert, 'STATUS-EXPIRED')
|
||||
# elif status == 'ack':
|
||||
# r = self.goalert_ack_alert(alert, 'STATUS-ACK')
|
||||
|
||||
LOG.debug('goalert response: %s - %s' % (r.status_code, r.text))
|
26
plugins/goalert/setup.py
Normal file
26
plugins/goalert/setup.py
Normal file
|
@ -0,0 +1,26 @@
|
|||
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
version = '5.0.3'
|
||||
|
||||
setup(
|
||||
name="alerta-goalert",
|
||||
version=version,
|
||||
description='Alerta plugin for GoAlert',
|
||||
url='https://github.com/alerta/alerta-contrib',
|
||||
license='MIT',
|
||||
author='SKob',
|
||||
author_email='skobolo@gmail.com',
|
||||
packages=find_packages(),
|
||||
py_modules=['alerta_goalert'],
|
||||
install_requires=[
|
||||
'requests'
|
||||
],
|
||||
include_package_data=True,
|
||||
zip_safe=True,
|
||||
entry_points={
|
||||
'alerta.plugins': [
|
||||
'goalert = alerta_goalert:TriggerEvent'
|
||||
]
|
||||
}
|
||||
)
|
Loading…
Add table
Reference in a new issue