2018-01-29 10:29:11 +01:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
## /etc/icinga2/scripts/host-by-telegram.sh / 20170330
|
|
|
|
## Marianne M. Spiller <github@spiller.me>
|
2019-08-20 13:26:17 +02:00
|
|
|
## Last updated 20190820
|
|
|
|
## Last tests used icinga2-2.10.5-1.stretch
|
2018-01-29 10:29:11 +01:00
|
|
|
|
|
|
|
PROG="`basename $0`"
|
|
|
|
HOSTNAME="`hostname`"
|
|
|
|
TRANSPORT="curl"
|
2019-07-22 12:32:33 +02:00
|
|
|
unset DEBUG
|
2018-01-29 10:29:11 +01:00
|
|
|
|
|
|
|
if [ -z "`which $TRANSPORT`" ] ; then
|
|
|
|
echo "$TRANSPORT not in \$PATH. Consider installing it."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
Usage() {
|
|
|
|
cat << EOF
|
|
|
|
|
|
|
|
host-by-mail notification script for Icinga 2 by spillerm <github@spiller.me>
|
|
|
|
|
|
|
|
The following are mandatory:
|
|
|
|
-4 HOSTADDRESS (\$address$)
|
2019-07-22 12:54:42 +02:00
|
|
|
-6 HOSTADDRESS6 (\$address6$)
|
2018-01-29 10:29:11 +01:00
|
|
|
-d LONGDATETIME (\$icinga.long_date_time$)
|
|
|
|
-l HOSTALIAS (\$host.name$)
|
|
|
|
-n HOSTDISPLAYNAME (\$host.display_name$)
|
|
|
|
-o HOSTOUTPUT (\$host.output$)
|
|
|
|
-p TELEGRAM_BOT (\$telegram_bot$)
|
2019-07-22 13:07:58 +02:00
|
|
|
-q TELEGRAM_CHATID (\$telegram_chatid$)
|
2018-01-29 10:29:11 +01:00
|
|
|
-r TELEGRAM_BOTTOKEN (\$telegram_bottoken$)
|
|
|
|
-s HOSTSTATE (\$host.state$)
|
|
|
|
-t NOTIFICATIONTYPE (\$notification.type$)
|
|
|
|
|
|
|
|
And these are optional:
|
|
|
|
-b NOTIFICATIONAUTHORNAME (\$notification.author$)
|
|
|
|
-c NOTIFICATIONCOMMENT (\$notification.comment$)
|
|
|
|
-i HAS_ICINGAWEB2 (\$icingaweb2url$, Default: unset)
|
|
|
|
-v (\$notification_logtosyslog$, Default: false)
|
2019-07-22 12:32:33 +02:00
|
|
|
-D DEBUG enable debug output - meant for CLI debug only
|
2018-01-29 10:29:11 +01:00
|
|
|
|
|
|
|
EOF
|
|
|
|
|
|
|
|
exit 1;
|
|
|
|
}
|
|
|
|
|
2019-07-22 12:32:33 +02:00
|
|
|
while getopts 4:6::b:c:d:hi:l:n:o:p:q:r:s:t:u:v:D opt
|
2018-01-29 10:29:11 +01:00
|
|
|
do
|
|
|
|
case "$opt" in
|
|
|
|
4) HOSTADDRESS=$OPTARG ;;
|
|
|
|
6) HOSTADDRESS6=$OPTARG ;;
|
|
|
|
b) NOTIFICATIONAUTHORNAME=$OPTARG ;;
|
|
|
|
c) NOTIFICATIONCOMMENT=$OPTARG ;;
|
|
|
|
d) LONGDATETIME=$OPTARG ;;
|
|
|
|
h) Usage ;;
|
|
|
|
i) HAS_ICINGAWEB2=$OPTARG ;;
|
|
|
|
l) HOSTALIAS=$OPTARG ;;
|
|
|
|
n) HOSTDISPLAYNAME=$OPTARG ;;
|
|
|
|
o) HOSTOUTPUT=$OPTARG ;;
|
|
|
|
p) TELEGRAM_BOT=$OPTARG ;;
|
2019-07-22 13:07:58 +02:00
|
|
|
q) TELEGRAM_CHATID=$OPTARG ;;
|
2018-01-29 10:29:11 +01:00
|
|
|
r) TELEGRAM_BOTTOKEN=$OPTARG ;;
|
|
|
|
s) HOSTSTATE=$OPTARG ;;
|
|
|
|
t) NOTIFICATIONTYPE=$OPTARG ;;
|
|
|
|
v) VERBOSE=$OPTARG ;;
|
2019-07-22 12:32:33 +02:00
|
|
|
D) DEBUG=1; echo -e "\n**********************************************\nWARNING: DEBUG MODE, DEACTIVATE ASAP\n**********************************************\n" ;;
|
2018-01-29 10:29:11 +01:00
|
|
|
\?) echo "ERROR: Invalid option -$OPTARG" >&2
|
|
|
|
Usage ;;
|
|
|
|
:) echo "Missing option argument for -$OPTARG" >&2
|
|
|
|
Usage ;;
|
|
|
|
*) echo "Unimplemented option: -$OPTARG" >&2
|
|
|
|
Usage ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
## Build the message's subject
|
|
|
|
SUBJECT="[$NOTIFICATIONTYPE] $SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE!"
|
|
|
|
|
|
|
|
## Build the message itself
|
|
|
|
NOTIFICATION_MESSAGE=$(cat << EOF
|
|
|
|
$HOSTDISPLAYNAME ($HOSTALIAS) is $HOSTSTATE!
|
|
|
|
When? $LONGDATETIME
|
|
|
|
Info? $HOSTOUTPUT
|
|
|
|
Host? $HOSTALIAS
|
|
|
|
IPv4? $HOSTADDRESS
|
|
|
|
EOF
|
|
|
|
)
|
|
|
|
|
|
|
|
## Is this host IPv6 capable?
|
|
|
|
if [ -n "$HOSTADDRESS6" ] ; then
|
|
|
|
NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
|
|
|
|
IPv6? $HOSTADDRESS6"
|
|
|
|
fi
|
|
|
|
## 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
|
|
|
|
Get live status:
|
|
|
|
$HAS_ICINGAWEB2/monitoring/host/show?host=$HOSTALIAS"
|
|
|
|
fi
|
|
|
|
## Build the message's subject
|
|
|
|
SUBJECT="[$NOTIFICATIONTYPE] Host $HOSTDISPLAYNAME is $HOSTSTATE!"
|
|
|
|
|
|
|
|
## Are we verbose? Then put a message to syslog...
|
|
|
|
if [ "$VERBOSE" == "true" ] ; then
|
|
|
|
logger "$PROG sends $SUBJECT => Telegram Channel $TELEGRAM_BOT"
|
|
|
|
fi
|
|
|
|
|
2019-07-22 12:32:33 +02:00
|
|
|
## debug output or not
|
|
|
|
if [ -z $DEBUG ];then
|
|
|
|
CURLARGS="--silent --output /dev/null"
|
|
|
|
else
|
|
|
|
CURLARGS=-v
|
|
|
|
set -x
|
|
|
|
echo -e "DEBUG MODE!"
|
|
|
|
fi
|
|
|
|
|
2018-01-29 10:29:11 +01:00
|
|
|
## And finally, send the message
|
2019-07-22 12:32:33 +02:00
|
|
|
/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
|