2016-03-07 12:13:52 +00:00
|
|
|
|
|
|
|
import datetime
|
2016-09-15 09:16:11 +00:00
|
|
|
import logging
|
2017-09-17 14:56:55 +00:00
|
|
|
import os
|
|
|
|
import requests
|
2016-03-07 12:13:52 +00:00
|
|
|
|
2017-09-17 14:56:55 +00:00
|
|
|
try:
|
|
|
|
from alerta.plugins import app # alerta >= 5.0
|
|
|
|
except ImportError:
|
|
|
|
from alerta.app import app # alerta < 5.0
|
2016-03-07 12:13:52 +00:00
|
|
|
from alerta.plugins import PluginBase
|
|
|
|
|
2016-09-15 09:16:11 +00:00
|
|
|
LOG = logging.getLogger('alerta.plugins.prometheus')
|
2016-03-07 12:13:52 +00:00
|
|
|
|
2017-01-03 09:59:47 +00:00
|
|
|
DEFAULT_ALERTMANAGER_API_URL = 'http://localhost:9093'
|
|
|
|
|
|
|
|
ALERTMANAGER_API_URL = os.environ.get('ALERTMANAGER_API_URL') or app.config.get('ALERTMANAGER_API_URL', None)
|
2018-09-11 08:12:59 +00:00
|
|
|
ALERTMANAGER_USERNAME = os.environ.get('ALERTMANAGER_USERNAME') or app.config.get('ALERTMANAGER_USERNAME', None)
|
|
|
|
ALERTMANAGER_PASSWORD = os.environ.get('ALERTMANAGER_PASSWORD') or app.config.get('ALERTMANAGER_PASSWORD', None)
|
2016-11-19 21:23:47 +00:00
|
|
|
ALERTMANAGER_SILENCE_DAYS = os.environ.get('ALERTMANAGER_SILENCE_DAYS') or app.config.get('ALERTMANAGER_SILENCE_DAYS', 1)
|
2019-07-24 06:39:08 +00:00
|
|
|
ALERTMANAGER_SILENCE_FROM_ACK = os.environ.get('ALERTMANAGER_SILENCE_FROM_ACK') or app.config.get('ALERTMANAGER_SILENCE_FROM_ACK', False)
|
2016-03-07 12:13:52 +00:00
|
|
|
|
|
|
|
class AlertmanagerSilence(PluginBase):
|
|
|
|
|
2018-09-11 08:12:59 +00:00
|
|
|
def __init__(self, name=None):
|
|
|
|
|
|
|
|
self.auth = (ALERTMANAGER_USERNAME, ALERTMANAGER_PASSWORD) if ALERTMANAGER_USERNAME else None
|
|
|
|
|
|
|
|
super(AlertmanagerSilence, self).__init__(name)
|
|
|
|
|
2016-03-07 12:13:52 +00:00
|
|
|
def pre_receive(self, alert):
|
|
|
|
return alert
|
|
|
|
|
|
|
|
def post_receive(self, alert):
|
|
|
|
return
|
|
|
|
|
|
|
|
def status_change(self, alert, status, text):
|
2016-11-19 21:23:47 +00:00
|
|
|
|
2016-03-07 12:13:52 +00:00
|
|
|
if alert.event_type != 'prometheusAlert':
|
|
|
|
return
|
|
|
|
|
|
|
|
if alert.status == status:
|
|
|
|
return
|
|
|
|
|
|
|
|
if status == 'ack':
|
2019-07-24 06:39:08 +00:00
|
|
|
|
|
|
|
if ALERTMANAGER_SILENCE_FROM_ACK:
|
|
|
|
silence_seconds = int(alert.timeout)
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
silence_days = int(ALERTMANAGER_SILENCE_DAYS)
|
|
|
|
except Exception as e:
|
|
|
|
LOG.error(
|
|
|
|
"Alertmanager: Could not parse 'ALERTMANAGER_SILENCE_DAYS': %s", e)
|
|
|
|
raise RuntimeError(
|
|
|
|
"Could not parse 'ALERTMANAGER_SILENCE_DAYS': %s" % e)
|
|
|
|
silence_seconds = silence_days * 86400
|
|
|
|
|
|
|
|
LOG.debug('Alertmanager: Add silence for alertname=%s instance=%s timeout=%s',
|
|
|
|
alert.event, alert.resource, str(silence_seconds))
|
2016-03-07 12:13:52 +00:00
|
|
|
data = {
|
|
|
|
"matchers": [
|
|
|
|
{
|
|
|
|
"name": "alertname",
|
|
|
|
"value": alert.event
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "instance",
|
|
|
|
"value": alert.resource
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"startsAt": datetime.datetime.utcnow().replace(microsecond=0).isoformat() + ".000Z",
|
2019-07-24 06:39:08 +00:00
|
|
|
"endsAt": (datetime.datetime.utcnow() + datetime.timedelta(seconds=silence_seconds))
|
2016-03-07 12:13:52 +00:00
|
|
|
.replace(microsecond=0).isoformat() + ".000Z",
|
|
|
|
"createdBy": "alerta",
|
|
|
|
"comment": text if text != '' else "silenced by alerta"
|
|
|
|
}
|
|
|
|
|
2017-01-03 09:59:47 +00:00
|
|
|
base_url = ALERTMANAGER_API_URL or alert.attributes.get('externalUrl', DEFAULT_ALERTMANAGER_API_URL)
|
2017-01-03 02:03:02 +00:00
|
|
|
url = base_url + '/api/v1/silences'
|
2017-12-24 20:36:38 +00:00
|
|
|
|
|
|
|
LOG.debug('Alertmanager: URL=%s', url)
|
|
|
|
LOG.debug('Alertmanager: data=%s', data)
|
|
|
|
|
2016-03-07 12:13:52 +00:00
|
|
|
try:
|
2018-09-11 08:12:59 +00:00
|
|
|
r = requests.post(url, json=data, auth=self.auth, timeout=2)
|
2016-03-07 12:13:52 +00:00
|
|
|
except Exception as e:
|
2017-12-24 20:36:38 +00:00
|
|
|
raise RuntimeError("Alertmanager: ERROR - %s" % e)
|
2016-11-19 21:23:47 +00:00
|
|
|
LOG.debug('Alertmanager: %s - %s', r.status_code, r.text)
|
|
|
|
|
|
|
|
# example r={"status":"success","data":{"silenceId":8}}
|
|
|
|
try:
|
2020-02-20 11:42:10 +00:00
|
|
|
data = r.json().get('data', [])
|
|
|
|
if data:
|
|
|
|
silenceId = data['silenceId']
|
|
|
|
alert.attributes['silenceId'] = silenceId
|
|
|
|
else:
|
|
|
|
silenceId = alert.attributes.get('silenceId', "unknown")
|
2017-08-13 21:49:14 +00:00
|
|
|
text = text + ' (silenced in Alertmanager)'
|
2016-11-19 21:23:47 +00:00
|
|
|
except Exception as e:
|
2017-12-24 20:36:38 +00:00
|
|
|
raise RuntimeError("Alertmanager: ERROR - %s" % e)
|
2016-11-19 21:23:47 +00:00
|
|
|
LOG.debug('Alertmanager: Added silenceId %s to attributes', silenceId)
|
|
|
|
|
|
|
|
elif status == 'open':
|
|
|
|
LOG.debug('Alertmanager: Remove silence for alertname=%s instance=%s', alert.event, alert.resource)
|
|
|
|
|
|
|
|
silenceId = alert.attributes.get('silenceId', None)
|
|
|
|
if silenceId:
|
2017-01-03 09:59:47 +00:00
|
|
|
base_url = ALERTMANAGER_API_URL or alert.attributes.get('externalUrl', DEFAULT_ALERTMANAGER_API_URL)
|
2017-01-03 02:03:02 +00:00
|
|
|
url = base_url + '/api/v1/silence/%s' % silenceId
|
2016-11-19 21:23:47 +00:00
|
|
|
try:
|
2018-09-11 08:12:59 +00:00
|
|
|
r = requests.delete(url, auth=self.auth, timeout=2)
|
2016-11-19 21:23:47 +00:00
|
|
|
except Exception as e:
|
2017-12-24 20:36:38 +00:00
|
|
|
raise RuntimeError("Alertmanager: ERROR - %s" % e)
|
2016-11-19 21:23:47 +00:00
|
|
|
LOG.debug('Alertmanager: %s - %s', r.status_code, r.text)
|
2016-03-07 12:13:52 +00:00
|
|
|
|
2016-11-19 21:23:47 +00:00
|
|
|
try:
|
2017-08-13 21:49:14 +00:00
|
|
|
alert.attributes['silenceId'] = None
|
2016-11-19 21:23:47 +00:00
|
|
|
except Exception as e:
|
2017-12-24 20:36:38 +00:00
|
|
|
raise RuntimeError("Alertmanager: ERROR - %s" % e)
|
2016-11-19 21:23:47 +00:00
|
|
|
LOG.debug('Alertmanager: Removed silenceId %s from attributes', silenceId)
|
2017-08-13 21:49:14 +00:00
|
|
|
|
|
|
|
return alert, status, text
|