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:
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') or app.config['PUSHOVER_TOKEN']
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', '')
PUSHOVER_TOKEN = os.environ.get("PUSHOVER_TOKEN") or app.config["PUSHOVER_TOKEN"]
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", "")
PUSHOVER_EMERG = 2 # requires user ack
PUSHOVER_HIGH = 1
@ -27,54 +25,56 @@ PUSHOVER_BADGE = -2 # no notification
# See https://pushover.net/api#priority
PRIORITY_MAP = {
'critical': PUSHOVER_EMERG,
'major': PUSHOVER_HIGH,
'minor': PUSHOVER_NORMAL,
'warning': PUSHOVER_LOW
"critical": PUSHOVER_EMERG,
"major": PUSHOVER_HIGH,
"minor": PUSHOVER_NORMAL,
"warning": PUSHOVER_LOW,
}
class PushMessage(PluginBase):
def pre_receive(self, alert):
return alert
def post_receive(self, alert):
if alert.repeat:
return
title = '{}: {} alert for {} - {} is {}'.format(
alert.environment, alert.severity.capitalize(),
','.join(alert.service), alert.resource, alert.event
title = "{}: {} alert for {} - {} is {} - {}".format(
alert.environment,
alert.severity.capitalize(),
",".join(alert.service),
alert.resource,
alert.event,
alert.value,
)
priority = PRIORITY_MAP.get(alert.severity, PUSHOVER_BADGE)
payload = {
'token': PUSHOVER_TOKEN,
'user': PUSHOVER_USER,
'title': title,
'message': alert.text,
'url': '{}/#/alert/{}'.format(DASHBOARD_URL, alert.id),
'url_title': 'View alert',
'priority': priority,
'timestamp': alert.create_time,
'sound': 'tugboat'
"token": PUSHOVER_TOKEN,
"user": PUSHOVER_USER,
"title": title,
"message": alert.text,
"url": "{}/#/alert/{}".format(DASHBOARD_URL, alert.id),
"url_title": "View alert",
"priority": priority,
"timestamp": alert.create_time,
"sound": "tugboat",
}
if priority == PUSHOVER_EMERG:
payload['retry'] = 299 # retry every seconds
payload['expire'] = 900 # stop after seconds
payload["retry"] = 299 # retry every seconds
payload["expire"] = 900 # stop after seconds
LOG.debug('Pushover.net: %s', payload)
LOG.debug("Pushover.net: %s", payload)
try:
r = requests.post(PUSHOVER_URL, data=payload, timeout=2)
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):
return