6
0
Fork 0

Add type __matrix_alertmanager_receiver

This commit is contained in:
Timothée Floure 2021-04-01 10:00:14 +02:00
parent 01e99e110f
commit 6957edfc1e
Signed by: tfloure
GPG Key ID: 4502C902C00A1E12
11 changed files with 204 additions and 0 deletions

View File

@ -0,0 +1,10 @@
#!/bin/sh
cat << EOF
Homeserver = "${HOMESERVER:?}"
TargetRoomID = "${ROOM_ID:?}"
MXID = "${USER_ID:?}"
MXToken = "${USER_TOKEN:?}"
HTTPPort = ${LISTENING_PORT:?}
HTTPAddress = "${LISTENING_HOST:?}"
EOF

View File

@ -0,0 +1,7 @@
#!/sbin/openrc-run
name="matrix-alertmanager-receiver"
description="Forwards Prometheus Alerts to a Matrix room"
command=/usr/local/bin/matrix-alertmanager-receiver
command_background=true
command_user="prometheus:prometheus"
pidfile=/run/matrix-alertmanager-receiver.pid

View File

@ -0,0 +1,11 @@
[Unit]
Description=Sends Prometheus Alerts to a Matrix room
After=network.target
[Service]
Type=simple
User=prometheus
ExecStart=/usr/local/bin/matrix-alertmanager-receiver
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,33 @@
#!/bin/sh
UPSTREAM=https://git.sr.ht/~fnux/matrix-alertmanager-receiver
VERSION=0.1.1
# Fetch, build and install binary.
cat << EOF
if ! command -v matrix-alertmanager-receiver; then
workdir=\$(mktemp -d)
cd \$workdir
# Fetch source archive.
curl "$UPSTREAM/archive/$VERSION.tar.gz" -o "$VERSION.tar.gz"
tar xf "$VERSION.tar.gz"
cd "matrix-alertmanager-receiver-$VERSION"
# Build binary.
go build -v
# Install binary.
install -m 0755 matrix-alertmanager-receiver /usr/local/bin/
# Cleanup build directory.
rm -r \$workdir
# Start service.
service matrix-alertmanager-receiver start
fi
EOF
if grep -q "^__file/etc/matrix-alertmanager-receiver.toml:" "${__messages_in:?}"; then
echo "service matrix-alertmanager-receiver restart"
fi

View File

@ -0,0 +1,70 @@
cdist-type__matrix_alertmanager_receiver(7)
=====================
NAME
----
cdist-type__matrix_alertmanager_receiver - Deploy a simple Prometheus Alertmanager-to-Matrix service
DESCRIPTION
-----------
This (singleton) type deploys `matrix-alertmanager-receiver <https://git.sr.ht/~fnux/matrix-alertmanager-receiver>`_, a simple daemon allowing Prometheus alerts to be sent to a `Matrix <https://matrix/>`_ room.
REQUIRED PARAMETERS
-------------------
homeserver
Address of the matrix homeserver to use.
room-id
ID of the (Matrix) room receiving alerts. Note that it must be the room ID:
an alias won't work!
user-id
MXID of the (Matrix) user sending alerts.
user-token
Token of the (Matrix) user sending alerts. Can be found under
Settings/Advanced in the Element web client.
OPTIONAL PARAMETERS
-------------------
host
Host to bind, defaults to `localhost`.
port
Port to listen on, defaults to `9088`.
BOOLEAN PARAMETERS
------------------
None.
EXAMPLES
--------
.. code-block:: sh
__matrix_alertmanager_receiver \
--homeserver 'https://matrix.domain.tld/' \
--room-id '!patatra:domain.tld' \
--user-id '@alerts:domain.tls' \
--user-token 'secretsecretsecret' \
--host 'localhost' \
--port 9088
SEE ALSO
--------
- `Upstream <https://git.sr.ht/~fnux/matrix-alertmanager-receiver>`_
- `__prometheus_alertmanager(7)`
AUTHORS
-------
Timothée Floure <t.floure@e-durable.ch>
COPYING
-------
Copyright \(C) 2020 Timothée Floure. You can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

View File

@ -0,0 +1,65 @@
#!/bin/sh
os="$(cat "${__global:?}/explorer/os")"
init="$(cat "${__global:?}/explorer/init")"
case "$os" in
alpine)
__package go
__package prometheus
__package curl
;;
debian|ubuntu)
__package golang
__package prometheus
__package curl
;;
*)
echo "This type does not support $os. Exiting." >&2
exit 1
;;
esac
# Type parameters.
HOMESERVER=$(cat "${__object:?}/parameter/homeserver")
ROOM_ID=$(cat "${__object:?}/parameter/room-id")
USER_ID=$(cat "${__object:?}/parameter/user-id")
USER_TOKEN=$(cat "${__object:?}/parameter/user-token")
export HOMESERVER ROOM_ID USER_ID USER_TOKEN
LISTENING_HOST=$(cat "${__object:?}/parameter/host")
LISTENING_PORT=$(cat "${__object:?}/parameter/port")
export LISTENING_PORT LISTENING_HOST
# Generate and deploy configuration
mkdir -p "${__object:?}/files"
"${__type:?}/files/matrix-alertmanager-receiver.toml.sh" > \
"${__object:?}/files/matrix-alertmanager-receiver.toml"
require="__package/prometheus" __file /etc/matrix-alertmanager-receiver.toml \
--source "${__object:?}/files/matrix-alertmanager-receiver.toml" \
--owner prometheus \
--mode 0644
case "$init" in
systemd)
unit_path='/etc/systemd/system/matrix-alertmanager-receiver.service'
__file "$unit_path" \
--source "${__type:?}/files/systemd-unit" \
--mode 0644
;;
busybox-init+openrc)
unit_path='/etc/init.d/matrix-alertmanager-receiver'
__file "$unit_path" \
--source "${__type:?}/files/openrc-unit" \
--owner prometheus \
--mode 0755
;;
*)
echo "Unknown init: $init" >&2
exit 1
;;
esac
require="__file/$unit_path" \
__start_on_boot matrix-alertmanager-receiver

View File

@ -0,0 +1 @@
localhost

View File

@ -0,0 +1 @@
9088

View File

@ -0,0 +1,2 @@
host
port

View File

@ -0,0 +1,4 @@
homeserver
room-id
user-id
user-token