QoL, formatting

This commit is contained in:
C0rn3j 2020-06-12 21:33:40 +02:00
parent 9b2ab135ea
commit 1723992b30

View file

@ -20,15 +20,12 @@ cat << EOF
alert-by-telegram notification script for Icinga 2 by spillerm <github@spiller.me> alert-by-telegram notification script for Icinga 2 by spillerm <github@spiller.me>
The following are mandatory: The following are mandatory:
-4 HOSTADDRESS (\$address$)
-6 HOSTADDRESS6 (\$address6$)
-a ALERTTYPE (host or service) -a ALERTTYPE (host or service)
-d LONGDATETIME (\$icinga.long_date_time$) -d LONGDATETIME (\$icinga.long_date_time$)
-e SERVICENAME (\$service.name$ Only if ALERTTYPE is service) # TODO, currently unused -e SERVICENAME (\$service.name$ Only if ALERTTYPE is service) # TODO, currently unused
-l HOSTALIAS (\$host.name$) -l HOSTALIAS (\$host.name$)
-n HOSTDISPLAYNAME (\$host.display_name$) -n HOSTDISPLAYNAME (\$host.display_name$)
-o SERVICEOUTPUT (\$service.output$ or \$host.output$) -o SERVICEOUTPUT (\$service.output$ or \$host.output$)
-p TELEGRAM_BOT (\$telegram_bot$)
-q TELEGRAM_CHATID (\$telegram_chatid$) -q TELEGRAM_CHATID (\$telegram_chatid$)
-r TELEGRAM_BOTTOKEN (\$telegram_bottoken$) -r TELEGRAM_BOTTOKEN (\$telegram_bottoken$)
-s SERVICESTATE (\$service.state$ or \$host.state$) -s SERVICESTATE (\$service.state$ or \$host.state$)
@ -36,10 +33,13 @@ The following are mandatory:
-u SERVICEDISPLAYNAME (\$service.display_name$) -u SERVICEDISPLAYNAME (\$service.display_name$)
And these are optional: And these are optional:
-4 HOSTADDRESS (\$address$)
-6 HOSTADDRESS6 (\$address6$)
-b NOTIFICATIONAUTHORNAME (\$notification.author$) -b NOTIFICATIONAUTHORNAME (\$notification.author$)
-c NOTIFICATIONCOMMENT (\$notification.comment$) -c NOTIFICATIONCOMMENT (\$notification.comment$)
-i HAS_ICINGAWEB2 (\$icingaweb2url$, Default: unset) -i HAS_ICINGAWEB2 (\$icingaweb2url$, Default: unset)
-v (\$notification_logtosyslog$, Default: false) -v (\$notification_logtosyslog$, Default: false)
-p TELEGRAM_BOT (\$telegram_bot$)
-D DEBUG enable debug output - meant for CLI debug only -D DEBUG enable debug output - meant for CLI debug only
EOF EOF
} }
@ -91,9 +91,9 @@ else
fi fi
fi fi
if [[ -z ${HOSTADDRESS-} ]] || [[ -z ${HOSTADDRESS6-} ]] || [[ -z ${LONGDATETIME-} ]] || [[ -z ${HOSTALIAS-} ]] \ if [[ -z ${LONGDATETIME-} ]] || [[ -z ${HOSTALIAS-} ]] || [[ -z ${HOSTDISPLAYNAME-} ]] \
|| [[ -z ${HOSTDISPLAYNAME-} ]] || [[ -z ${SERVICEOUTPUT-} ]] || [[ -z ${TELEGRAM_BOT-} ]] || [[ -z ${TELEGRAM_CHATID-} ]] \ || [[ -z ${SERVICEOUTPUT-} ]] || [[ -z ${TELEGRAM_CHATID-} ]] || [[ -z ${TELEGRAM_BOTTOKEN-} ]] \
|| [[ -z ${TELEGRAM_BOTTOKEN-} ]] || [[ -z ${SERVICESTATE-} ]] || [[ -z ${NOTIFICATIONTYPE-} ]]; then || [[ -z ${SERVICESTATE-} ]] || [[ -z ${NOTIFICATIONTYPE-} ]]; then
Usage Usage
exit 1 exit 1
fi fi
@ -108,49 +108,51 @@ fi
## Build the message itself ## Build the message itself
if [[ $ALERTTYPE == "host" ]]; then if [[ $ALERTTYPE == "host" ]]; then
NOTIFICATION_MESSAGE=$(cat << EOF NOTIFICATION_MESSAGE=$(cat << EOF
$HOSTDISPLAYNAME ($HOSTALIAS) is $SERVICESTATE! <u>[$SERVICESTATE] $HOSTDISPLAYNAME ($HOSTALIAS) - at $LONGDATETIME</u>
When? $LONGDATETIME
Info? $SERVICEOUTPUT
Host? $HOSTALIAS
IPv4? $HOSTADDRESS
EOF EOF
) )
else else
NOTIFICATION_MESSAGE=$(cat << EOF NOTIFICATION_MESSAGE=$(cat << EOF
[$SERVICESTATE] $SERVICEDISPLAYNAME is $SERVICESTATE since $LONGDATETIME <u>[$SERVICESTATE] $SERVICEDISPLAYNAME - at $LONGDATETIME</u>
Host: $HOSTALIAS (IPv4 $HOSTADDRESS) <b>Host:</b> <code>$HOSTALIAS</code>
More info: $SERVICEOUTPUT
EOF EOF
) )
fi fi
## Is this host IPv6 capable? if [[ -n "${HOSTADDRESS-}" ]]; then
if [ -n "$HOSTADDRESS6" ]; then
NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
IPv6? $HOSTADDRESS6" <b>IPv4:</b> <code>$HOSTADDRESS</code>"
fi fi
## Are there any comments? Put them into the message! if [[ -n "${HOSTADDRESS6-}" ]]; then
if [ -n "${NOTIFICATIONCOMMENT-}" ] ; then
NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
Comment by $NOTIFICATIONAUTHORNAME: <b>IPv6:</b> <code>$HOSTADDRESS6</code>"
$NOTIFICATIONCOMMENT" fi
NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
<b>Output:</b> <code>$SERVICEOUTPUT</code>"
## Are there any comments? Put them into the message!
if [[ -n "${NOTIFICATIONCOMMENT-}" ]] ; then
NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
<b>Comment by $NOTIFICATIONAUTHORNAME:</b> <code>$NOTIFICATIONCOMMENT</code>"
fi fi
## Are we using Icinga Web 2? Put the URL into the message! ## Are we using Icinga Web 2? Put the URL into the message!
if [ -n "${HAS_ICINGAWEB2-}" ] ; then if [[ -n "${HAS_ICINGAWEB2-}" ]] ; then
NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
Get live status: <b>Get live status:</b> <code>$HAS_ICINGAWEB2/monitoring/host/show?host=$HOSTALIAS</code>"
$HAS_ICINGAWEB2/monitoring/host/show?host=$HOSTALIAS"
fi fi
## Are we verbose? Then put a message to syslog... ## Are we verbose? Then put a message to syslog...
if [ "${VERBOSE-}" == "true" ] ; then if [[ "${VERBOSE-}" == "true" ]] ; then
logger "$PROG sends $SUBJECT => Telegram Channel $TELEGRAM_BOT" logger "$PROG sends $SUBJECT => Telegram Channel $TELEGRAM_BOT"
fi fi
## debug output or not? ## debug output or not?
if [ -z ${DEBUG-} ]; then if [[ -z ${DEBUG-} ]]; then
CURLARGS="--silent --output /dev/null" CURLARGS="--silent --output /dev/null"
else else
CURLARGS=-v CURLARGS=-v