Add alert value to pushover notification

This commit is contained in:
Magnus Walbeck 2023-07-28 12:01:51 +02:00
parent 4ba3682468
commit 1f548f4534
Signed by: mwalbeck
SSH key fingerprint: SHA256:E7ZkPPw+zrr8Nxtu1Z908TRF7I0dNjNavoABIf49p4U

View file

@ -9,15 +9,13 @@ try:
except ImportError: except ImportError:
from alerta.app import app # alerta < 5.0 from alerta.app import app # alerta < 5.0
LOG = logging.getLogger('alerta.plugins.pushover') LOG = logging.getLogger("alerta.plugins.pushover")
PUSHOVER_URL = 'https://api.pushover.net/1/messages.json' PUSHOVER_URL = "https://api.pushover.net/1/messages.json"
PUSHOVER_TOKEN = os.environ.get( PUSHOVER_TOKEN = os.environ.get("PUSHOVER_TOKEN") or app.config["PUSHOVER_TOKEN"]
'PUSHOVER_TOKEN') or app.config['PUSHOVER_TOKEN'] PUSHOVER_USER = os.environ.get("PUSHOVER_USER") or app.config["PUSHOVER_USER"]
PUSHOVER_USER = os.environ.get('PUSHOVER_USER') or app.config['PUSHOVER_USER'] DASHBOARD_URL = os.environ.get("DASHBOARD_URL") or app.config.get("DASHBOARD_URL", "")
DASHBOARD_URL = os.environ.get(
'DASHBOARD_URL') or app.config.get('DASHBOARD_URL', '')
PUSHOVER_EMERG = 2 # requires user ack PUSHOVER_EMERG = 2 # requires user ack
PUSHOVER_HIGH = 1 PUSHOVER_HIGH = 1
@ -27,54 +25,56 @@ PUSHOVER_BADGE = -2 # no notification
# See https://pushover.net/api#priority # See https://pushover.net/api#priority
PRIORITY_MAP = { PRIORITY_MAP = {
'critical': PUSHOVER_EMERG, "critical": PUSHOVER_EMERG,
'major': PUSHOVER_HIGH, "major": PUSHOVER_HIGH,
'minor': PUSHOVER_NORMAL, "minor": PUSHOVER_NORMAL,
'warning': PUSHOVER_LOW "warning": PUSHOVER_LOW,
} }
class PushMessage(PluginBase): class PushMessage(PluginBase):
def pre_receive(self, alert): def pre_receive(self, alert):
return alert return alert
def post_receive(self, alert): def post_receive(self, alert):
if alert.repeat: if alert.repeat:
return return
title = '{}: {} alert for {} - {} is {}'.format( title = "{}: {} alert for {} - {} is {} - {}".format(
alert.environment, alert.severity.capitalize(), alert.environment,
','.join(alert.service), alert.resource, alert.event alert.severity.capitalize(),
",".join(alert.service),
alert.resource,
alert.event,
alert.value,
) )
priority = PRIORITY_MAP.get(alert.severity, PUSHOVER_BADGE) priority = PRIORITY_MAP.get(alert.severity, PUSHOVER_BADGE)
payload = { payload = {
'token': PUSHOVER_TOKEN, "token": PUSHOVER_TOKEN,
'user': PUSHOVER_USER, "user": PUSHOVER_USER,
'title': title, "title": title,
'message': alert.text, "message": alert.text,
'url': '{}/#/alert/{}'.format(DASHBOARD_URL, alert.id), "url": "{}/#/alert/{}".format(DASHBOARD_URL, alert.id),
'url_title': 'View alert', "url_title": "View alert",
'priority': priority, "priority": priority,
'timestamp': alert.create_time, "timestamp": alert.create_time,
'sound': 'tugboat' "sound": "tugboat",
} }
if priority == PUSHOVER_EMERG: if priority == PUSHOVER_EMERG:
payload['retry'] = 299 # retry every seconds payload["retry"] = 299 # retry every seconds
payload['expire'] = 900 # stop after seconds payload["expire"] = 900 # stop after seconds
LOG.debug('Pushover.net: %s', payload) LOG.debug("Pushover.net: %s", payload)
try: try:
r = requests.post(PUSHOVER_URL, data=payload, timeout=2) r = requests.post(PUSHOVER_URL, data=payload, timeout=2)
except Exception as e: except Exception as e:
raise RuntimeError('Pushover.net: ERROR - %s' % e) raise RuntimeError("Pushover.net: ERROR - %s" % e)
LOG.debug('Pushover.net: %s - %s', r.status_code, r.text) LOG.debug("Pushover.net: %s - %s", r.status_code, r.text)
def status_change(self, alert, status, text): def status_change(self, alert, status, text):
return return