diff options
| author | Tobias Wiese <tobias@tobiaswiese.com> | 2020-05-11 20:35:04 +0200 |
|---|---|---|
| committer | Tobias Wiese <tobias@tobiaswiese.com> | 2020-05-11 20:35:04 +0200 |
| commit | 50be889b75116ae5b36ea9113e79649df16c6986 (patch) | |
| tree | 2c9304d31cfa916c7add63147878c1be542a6d6e /scripts/telegram-service-notification.sh | |
| parent | 4c255bd3feafb976e0030b699807d0bb8d3957a2 (diff) | |
notifications: Notify via email and telegram
Provide notification in case of a state change via email and telegram
bot.
The telegram bot is especially helpful in cases where a problem with
email delivery is to be reportet.
Signed-off-by: Tobias Wiese <tobias@tobiaswiese.com>
Diffstat (limited to 'scripts/telegram-service-notification.sh')
| -rwxr-xr-x | scripts/telegram-service-notification.sh | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/scripts/telegram-service-notification.sh b/scripts/telegram-service-notification.sh new file mode 100755 index 0000000..c5815ec --- /dev/null +++ b/scripts/telegram-service-notification.sh @@ -0,0 +1,92 @@ +#!/bin/sh +#!/bin/sh +# (C) Tobias Wiese | GPLv2+ +# This file is based on mail-service-notification.sh from Icinga used under GPLv2+ license +# Except of function urlencode which is Copyright (C) by Brian White (brian@aljex.com) used under MIT license +# Changes: The script was adapted to send Telegram Messages instead of mail. + +PROG="$(basename $0)" +ICINGA2HOST="$(hostname)" + +if [ -z "$(type curl)" ] ; then + echo "curl not found in \$PATH. Consider installing it." 2>&1 + exit 1 +fi + +## Function helpers +urlencode() { + local LANG=C i=0 c e s="$1" + + while [ $i -lt ${#1} ]; do + [ "$i" -eq 0 ] || s="${s#?}" + c=${s%"${s#?}"} + [ -z "${c#[[:alnum:].~_-]}" ] || c=$(printf '%%%02X' "'$c") + e="${e}${c}" + i=$((i + 1)) + done + echo "$e" +} + +## Keep formatting in sync with telegram-host-notification.sh +for P in DATE HOSTNAME HOSTDISPLAYNAME SERVICENAME SERVICEDISPLAYNAME \ + SERVICEOUTPUT SERVICESTATE USERCHATID TYPE +do + eval "PAR=\$${P}" + + if [ ! "$PAR" ] ; then + echo "Required parameter '$P' is missing." >&2 + exit 1 + fi +done + +## Build the notification message +NOTIFICATION_MESSAGE=`cat << EOF +***** Service Monitoring on $ICINGA2HOST ***** + +$SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE! + +Info: $SERVICEOUTPUT + +When: $DATE +Service: $SERVICENAME +Host: $HOSTNAME +EOF +` + +## Check whether IPv4 was specified. +if [ -n "$ADDRESS" ] ; then + NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE +IPv4: $ADDRESS" +fi + +## Check whether IPv6 was specified. +if [ -n "$ADDRESS6" ] ; then + NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE +IPv6: $ADDRESS6" +fi + +## Check whether author and comment was specified. +if [ -n "$COMMENT" ] ; then + NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE + +Comment by $AUTHOR: + $COMMENT" +fi + +## Check whether Icinga Web 2 URL was specified. +if [ -n "$ICINGAWEB2URL" ] ; then + NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE + +$ICINGAWEB2URL/monitoring/service/show?host=$(urlencode "$HOSTNAME")&service=$(urlencode "$SERVICENAME")" +fi + +## Check whether verbose mode was enabled and log to syslog. +if [ "$VERBOSE" = "true" ] ; then + logger "$PROG sends Telegram => $CHATID" +fi + +## Send message via telegram +curl -X POST "https://api.telegram.org/bot${TOKEN}/sendMessage" \ + --data-urlencode "chat_id=$USERCHATID" \ + --data-urlencode "text=$NOTIFICATION_MESSAGE" \ + --data-urlencode "disable_web_page_preview=true" |
