Initial commit
This commit is contained in:
commit
74a703cc2e
2 changed files with 107 additions and 0 deletions
88
alerta_matrix.py
Normal file
88
alerta_matrix.py
Normal file
|
@ -0,0 +1,88 @@
|
|||
import logging
|
||||
import os
|
||||
import requests
|
||||
import urllib
|
||||
import json
|
||||
|
||||
from alerta.plugins import app
|
||||
from alerta.plugins import PluginBase
|
||||
|
||||
LOG = logging.getLogger("alerta.plugins.matrix")
|
||||
|
||||
MATRIX_HOMESERVER_URL = [
|
||||
os.environ.get("MATRIX_HOMESERVER") or app.config["MATRIX_HOMESERVER"],
|
||||
"/_matrix/client/r0/rooms/",
|
||||
urllib.parse.quote(os.environ.get("MATRIX_ROOM") or app.config["MATRIX_ROOM"], ":"),
|
||||
"/send/m.room.message",
|
||||
]
|
||||
MATRIX_ACCESS_TOKEN = (
|
||||
os.environ.get("MATRIX_ACCESS_TOKEN") or app.config["MATRIX_ACCESS_TOKEN"]
|
||||
)
|
||||
|
||||
DASHBOARD_URL = os.environ.get("DASHBOARD_URL") or app.config.get("DASHBOARD_URL", "")
|
||||
|
||||
PRIORITY = {
|
||||
"critical": "🔴 ",
|
||||
"warning": "⚠️ ",
|
||||
"ok": "✅ ",
|
||||
"cleared": "✅ ",
|
||||
"normal": "✅ ",
|
||||
}
|
||||
|
||||
|
||||
class PushMessage(PluginBase):
|
||||
def pre_receive(self, alert):
|
||||
return alert
|
||||
|
||||
def post_receive(self, alert):
|
||||
|
||||
if alert.repeat:
|
||||
return
|
||||
|
||||
priority = PRIORITY.get(alert.severity, "")
|
||||
|
||||
body = "{}{}: {} alert for {} - {} is {}\n{}\nCurrent value: {}".format(
|
||||
priority,
|
||||
alert.environment,
|
||||
alert.severity.capitalize(),
|
||||
",".join(alert.service),
|
||||
alert.resource,
|
||||
alert.event,
|
||||
alert.text,
|
||||
alert.value,
|
||||
)
|
||||
|
||||
formatted_body = "{}<strong>{}: {} alert for {} - {} is {}</strong></br>{}</br><strong>Current value:</strong> {}".format(
|
||||
priority,
|
||||
alert.environment,
|
||||
alert.severity.capitalize(),
|
||||
",".join(alert.service),
|
||||
alert.resource,
|
||||
alert.event,
|
||||
alert.text,
|
||||
alert.value,
|
||||
)
|
||||
|
||||
payload = {
|
||||
"msgtype": "m.text",
|
||||
"format": "org.matrix.custom.html",
|
||||
"body": body,
|
||||
"formatted_body": formatted_body,
|
||||
}
|
||||
|
||||
LOG.debug("Matrix: %s", payload)
|
||||
|
||||
try:
|
||||
r = requests.post(
|
||||
"".join(MATRIX_HOMESERVER_URL),
|
||||
headers={"Authorization": "Bearer " + MATRIX_ACCESS_TOKEN},
|
||||
data=json.dumps(payload).encode("utf-8"),
|
||||
timeout=2,
|
||||
)
|
||||
except Exception as e:
|
||||
raise RuntimeError("Matrix: ERROR - %s" % e)
|
||||
|
||||
LOG.debug("Matrix: %s - %s", r.status_code, r.text)
|
||||
|
||||
def status_change(self, alert, status, text):
|
||||
return
|
19
setup.py
Normal file
19
setup.py
Normal file
|
@ -0,0 +1,19 @@
|
|||
from setuptools import setup, find_packages
|
||||
|
||||
version = "0.0.1"
|
||||
|
||||
setup(
|
||||
name="alerta-matrix",
|
||||
version=version,
|
||||
description="Alerta plugin for sending matrix alerts",
|
||||
url="https://github.com/alerta/alerta-contrib",
|
||||
license="Apache License 2.0",
|
||||
author="Magnus Walbeck",
|
||||
author_email="mw@mwalbeck.org",
|
||||
packages=find_packages(),
|
||||
py_modules=["alerta_matrix"],
|
||||
install_requires=["requests"],
|
||||
include_package_data=True,
|
||||
zip_safe=True,
|
||||
entry_points={"alerta.plugins": ["matrix = alerta_matrix:PushMessage"]},
|
||||
)
|
Loading…
Add table
Reference in a new issue