diff options
author | David Heidelberg <david@ixit.cz> | 2020-06-08 10:02:48 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-08 11:02:48 +0300 |
commit | dc229f7ed744f7b1b5dcdcea4038360dae112ff2 (patch) | |
tree | 3275f7d9ef660e1ff2169eab07143f87bd79cde5 /health/notifications | |
parent | b400a1cfbcc1482fe2155290df2d136c55723343 (diff) |
Support for matrix notifications (#9196)
Diffstat (limited to 'health/notifications')
-rw-r--r-- | health/notifications/Makefile.am | 1 | ||||
-rwxr-xr-x | health/notifications/alarm-notify.sh.in | 63 | ||||
-rwxr-xr-x | health/notifications/health_alarm_notify.conf | 39 | ||||
-rw-r--r-- | health/notifications/matrix/Makefile.inc | 12 | ||||
-rw-r--r-- | health/notifications/matrix/README.md | 42 |
5 files changed, 156 insertions, 1 deletions
diff --git a/health/notifications/Makefile.am b/health/notifications/Makefile.am index 606770fd05..a0f0d82d6a 100644 --- a/health/notifications/Makefile.am +++ b/health/notifications/Makefile.am @@ -45,4 +45,5 @@ include syslog/Makefile.inc include telegram/Makefile.inc include twilio/Makefile.inc include web/Makefile.inc +include matrix/Makefile.inc include custom/Makefile.inc diff --git a/health/notifications/alarm-notify.sh.in b/health/notifications/alarm-notify.sh.in index 375cc0771f..9891cc0a22 100755 --- a/health/notifications/alarm-notify.sh.in +++ b/health/notifications/alarm-notify.sh.in @@ -172,6 +172,7 @@ rocketchat sms hangouts dynatrace +matrix " # ----------------------------------------------------------------------------- @@ -351,6 +352,10 @@ FLEEP_SENDER="${host}" # Amazon SNS configs AWSSNS_MESSAGE_FORMAT= +# Matrix configs +MATRIX_HOMESERVER= +MATRIX_ACCESSTOKEN= + # syslog configs SYSLOG_FACILITY= @@ -522,6 +527,8 @@ filter_recipient_by_criticality() { [ -z "${DYNATRACE_TAG_VALUE}" ] || [ -z "${DYNATRACE_EVENT}" ]; } && SEND_DYNATRACE="NO" +# check matrix +{ [ -z "${MATRIX_HOMESERVER}" ] || [ -z "${MATRIX_ACCESSTOKEN}" ]; } && SEND_MATRIX="NO" if [ "${SEND_PUSHOVER}" = "YES" ] || [ "${SEND_SLACK}" = "YES" ] || @@ -540,6 +547,7 @@ if [ "${SEND_PUSHOVER}" = "YES" ] || [ "${SEND_FLEEP}" = "YES" ] || [ "${SEND_PROWL}" = "YES" ] || [ "${SEND_HANGOUTS}" = "YES" ] || + [ "${SEND_MATRIX}" = "YES" ] || [ "${SEND_CUSTOM}" = "YES" ] || [ "${SEND_MSTEAM}" = "YES" ] || [ "${SEND_DYNATRACE}" = "YES" ]; then @@ -567,6 +575,7 @@ if [ "${SEND_PUSHOVER}" = "YES" ] || SEND_FLEEP="NO" SEND_PROWL="NO" SEND_HANGOUTS="NO" + SEND_MATRIX="NO" SEND_CUSTOM="NO" SEND_DYNATRACE="NO" fi @@ -689,6 +698,7 @@ for method in "${SEND_EMAIL}" \ "${SEND_PD}" \ "${SEND_FLEEP}" \ "${SEND_PROWL}" \ + "${SEND_MATRIX}" \ "${SEND_CUSTOM}" \ "${SEND_IRC}" \ "${SEND_HANGOUTS}" \ @@ -1704,6 +1714,51 @@ send_awssns() { } # ----------------------------------------------------------------------------- +# Matrix sender + +send_matrix() { + local homeserver="${1}" webhook accesstoken rooms="${2}" httpcode sent=0 payload + + [ "${SEND_MATRIX}" != "YES" ] && return 1 + [ -z "${MATRIX_ACCESSTOKEN}" ] && return 1 + + accesstoken="${MATRIX_ACCESSTOKEN}" + + case "${status}" in + WARNING) emoji="⚠️" ;; + CRITICAL) emoji="🔴" ;; + CLEAR) emoji="✅" ;; + *) emoji="⚪️" ;; + esac + + for room in ${rooms}; do + webhook="$homeserver/_matrix/client/r0/rooms/$(urlencode $room)/send/m.room.message?access_token=$accesstoken" + payload="$( + cat <<EOF + { + "msgtype": "m.notice", + "format": "org.matrix.custom.html", + "formatted_body": "${emoji} ${host} ${status_message} - <b>${name//_/ }</b><br>${chart} (${family})<br><a href=\"${goto_url}\">${alarm}</a><br><i>${info}</i>", + "body": "${emoji} ${host} ${status_message} - ${name//_/ } ${chart} (${family}) ${goto_url} ${alarm} ${info}" + } +EOF + )" + + httpcode=$(docurl -X POST --data "${payload}" "${webhook}") + if [ "${httpcode}" == "200" ]; then + info "sent Matrix notification for: ${host} ${chart}.${name} is ${status} to '${room}'" + sent=$((sent + 1)) + else + error "failed to send Matrix notification for: ${host} ${chart}.${name} is ${status} to '${room}', with HTTP error code ${httpcode}." + fi + done + + [ ${sent} -gt 0 ] && return 0 + + return 1 +} + +# ----------------------------------------------------------------------------- # syslog sender send_syslog() { @@ -2283,6 +2338,13 @@ send_awssns "${to_awssns}" SENT_AWSSNS=$? # ----------------------------------------------------------------------------- +# send the Matrix message +send_matrix "${MATRIX_HOMESERVER}" "${to_matrix}" + +SENT_MATRIX=$? + + +# ----------------------------------------------------------------------------- # send the syslog message send_syslog "${to_syslog}" @@ -2491,6 +2553,7 @@ for state in "${SENT_EMAIL}" \ "${SENT_CUSTOM}" \ "${SENT_IRC}" \ "${SENT_AWSSNS}" \ + "${SENT_MATRIX}" \ "${SENT_SYSLOG}" \ "${SENT_SMS}" \ "${SENT_MSTEAM}" \ diff --git a/health/notifications/health_alarm_notify.conf b/health/notifications/health_alarm_notify.conf index 7baffb68b1..00dbde9c27 100755 --- a/health/notifications/health_alarm_notify.conf +++ b/health/notifications/health_alarm_notify.conf @@ -776,6 +776,26 @@ SEND_SMS="YES" DEFAULT_RECIPIENT_SMS="" +# Matrix notifications +# + +# enable/disable Matrix notifications +SEND_MATRIX="YES" + +# The url of the Matrix homeserver +# e.g https://matrix.org:8448 +MATRIX_HOMESERVER= + +# An access token from a valid Matrix account. Tokens usually don't expire, +# can be controlled from a Matrix client. +# See https://matrix.org/docs/guides/client-server.html +MATRIX_ACCESSTOKEN= + +# Specify the default rooms to receive the notification if no rooms are provided +# in a role's recipients. +# The format is !roomid:homeservername +DEFAULT_RECIPIENT_MATRIX="" + #------------------------------------------------------------------------------ # custom notifications # @@ -911,6 +931,9 @@ role_recipients_msteam[sysadmin]="${DEFAULT_RECIPIENT_MSTEAM}" role_recipients_rocketchat[sysadmin]="${DEFAULT_RECIPIENT_ROCKETCHAT}" role_recipients_dynatrace[sysadmin]="${DEFAULT_RECIPIENT_DYNATRACE}" + +role_recipients_matrix[sysadmin]="${DEFAULT_RECIPIENT_MATRIX}" + # ----------------------------------------------------------------------------- # DNS related alarms @@ -961,6 +984,9 @@ role_recipients_rocketchat[domainadmin]="${DEFAULT_RECIPIENT_ROCKETCHAT}" role_recipients_sms[domainadmin]="${DEFAULT_RECIPIENT_SMS}" role_recipients_dynatrace[domainadmin]="${DEFAULT_RECIPIENT_DYNATRACE}" + +role_recipients_matrix[domainadmin]="${DEFAULT_RECIPIENT_MATRIX}" + # ----------------------------------------------------------------------------- # database servers alarms # mysql, redis, memcached, postgres, etc @@ -1012,6 +1038,9 @@ role_recipients_rocketchat[dba]="${DEFAULT_RECIPIENT_ROCKETCHAT}" role_recipients_sms[dba]="${DEFAULT_RECIPIENT_SMS}" role_recipients_dynatrace[dba]="${DEFAULT_RECIPIENT_DYNATRACE}" + +role_recipients_matrix[dba]="${DEFAULT_RECIPIENT_MATRIX}" + # ----------------------------------------------------------------------------- # web servers alarms # apache, nginx, lighttpd, etc @@ -1063,6 +1092,9 @@ role_recipients_rocketchat[webmaster]="${DEFAULT_RECIPIENT_ROCKETCHAT}" role_recipients_sms[webmaster]="${DEFAULT_RECIPIENT_SMS}" role_recipients_dynatrace[webmaster]="${DEFAULT_RECIPIENT_DYNATRACE}" + +role_recipients_matrix[webmaster]="${DEFAULT_RECIPIENT_MATRIX}" + # ----------------------------------------------------------------------------- # proxy servers alarms # squid, etc @@ -1114,6 +1146,9 @@ role_recipients_rocketchat[proxyadmin]="${DEFAULT_RECIPIENT_ROCKETCHAT}" role_recipients_sms[proxyadmin]="${DEFAULT_RECIPIENT_SMS}" role_recipients_dynatrace[proxyadmin]="${DEFAULT_RECIPIENT_DYNATRACE}" + +role_recipients_matrix[proxyadmin]="${DEFAULT_RECIPIENT_MATRIX}" + # ----------------------------------------------------------------------------- # peripheral devices # UPS, photovoltaics, etc @@ -1162,4 +1197,6 @@ role_recipients_rocketchat[sitemgr]="${DEFAULT_RECIPIENT_ROCKETCHAT}" role_recipients_sms[sitemgr]="${DEFAULT_RECIPIENT_SMS}" -role_recipients_dynatrace[sitemgr]="${DEFAULT_RECIPIENT_DYNATRACE}"
\ No newline at end of file +role_recipients_dynatrace[sitemgr]="${DEFAULT_RECIPIENT_DYNATRACE}" + +role_recipients_matrix[sitemgr]="${DEFAULT_RECIPIENT_MATRIX}" diff --git a/health/notifications/matrix/Makefile.inc b/health/notifications/matrix/Makefile.inc new file mode 100644 index 0000000000..9937d80c3f --- /dev/null +++ b/health/notifications/matrix/Makefile.inc @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: GPL-3.0-or-later + +# THIS IS NOT A COMPLETE Makefile +# IT IS INCLUDED BY ITS PARENT'S Makefile.am +# IT IS REQUIRED TO REFERENCE ALL FILES RELATIVE TO THE PARENT + +# install these files +dist_noinst_DATA += \ + matrix/README.md \ + matrix/Makefile.inc \ + $(NULL) + diff --git a/health/notifications/matrix/README.md b/health/notifications/matrix/README.md new file mode 100644 index 0000000000..9be28ec282 --- /dev/null +++ b/health/notifications/matrix/README.md @@ -0,0 +1,42 @@ +# Matrix + +Send notifications to [Matrix](https://matrix.org/) network rooms. + +The requirements for this notification method are: +1. The url of the homeserver (`https://homeserver:port`) +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. +3. The room ids that you want to sent the notification to. + +To obtain the access token you can use the following curl command: +``` +curl -XPOST -d '{"type":"m.login.password", "user":"example", "password":"wordpass"}' "https://homeserver:8448/_matrix/client/r0/login" +``` + +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. +Multiple room ids can be defined by separating with a space character. + +Detailed information about the Matrix client API is available at the official [site](https://matrix.org/docs/guides/client-server.html) + +Your health_alarm_notify.conf should look like this: +``` +############################################################################### +# Matrix notifications +# + +# enable/disable Matrix notifications +SEND_MATRIX="YES" + +# The url of the Matrix homeserver +# e.g https://matrix.org:8448 +MATRIX_HOMESERVER="https://matrix.org:8448" + +# A access token from a valid Matrix account. Tokens usually don't expire, +# can be controlled from a Matrix client. +# See https://matrix.org/docs/guides/client-server.html +MATRIX_ACCESSTOKEN="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + +# Specify the default rooms to receive the notification if no rooms are provided +# in a role's recipients. +# The format is !roomid:homeservername +DEFAULT_RECIPIENT_MATRIX="!XXXXXXXXXXXX:matrix.org" +``` |