#!/usr/bin/env bash ## /etc/icinga2/scripts/service-by-telegram.sh / 20170330 ## Last updated 20190820 ## Last tests used icinga2-2.11.2-1.buster # Copyright (C) 2018 Marianne M. Spiller # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . PROG="`basename $0`" HOSTNAME="`hostname`" TRANSPORT="curl" unset DEBUG if [ -z "`which $TRANSPORT`" ] ; then echo "$TRANSPORT not in \$PATH. Consider installing it." exit 1 fi Usage() { cat << EOF service-by-telegram notification script for Icinga 2 by spillerm The following are mandatory: -4 HOSTADDRESS (\$address$) -6 HOSTADDRESS6 (\$address6$) -d LONGDATETIME (\$icinga.long_date_time$) -e SERVICENAME (\$service.name$) -l HOSTALIAS (\$host.name$) -n HOSTDISPLAYNAME (\$host.display_name$) -o SERVICEOUTPUT (\$service.output$) -p TELEGRAM_BOT (\$telegram_bot$) -q TELEGRAM_CHATID (\$telegram_chatid$) -r TELEGRAM_BOTTOKEN (\$telegram_bottoken$) -s SERVICESTATE (\$service.state$) -t NOTIFICATIONTYPE (\$notification.type$) -u SERVICEDISPLAYNAME (\$service.display_name$) And these are optional: -b NOTIFICATIONAUTHORNAME (\$notification.author$) -c NOTIFICATIONCOMMENT (\$notification.comment$) -i HAS_ICINGAWEB2 (\$icingaweb2url$, Default: unset) -v (\$notification_logtosyslog$, Default: false) -D DEBUG enable debug output - meant for CLI debug only EOF exit 1; } while getopts 4:6:b:c:d:e:f:hi:l:n:o:p:q:r:s:t:u:v:D opt do case "$opt" in 4) HOSTADDRESS=$OPTARG ;; 6) HOSTADDRESS6=$OPTARG ;; b) NOTIFICATIONAUTHORNAME=$OPTARG ;; c) NOTIFICATIONCOMMENT=$OPTARG ;; d) LONGDATETIME=$OPTARG ;; e) SERVICENAME=$OPTARG ;; h) Usage ;; i) HAS_ICINGAWEB2=$OPTARG ;; l) HOSTALIAS=$OPTARG ;; n) HOSTDISPLAYNAME=$OPTARG ;; o) SERVICEOUTPUT=$OPTARG ;; p) TELEGRAM_BOT=$OPTARG ;; q) TELEGRAM_CHATID=$OPTARG ;; r) TELEGRAM_BOTTOKEN=$OPTARG ;; s) SERVICESTATE=$OPTARG ;; t) NOTIFICATIONTYPE=$OPTARG ;; u) SERVICEDISPLAYNAME=$OPTARG ;; v) VERBOSE=$OPTARG ;; D) DEBUG=1; echo -e "\n**********************************************\nWARNING: DEBUG MODE, DEACTIVATE ASAP\n**********************************************\n" ;; \?) echo "ERROR: Invalid option -$OPTARG" >&2 Usage ;; :) echo "Missing option argument for -$OPTARG" >&2 Usage ;; *) echo "Unimplemented option: -$OPTARG" >&2 Usage ;; esac done # make icon out of service state case "$SERVICESTATE" in CRITICAL) SERVICESTATEICON=$'\xF0\x9F\x9F\xA5';; WARNING) SERVICESTATEICON=$'\xF0\x9F\x9F\xA7';; OK) SERVICESTATEICON=$'\xF0\x9F\x9F\xA9';; UNKNOWN) SERVICESTATEICON=$'\xE2\x9D\x93\x0A';; *) SERVICESTATEICON="[$SERVICESTATE]";; esac # convert date dateInSec=$(date -d "$LONGDATETIME" +%s) nowInSec=$(date +%s) ageInSec=$(($nowInSec - $dateInSec)) ageInMin=$(($ageInSec/60)) ageInHours=$(($ageInMin/60)) ageInDays=$(($ageInHours/24)) if [ "${LONGDATETIME:0:10}" == "$(date +"%Y-%m-%d")" ]; then LONGDATETIME="${LONGDATETIME:11}" fi date=${LONGDATETIME#} if [ $ageInDays -gt 0 ]; then age=" since ${ageInDays}d $(($ageInHours-$ageInDays*24))h ($LONGDATETIME)" elif [ $ageInHours -gt 0 ]; then age=" since ${ageInHours}h $(($ageInMin-$ageInHours*60))m ($LONGDATETIME)" elif [ $ageInMin -gt 0 ]; then age=" since ${ageInMin}m $(($ageInSec-$ageInMin*60))s" elif [ $ageInSec -gt 5 ]; then age=" since ${ageInSec}s" elif [ $ageInSec -lt 0 ]; then age=" since $LONGDATETIME" fi ## Build the message itself NOTIFICATION_MESSAGE=$(cat << EOF $SERVICESTATEICON $SERVICEDISPLAYNAME is ${SERVICESTATE}$age Host: $HOSTALIAS (IPv4 $HOSTADDRESS EOF ) ## Is this host IPv6 capable? if [ -n "$HOSTADDRESS6" ] ; then NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE, IPv6 $HOSTADDRESS6" fi NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE)" NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE More info: $SERVICEOUTPUT" ## Are there any comments? Put them into the message! if [ -n "$NOTIFICATIONCOMMENT" ] ; then NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE Comment by $NOTIFICATIONAUTHORNAME: $NOTIFICATIONCOMMENT" fi ## Are we using Icinga Web 2? Put the URL into the message! if [ -n "$HAS_ICINGAWEB2" ] ; then NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE $HAS_ICINGAWEB2" fi ## Are we verbose? Then put a message to syslog... if [ "$VERBOSE" == "true" ] ; then SUBJECT="[$NOTIFICATIONTYPE] $SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE!" logger "$PROG sends $SUBJECT => Telegram Channel $TELEGRAM_BOT" fi ## debug output or not? if [ -z $DEBUG ];then CURLARGS="--silent --output /dev/null" else CURLARGS=-v set -x echo -e "DEBUG MODE!" fi ## And finally, send the message /usr/bin/curl $CURLARGS \ --data-urlencode "chat_id=${TELEGRAM_CHATID}" \ --data-urlencode "text=${NOTIFICATION_MESSAGE}" \ --data-urlencode "parse_mode=HTML" \ --data-urlencode "disable_web_page_preview=true" \ "https://api.telegram.org/bot${TELEGRAM_BOTTOKEN}/sendMessage" set +x