Support for matrix notifications (#9196)
David Heidelberg committed
Jun 8, 2020 at 10:02 UTC
dc229f7ed744f7b1b5dcdcea4038360dae112ff2
5 files changed
+156
-1
health/notifications/Makefile.am
+1
@@ -45,4 +45,5 @@ include syslog/Makefile.inc
45
include telegram/Makefile.inc
46
include twilio/Makefile.inc
47
include web/Makefile.inc
48
+include matrix/Makefile.inc
49
include custom/Makefile.inc
health/notifications/alarm-notify.sh.in
+63
@@ -172,6 +172,7 @@ rocketchat
172
sms
173
hangouts
174
dynatrace
175
+matrix
176
"
177
178
# -----------------------------------------------------------------------------
@@ -351,6 +352,10 @@ FLEEP_SENDER="${host}"
352
# Amazon SNS configs
353
AWSSNS_MESSAGE_FORMAT=
354
355
+# Matrix configs
356
+MATRIX_HOMESERVER=
357
+MATRIX_ACCESSTOKEN=
358
+
359
# syslog configs
360
SYSLOG_FACILITY=
361
@@ -522,6 +527,8 @@ filter_recipient_by_criticality() {
527
[ -z "${DYNATRACE_TAG_VALUE}" ] ||
528
[ -z "${DYNATRACE_EVENT}" ]; } && SEND_DYNATRACE="NO"
529
530
+# check matrix
531
+{ [ -z "${MATRIX_HOMESERVER}" ] || [ -z "${MATRIX_ACCESSTOKEN}" ]; } && SEND_MATRIX="NO"
532
533
if [ "${SEND_PUSHOVER}" = "YES" ] ||
534
[ "${SEND_SLACK}" = "YES" ] ||
@@ -540,6 +547,7 @@ if [ "${SEND_PUSHOVER}" = "YES" ] ||
547
[ "${SEND_FLEEP}" = "YES" ] ||
548
[ "${SEND_PROWL}" = "YES" ] ||
549
[ "${SEND_HANGOUTS}" = "YES" ] ||
550
+ [ "${SEND_MATRIX}" = "YES" ] ||
551
[ "${SEND_CUSTOM}" = "YES" ] ||
552
[ "${SEND_MSTEAM}" = "YES" ] ||
553
[ "${SEND_DYNATRACE}" = "YES" ]; then
@@ -567,6 +575,7 @@ if [ "${SEND_PUSHOVER}" = "YES" ] ||
575
SEND_FLEEP="NO"
576
SEND_PROWL="NO"
577
SEND_HANGOUTS="NO"
578
+ SEND_MATRIX="NO"
579
SEND_CUSTOM="NO"
580
SEND_DYNATRACE="NO"
581
fi
@@ -689,6 +698,7 @@ for method in "${SEND_EMAIL}" \
698
"${SEND_PD}" \
699
"${SEND_FLEEP}" \
700
"${SEND_PROWL}" \
701
+ "${SEND_MATRIX}" \
702
"${SEND_CUSTOM}" \
703
"${SEND_IRC}" \
704
"${SEND_HANGOUTS}" \
@@ -1703,6 +1713,51 @@ send_awssns() {
1713
return 1
1714
}
1715
1716
+# -----------------------------------------------------------------------------
1717
+# Matrix sender
1718
+
1719
+send_matrix() {
1720
+ local homeserver="${1}" webhook accesstoken rooms="${2}" httpcode sent=0 payload
1721
+
1722
+ [ "${SEND_MATRIX}" != "YES" ] && return 1
1723
+ [ -z "${MATRIX_ACCESSTOKEN}" ] && return 1
1724
+
1725
+ accesstoken="${MATRIX_ACCESSTOKEN}"
1726
+
1727
+ case "${status}" in
1728
+ WARNING) emoji="⚠️" ;;
1729
+ CRITICAL) emoji="🔴" ;;
1730
+ CLEAR) emoji="✅" ;;
1731
+ *) emoji="⚪️" ;;
1732
+ esac
1733
+
1734
+ for room in ${rooms}; do
1735
+ webhook="$homeserver/_matrix/client/r0/rooms/$(urlencode $room)/send/m.room.message?access_token=$accesstoken"
1736
+ payload="$(
1737
+ cat <<EOF
1738
+ {
1739
+ "msgtype": "m.notice",
1740
+ "format": "org.matrix.custom.html",
1741
+ "formatted_body": "${emoji} ${host} ${status_message} - <b>${name//_/ }</b><br>${chart} (${family})<br><a href=\"${goto_url}\">${alarm}</a><br><i>${info}</i>",
1742
+ "body": "${emoji} ${host} ${status_message} - ${name//_/ } ${chart} (${family}) ${goto_url} ${alarm} ${info}"
1743
+ }
1744
+EOF
1745
+ )"
1746
+
1747
+ httpcode=$(docurl -X POST --data "${payload}" "${webhook}")
1748
+ if [ "${httpcode}" == "200" ]; then
1749
+ info "sent Matrix notification for: ${host} ${chart}.${name} is ${status} to '${room}'"
1750
+ sent=$((sent + 1))
1751
+ else
1752
+ error "failed to send Matrix notification for: ${host} ${chart}.${name} is ${status} to '${room}', with HTTP error code ${httpcode}."
1753
+ fi
1754
+ done
1755
+
1756
+ [ ${sent} -gt 0 ] && return 0
1757
+
1758
+ return 1
1759
+}
1760
+
1761
# -----------------------------------------------------------------------------
1762
# syslog sender
1763
@@ -2282,6 +2337,13 @@ send_awssns "${to_awssns}"
2337
2338
SENT_AWSSNS=$?
2339
2340
+# -----------------------------------------------------------------------------
2341
+# send the Matrix message
2342
+send_matrix "${MATRIX_HOMESERVER}" "${to_matrix}"
2343
+
2344
+SENT_MATRIX=$?
2345
+
2346
+
2347
# -----------------------------------------------------------------------------
2348
# send the syslog message
2349
@@ -2491,6 +2553,7 @@ for state in "${SENT_EMAIL}" \
2553
"${SENT_CUSTOM}" \
2554
"${SENT_IRC}" \
2555
"${SENT_AWSSNS}" \
2556
+ "${SENT_MATRIX}" \
2557
"${SENT_SYSLOG}" \
2558
"${SENT_SMS}" \
2559
"${SENT_MSTEAM}" \
health/notifications/health_alarm_notify.conf
+38
-1
@@ -776,6 +776,26 @@ SEND_SMS="YES"
776
777
DEFAULT_RECIPIENT_SMS=""
778
779
+# Matrix notifications
780
+#
781
+
782
+# enable/disable Matrix notifications
783
+SEND_MATRIX="YES"
784
+
785
+# The url of the Matrix homeserver
786
+# e.g https://matrix.org:8448
787
+MATRIX_HOMESERVER=
788
+
789
+# An access token from a valid Matrix account. Tokens usually don't expire,
790
+# can be controlled from a Matrix client.
791
+# See https://matrix.org/docs/guides/client-server.html
792
+MATRIX_ACCESSTOKEN=
793
+
794
+# Specify the default rooms to receive the notification if no rooms are provided
795
+# in a role's recipients.
796
+# The format is !roomid:homeservername
797
+DEFAULT_RECIPIENT_MATRIX=""
798
+
799
#------------------------------------------------------------------------------
800
# custom notifications
801
#
@@ -911,6 +931,9 @@ role_recipients_msteam[sysadmin]="${DEFAULT_RECIPIENT_MSTEAM}"
931
role_recipients_rocketchat[sysadmin]="${DEFAULT_RECIPIENT_ROCKETCHAT}"
932
933
role_recipients_dynatrace[sysadmin]="${DEFAULT_RECIPIENT_DYNATRACE}"
934
+
935
+role_recipients_matrix[sysadmin]="${DEFAULT_RECIPIENT_MATRIX}"
936
+
937
# -----------------------------------------------------------------------------
938
# DNS related alarms
939
@@ -961,6 +984,9 @@ role_recipients_rocketchat[domainadmin]="${DEFAULT_RECIPIENT_ROCKETCHAT}"
984
role_recipients_sms[domainadmin]="${DEFAULT_RECIPIENT_SMS}"
985
986
role_recipients_dynatrace[domainadmin]="${DEFAULT_RECIPIENT_DYNATRACE}"
987
+
988
+role_recipients_matrix[domainadmin]="${DEFAULT_RECIPIENT_MATRIX}"
989
+
990
# -----------------------------------------------------------------------------
991
# database servers alarms
992
# mysql, redis, memcached, postgres, etc
@@ -1012,6 +1038,9 @@ role_recipients_rocketchat[dba]="${DEFAULT_RECIPIENT_ROCKETCHAT}"
1038
role_recipients_sms[dba]="${DEFAULT_RECIPIENT_SMS}"
1039
1040
role_recipients_dynatrace[dba]="${DEFAULT_RECIPIENT_DYNATRACE}"
1041
+
1042
+role_recipients_matrix[dba]="${DEFAULT_RECIPIENT_MATRIX}"
1043
+
1044
# -----------------------------------------------------------------------------
1045
# web servers alarms
1046
# apache, nginx, lighttpd, etc
@@ -1063,6 +1092,9 @@ role_recipients_rocketchat[webmaster]="${DEFAULT_RECIPIENT_ROCKETCHAT}"
1092
role_recipients_sms[webmaster]="${DEFAULT_RECIPIENT_SMS}"
1093
1094
role_recipients_dynatrace[webmaster]="${DEFAULT_RECIPIENT_DYNATRACE}"
1095
+
1096
+role_recipients_matrix[webmaster]="${DEFAULT_RECIPIENT_MATRIX}"
1097
+
1098
# -----------------------------------------------------------------------------
1099
# proxy servers alarms
1100
# squid, etc
@@ -1114,6 +1146,9 @@ role_recipients_rocketchat[proxyadmin]="${DEFAULT_RECIPIENT_ROCKETCHAT}"
1146
role_recipients_sms[proxyadmin]="${DEFAULT_RECIPIENT_SMS}"
1147
1148
role_recipients_dynatrace[proxyadmin]="${DEFAULT_RECIPIENT_DYNATRACE}"
1149
+
1150
+role_recipients_matrix[proxyadmin]="${DEFAULT_RECIPIENT_MATRIX}"
1151
+
1152
# -----------------------------------------------------------------------------
1153
# peripheral devices
1154
# UPS, photovoltaics, etc
@@ -1162,4 +1197,6 @@ role_recipients_rocketchat[sitemgr]="${DEFAULT_RECIPIENT_ROCKETCHAT}"
1197
1198
role_recipients_sms[sitemgr]="${DEFAULT_RECIPIENT_SMS}"
1199
1165
-role_recipients_dynatrace[sitemgr]="${DEFAULT_RECIPIENT_DYNATRACE}"
\ No newline at end of file
1200
+role_recipients_dynatrace[sitemgr]="${DEFAULT_RECIPIENT_DYNATRACE}"
1201
+
1202
+role_recipients_matrix[sitemgr]="${DEFAULT_RECIPIENT_MATRIX}"
health/notifications/matrix/Makefile.inc
new
+12
@@ -0,0 +1,12 @@
1
+# SPDX-License-Identifier: GPL-3.0-or-later
2
+
3
+# THIS IS NOT A COMPLETE Makefile
4
+# IT IS INCLUDED BY ITS PARENT'S Makefile.am
5
+# IT IS REQUIRED TO REFERENCE ALL FILES RELATIVE TO THE PARENT
6
+
7
+# install these files
8
+dist_noinst_DATA += \
9
+ matrix/README.md \
10
+ matrix/Makefile.inc \
11
+ $(NULL)
12
+
health/notifications/matrix/README.md
new
+42
@@ -0,0 +1,42 @@
1
+# Matrix
2
+
3
+Send notifications to [Matrix](https://matrix.org/) network rooms.
4
+
5
+The requirements for this notification method are:
6
+1. The url of the homeserver (`https://homeserver:port`)
7
+2. Credentials for connecting to the homeserver, in the form of a valid access token for your account (or for a dedicated notification account). These tokens usually don't expire.
8
+3. The room ids that you want to sent the notification to.
9
+
10
+To obtain the access token you can use the following curl command:
11
+```
12
+curl -XPOST -d '{"type":"m.login.password", "user":"example", "password":"wordpass"}' "https://homeserver:8448/_matrix/client/r0/login"
13
+```
14
+
15
+The room ids are unique identifiers and can be obtained from the room settings in a Matrix client (e.g. Riot). Their format is !uniqueid:homeserver.
16
+Multiple room ids can be defined by separating with a space character.
17
+
18
+Detailed information about the Matrix client API is available at the official [site](https://matrix.org/docs/guides/client-server.html)
19
+
20
+Your health_alarm_notify.conf should look like this:
21
+```
22
+###############################################################################
23
+# Matrix notifications
24
+#
25
+
26
+# enable/disable Matrix notifications
27
+SEND_MATRIX="YES"
28
+
29
+# The url of the Matrix homeserver
30
+# e.g https://matrix.org:8448
31
+MATRIX_HOMESERVER="https://matrix.org:8448"
32
+
33
+# A access token from a valid Matrix account. Tokens usually don't expire,
34
+# can be controlled from a Matrix client.
35
+# See https://matrix.org/docs/guides/client-server.html
36
+MATRIX_ACCESSTOKEN="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
37
+
38
+# Specify the default rooms to receive the notification if no rooms are provided
39
+# in a role's recipients.
40
+# The format is !roomid:homeservername
41
+DEFAULT_RECIPIENT_MATRIX="!XXXXXXXXXXXX:matrix.org"
42
+```