-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
src/python/WMCore/Services/AlertManager/AlertManagerAPI.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
""" | ||
AlertManagerAPI - send alerts to MONIT AlertManager via API calls | ||
""" | ||
|
||
from __future__ import division | ||
from datetime import timedelta, datetime | ||
import socket | ||
import json | ||
import logging | ||
|
||
from WMCore.Services.pycurl_manager import RequestHandler | ||
from Utils.Timers import LocalTimezone | ||
|
||
|
||
class AlertManagerAPI(object): | ||
""" | ||
A class used to send alerts via the MONIT AlertManager API | ||
""" | ||
|
||
def __init__(self, alertManagerUrl): | ||
self.alertManagerUrl = alertManagerUrl | ||
# sender's hostname is added as an annotation | ||
self.hostname = socket.gethostname() | ||
self.mgr = RequestHandler() | ||
self.ltz = LocalTimezone() | ||
|
||
def send(self, alertName, severity, summary, description, tag="wmcore", service="", endSecs=600, generatorURL=""): | ||
""" | ||
:param alertName: a unique name for the alert | ||
:param severity: low, medium, high | ||
:param summary: a short description of the alert | ||
:param description: a longer informational message with details about the alert | ||
:param service: the name of the service firing an alert | ||
:param endSecs: how many minutes until the alarm is silenced | ||
:param generatorURL: this URL will be sent to AlertManager and configured as a clickable "Source" link in the web interface | ||
AlertManager JSON format reference: https://www.prometheus.io/docs/alerting/latest/clients/ | ||
[ | ||
{ | ||
"labels": { | ||
"alertname": "<requiredAlertName>", | ||
"<labelname>": "<labelvalue>", | ||
... | ||
}, | ||
"annotations": { | ||
"<labelname>": "<labelvalue>", | ||
... | ||
}, | ||
"startsAt": "<rfc3339>", # optional, will be current time if not present | ||
"endsAt": "<rfc3339>", | ||
"generatorURL": "<generator_url>" # optional | ||
}, | ||
] | ||
""" | ||
|
||
validSeverity = ["high", "medium", "low"] | ||
if severity not in validSeverity: | ||
logging.critical("Alert submitted to AlertManagerAPI with invalid severity: %s", severity) | ||
return | ||
|
||
headers = {"Content-Type": "application/json"} | ||
request = [] | ||
alert = {} | ||
labels = {} | ||
annotations = {} | ||
|
||
# add labels | ||
labels["alertname"] = alertName | ||
labels["severity"] = severity | ||
labels["tag"] = tag | ||
labels["service"] = service | ||
alert["labels"] = labels | ||
|
||
# add annotations | ||
annotations["hostname"] = self.hostname | ||
annotations["summary"] = summary | ||
annotations["description"] = description | ||
alert["annotations"] = annotations | ||
|
||
# In python3 we won't need the LocalTimezone class | ||
# Will change to d = datetime.now().astimezone() + timedelta(seconds = endSecs) | ||
d = datetime.now(self.ltz) + timedelta(seconds = endSecs) | ||
alert["endsAt"] = d.isoformat("T") | ||
alert["generatorURL"] = generatorURL | ||
|
||
request.append(alert) | ||
# need to do this because pycurl_manager only accepts dict and encoded strings type | ||
params = json.dumps(request) | ||
|
||
res = self.mgr.getdata(self.alertManagerUrl, params=params, headers=headers, verb='POST') | ||
|
||
return res | ||
|