#!/usr/bin/env 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 ## Function helpers Usage() { cat <&2 Usage ;; :) echo "Missing option argument for -$OPTARG" >&2 Usage ;; *) echo "Unimplemented option: -$OPTARG" >&2 Usage ;; esac done shift $((OPTIND - 1)) echo "$LONGDATETIME $HOSTNAME $HOSTDISPLAYNAME $SERVICENAME $SERVICEDISPLAYNAME $SERVICEOUTPUT $SERVICESTATE $NOTIFICATIONTYPE" ## Check required parameters (TODO: better error message) if [ ! "$LONGDATETIME" ] || [ ! "$HOSTNAME" ] || [ ! "$HOSTDISPLAYNAME" ] || [ ! "$SERVICENAME" ] || [ ! "$SERVICEDISPLAYNAME" ] || [ ! "$SERVICEOUTPUT" ] || [ ! "$SERVICESTATE" ] || [ ! "$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 [ "$SERVICESTATE" = "UNKNOWN" ]; then ICON=$question_ico elif [ "$SERVICESTATE" = "OK" ]; then ICON=$ok_ico elif [ "$SERVICESTATE" = "WARNING" ]; then ICON=$warn_ico elif [ "$SERVICESTATE" = "CRITICAL" ]; then ICON=$error_ico fi NOTIFICATION_MESSAGE=$( cat <<-EOF $ICON Service: $SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE.
When: $LONGDATETIME.
Info: $SERVICEOUTPUT
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 # Replace space with HTML SERVICENAME=${SERVICENAME// /%20} NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE $ICINGAWEB2URL/monitoring/service/show?host=$HOSTNAME&service=$SERVICENAME
" 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"