summaryrefslogtreecommitdiffstats
path: root/scripts/telegram-service-notification.sh
diff options
context:
space:
mode:
Diffstat (limited to 'scripts/telegram-service-notification.sh')
-rwxr-xr-xscripts/telegram-service-notification.sh92
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"