#!/bin/bash # Based on original E-Mail Icinga2 notification PROG="$(basename $0)" ICINGA2HOST="$(hostname)" CURLBIN="curl" MX_TXN="$(date "+%s")$((RANDOM % 9999))" if [ -z "$(which $CURLBIN)" ]; then echo "$CURLBIN not found in \$PATH. Consider installing it." exit 1 fi 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 ## Function helpers Usage() { cat <&2 Error ;; :) echo "Missing option argument for -$OPTARG" >&2 Error ;; *) echo "Unimplemented option: -$OPTARG" >&2 Error ;; esac done shift $((OPTIND - 1)) ## Check required parameters (TODO: better error message) if [ ! "$LONGDATETIME" ] || [ ! "$HOSTNAME" ] || [ ! "$HOSTDISPLAYNAME" ] || [ ! "$HOSTOUTPUT" ] || [ ! "$HOSTSTATE" ] || [ ! "$NOTIFICATIONTYPE" ]; then Error "Requirement parameters are missing." fi ## Build the notification message if [ "$HOSTSTATE" = "UP" ]; then ICON=$ok_ico elif [ "$HOSTSTATE" = "DOWN" ]; then ICON=$error_ico fi if [ "$HOSTSTATE" = "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 NOTIFICATION_MESSAGE=$( cat <HOST: $HOSTDISPLAYNAME is $HOSTSTATE!
When: $LONGDATETIME
Info: $HOSTOUTPUT
EOF ) ## Check whether IPv4 was specified. if [ -n "$HOSTADDRESS" ]; then NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE IPv4: $HOSTADDRESS
" fi ## Check whether IPv6 was specified. if [ -n "$HOSTADDRESS6" ]; then NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE IPv6: $HOSTADDRESS6
" fi ## Check whether author and comment was specified. if [ -n "$NOTIFICATIONCOMMENT" ]; then NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE Comment by $NOTIFICATIONAUTHORNAME: $NOTIFICATIONCOMMENT
" fi ## Check whether Icinga Web 2 URL was specified. if [ -n "$ICINGAWEB2URL" ]; then NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE $ICINGAWEB2URL/monitoring/host/show?host=$HOSTNAME
" fi while read line; do message="${message}\n${line}" done <<<$NOTIFICATION_MESSAGE BODY="${message}" /usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" | $CURLBIN -k -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d "{ \"msgtype\": \"m.text\", \"body\": \"$BODY\", \"formatted_body\": \"$BODY\", \"format\": \"org.matrix.custom.html\" }" "$MATRIXSERVER/_matrix/client/r0/rooms/$MATRIXROOM/send/m.room.message/$MX_TXN?access_token=$MATRIXTOKEN"