#!/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 [$SERVICESTATE] $SERVICEDISPLAYNAME on $HOSTDISPLAYNAME $SERVICEOUTPUT EOF ` ## 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"