Icinga notifications in Matrix
We just added Matrix notifications to an old Icinga server we have, because why not? We love having everything in Matrix...
To get this done, we borrowed heavily from this post.
First of all, the script. This is what sends the actual message to a Matrix room:
#!/bin/bash

# /usr/local/bin/matrix-icinga.sh
# Pipe the message to send into stdin

# Matrix items:
MX_TOKEN=[Set token for a Matrix user here]
MX_SERVER=https://[Your matrix server url]
MX_ROOM=[Matrix room ID, not alias]

MX_TXN="`date "+%s"`$(( RANDOM % 9999 ))"

warn_ico="âš "
error_ico="❌"
ok_ico="?"
question_ico="âť“"

#Set the message icon based on service state
# For nagios, replace ICINGA_ with NAGIOS_ to get the environment variables from Nagios
if [ "$ICINGA_HOSTSTATE" = "UP" ]
then
ICON=$ok_ico
elif [ "$ICINGA_HOSTSTATE" = "DOWN" ]
then
ICON=$error_ico
fi
if [ "$ICINGA_SERVICESTATE" = "UNKNOWN" ]
then
ICON=$question_ico
elif [ "$ICINGA_SERVICESTATE" = "OK" ]
then
ICON=$ok_ico
elif [ "$ICINGA_SERVICESTATE" = "WARNING" ]
then
ICON=$warn_ico
elif [ "$ICINGA_SERVICESTATE" = "CRITICAL" ]
then
ICON=$error_ico
fi

MY_HOSTNAME=`hostname`
read message
while read line; do
message="${message}\n${line}"
done

BODY="${ICON} HOST: ${ICINGA_HOSTNAME} SERVICE: ${ICINGA_SERVICEDISPLAYNAME} \
MESSAGE: ${ICINGA_SERVICEOUTPUT} ${ICINGA_HOSTOUTPUT} \
https://${MY_HOSTNAME}/cgi-bin/icinga/status.cgi?host=${ICINGA_HOSTNAME} ${message}"

# Post into maint room
curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "{
\"msgtype\": \"m.text\",
\"body\": \"$BODY\"
}" "$MX_SERVER/_matrix/client/unstable/rooms/$MX_ROOM/send/m.room.message/$MX_TXN?access_token=$MX_TOKEN"


... Next, set up the commands for host and service notifications. On our system, commands are in /etc/icinga/commands.cfg.
###############################
# Matrix notifications
###############################

# 'notify-service-by-matrix' command definition
define command {
command_name notify-service-by-matrix
command_line /usr/bin/printf "%b" "Notification Type: $NOTIFICATIONTYPE$ State: $SERVICESTATE$ \
Date/Time: $LONGDATETIME$" | /usr/local/bin/matrix_icinga.sh $NOTIFICATIONTYPE$ >> /tmp/matrix.log
}

# 'notify-host-by-matrix' command definition
define command {
command_name notify-host-by-matrix
command_line /usr/bin/printf "%b" "Notification Type: $NOTIFICATIONTYPE$\n\nState: \
$HOSTSTATE$\nDate/Time: $LONGDATETIME$\n" | /usr/local/bin/matrix_icinga.sh $NOTIFICATIONTYPES$ \
>> /tmp/matrix.log
}

Next, add a "Matrix" contact. In our system, this is in /etc/icinga/objects/contacts_icinga.cfg:
define contact {
contact_name matrix
alias Matrix
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,r
service_notification_commands notify-service-by-matrix
host_notification_commands notify-host-by-matrix
}


Finally, add to an active contact group to finish the setup. In our system, using check_mk, this ended up being /etc/icinga/objects/check_mk/check_mk_templates.cfg:
define contactgroup {
contactgroup_name check_mk
alias check_mk dummy contact group
members check_mk,matrix
}


After updating check_mk and reloading, notifications now deliver to the Matrix room!

Add new comment

The content of this field is kept private and will not be shown publicly.

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <blockquote cite> <cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h1> <h2 id> <h3 id> <h4 id> <h5 id> <p> <br> <img src alt height width>
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.