summaryrefslogtreecommitdiffstats
path: root/scripts/telegram-host-notification.sh
diff options
context:
space:
mode:
authorTobias Wiese <tobias@tobiaswiese.com>2020-05-11 20:35:04 +0200
committerTobias Wiese <tobias@tobiaswiese.com>2020-05-11 20:35:04 +0200
commit50be889b75116ae5b36ea9113e79649df16c6986 (patch)
tree2c9304d31cfa916c7add63147878c1be542a6d6e /scripts/telegram-host-notification.sh
parent4c255bd3feafb976e0030b699807d0bb8d3957a2 (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-host-notification.sh')
-rwxr-xr-xscripts/telegram-host-notification.sh89
1 files changed, 89 insertions, 0 deletions
diff --git a/scripts/telegram-host-notification.sh b/scripts/telegram-host-notification.sh
new file mode 100755
index 0000000..5d42709
--- /dev/null
+++ b/scripts/telegram-host-notification.sh
@@ -0,0 +1,89 @@
+#!/bin/sh
+# (C) Tobias Wiese | GPLv2+
+# This file is based on mail-host-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-service-notification.sh
+for P in DATE HOSTNAME HOSTDISPLAYNAME HOSTOUTPUT HOSTSTATE 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
+***** Host Monitoring on $ICINGA2HOST *****
+
+$HOSTDISPLAYNAME is $HOSTSTATE!
+
+Info: $HOSTOUTPUT
+
+When: $DATE
+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/host/show?host=$(urlencode "$HOSTNAME")"
+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"