ansible/roles/nagios/templates/notify-by-matrix

82 lines
1.7 KiB
Plaintext
Raw Permalink Normal View History

#! /bin/sh
#
# notify-by-matrix
# Copyright (C) 2021 Vintage Salt <rehashedsalt@cock.li>
#
# Distributed under terms of the MIT license.
#
set -e
# Set our Matrix-related vars here
MX_TOKEN="{{ nagios_matrix_token }}"
MX_SERVER="{{ nagios_matrix_server }}"
MX_ROOM="{{ nagios_matrix_room }}"
# Get a TXN to prefix this particular message with
MX_TXN="$(date "+%s")$(( RANDOM % 9999 ))"
# Read the first line from STDIN
# This is supposed to be the NOTIFICATIONTYPE
read notiftype
prefix=""
# https://assets.nagios.com/downloads/nagioscore/docs/nagioscore/3/en/macrolist.html#notificationtype
case "$notiftype" in
PROBLEM)
# Large Red Circle (U+1F534)
prefix="🔴"
;;
RECOVERY)
# Large Green Circle (U+1F7E2)
prefix="🟢"
;;
ACKNOWLEDGEMENT)
# Symbol For Acknowledge (U+2406)
prefix="␆"
;;
FLAPPINGSTART)
# Large Orange Circle (U+1F7E0)
prefix="🟠"
;;
FLAPPINGSTOP)
# Large Green Circle (U+1F7E2)
prefix="🟢"
;;
FLAPPINGDISABLED)
# Bell with Cancellation Stroke (U+1F515)
prefix="🔕"
;;
DOWNTIMESTART)
# Bell with Cancellation Stroke (U+1F515)
prefix="🔕"
;;
DOWNTIMEEND)
# Bell (U+1F514)
prefix="🔔"
;;
DOWNTIMECANCELLED)
# Bell (U+1F514)
prefix="🔔"
;;
*)
prefix="$notiftype - "
;;
esac
# Read a message from STDIN
# NOTE: This is dangerous and stupid and unsanitized
read message
while read line; do
message="${message}\n${line}"
done
# Push it to the channel
curl -X PUT \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
-d "{
\"msgtype\": \"m.text\",
\"body\": \"$prefix $message\"
}" \
"$MX_SERVER/_matrix/client/unstable/rooms/$MX_ROOM/send/m.room.message/$MX_TXN?access_token=$MX_TOKEN"