initial commit
This commit is contained in:
19
Dockerfile
Normal file
19
Dockerfile
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
FROM python:3.8-slim-buster
|
||||||
|
|
||||||
|
RUN useradd -m bridge
|
||||||
|
|
||||||
|
RUN mkdir /app
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
RUN apt-get update &&\
|
||||||
|
apt-get install -y build-essential &&\
|
||||||
|
rm -rf /var/lib/apt/* /var/cache/apt/*
|
||||||
|
|
||||||
|
COPY requirements.txt ./
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
COPY bridge.py ./
|
||||||
|
COPY bridge.sh ./
|
||||||
|
|
||||||
|
USER bridge
|
||||||
|
CMD [ "bash", "bridge.sh" ]
|
||||||
13
README.md
Normal file
13
README.md
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# Grafana-matrix-alert
|
||||||
|
|
||||||
|
This is a small container to send alerts from grafana to a matrix room.
|
||||||
|
|
||||||
|
Preconditions: in matrix, a user must exist for grafana to use. That user must be a member of the "alert room".
|
||||||
|
|
||||||
|
Set the following environment variables when running the container:
|
||||||
|
MATRIX_HOMESERVER_URL
|
||||||
|
MATRIX_USER
|
||||||
|
MATRIX_PASSWORD
|
||||||
|
MATRIX_ROOM_ID (the internal room ID)
|
||||||
|
|
||||||
|
In Grafana, configure a notification channel of type "webhook" and set the URL to the host/port where the container is running.
|
||||||
63
bridge.py
Normal file
63
bridge.py
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
import asyncio
|
||||||
|
import datetime
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import time
|
||||||
|
import json
|
||||||
|
from ssl import create_default_context
|
||||||
|
from statistics import mean
|
||||||
|
|
||||||
|
from flask import Flask, request
|
||||||
|
from flask_restful import Resource, Api
|
||||||
|
|
||||||
|
# import pytz
|
||||||
|
# import requests
|
||||||
|
from nio import AsyncClient
|
||||||
|
|
||||||
|
logging.basicConfig(level=logging.INFO)
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
api = Api(app)
|
||||||
|
|
||||||
|
MATRIX_HOMESERVER_URL = os.environ.get("MATRIX_HOMESERVER_URL")
|
||||||
|
MATRIX_USER = os.environ.get("MATRIX_USER")
|
||||||
|
MATRIX_PASSWORD = os.environ.get("MATRIX_PASSWORD")
|
||||||
|
MATRIX_ROOM_ID = os.environ.get("MATRIX_ROOM_ID")
|
||||||
|
|
||||||
|
|
||||||
|
async def send_message(msg, room_id):
|
||||||
|
client = AsyncClient(
|
||||||
|
MATRIX_HOMESERVER_URL,
|
||||||
|
MATRIX_USER
|
||||||
|
)
|
||||||
|
await client.login(MATRIX_PASSWORD)
|
||||||
|
await client.room_send(
|
||||||
|
room_id=room_id,
|
||||||
|
message_type="m.room.message",
|
||||||
|
content={"msgtype": "m.text", "body": msg},
|
||||||
|
)
|
||||||
|
await client.close()
|
||||||
|
|
||||||
|
|
||||||
|
def publish(msg, room_id):
|
||||||
|
asyncio.get_event_loop().run_until_complete(send_message(msg, room_id))
|
||||||
|
|
||||||
|
|
||||||
|
# def now():
|
||||||
|
# return (
|
||||||
|
# pytz.utc.localize(datetime.datetime.utcnow())
|
||||||
|
# .astimezone(pytz.timezone("Europe/Brussels"))
|
||||||
|
# .isoformat()
|
||||||
|
# )
|
||||||
|
|
||||||
|
|
||||||
|
class PostEndpoint(Resource):
|
||||||
|
def post(self):
|
||||||
|
logging.info(request.data)
|
||||||
|
data = json.loads(request.data)
|
||||||
|
msg = f"{data.get('title', '<no title>')}: {data.get('message', '<no message>')}"
|
||||||
|
publish(msg, MATRIX_ROOM_ID)
|
||||||
|
return None, 204
|
||||||
|
|
||||||
|
|
||||||
|
api.add_resource(PostEndpoint, '/')
|
||||||
4
bridge.sh
Normal file
4
bridge.sh
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
cd /app || exit
|
||||||
|
gunicorn --workers=2 --bind=0.0.0.0:8000 bridge:app
|
||||||
4
requirements.txt
Normal file
4
requirements.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
Flask==1.1.2
|
||||||
|
Flask-RESTful==0.3.8
|
||||||
|
gunicorn==20.1.0
|
||||||
|
matrix-nio==0.17.0
|
||||||
Reference in New Issue
Block a user