From 57a092fc2a4e59068db60fb37a045ff41f1e741e Mon Sep 17 00:00:00 2001 From: pv2b Date: Tue, 8 Aug 2017 11:39:30 +0200 Subject: [PATCH] Notification scripts: Make the `-4` parameter optional Not all hosts may have the `address` attribute set, or just use `address6` instead. refs #5474 --- etc/icinga2/conf.d/commands.conf | 10 ++-------- etc/icinga2/scripts/mail-host-notification.sh | 13 +++++++++---- etc/icinga2/scripts/mail-service-notification.sh | 13 +++++++++---- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/etc/icinga2/conf.d/commands.conf b/etc/icinga2/conf.d/commands.conf index 2aaf44f86..11bfa8266 100644 --- a/etc/icinga2/conf.d/commands.conf +++ b/etc/icinga2/conf.d/commands.conf @@ -10,10 +10,7 @@ object NotificationCommand "mail-host-notification" { command = [ SysconfDir + "/icinga2/scripts/mail-host-notification.sh" ] arguments += { - "-4" = { - required = true - value = "$notification_address$" - } + "-4" = "$notification_address$" "-6" = "$notification_address6$" "-b" = "$notification_author$" "-c" = "$notification_comment$" @@ -72,10 +69,7 @@ object NotificationCommand "mail-service-notification" { command = [ SysconfDir + "/icinga2/scripts/mail-service-notification.sh" ] arguments += { - "-4" = { - required = true - value = "$notification_address$" - } + "-4" = "$notification_address$" "-6" = "$notification_address6$" "-b" = "$notification_author$" "-c" = "$notification_comment$" diff --git a/etc/icinga2/scripts/mail-host-notification.sh b/etc/icinga2/scripts/mail-host-notification.sh index d9ab70754..1f5db94db 100755 --- a/etc/icinga2/scripts/mail-host-notification.sh +++ b/etc/icinga2/scripts/mail-host-notification.sh @@ -16,7 +16,6 @@ Usage() { cat << EOF Required parameters: - -4 HOSTADDRESS (\$address\$) -d LONGDATETIME (\$icinga.long_date_time\$) -l HOSTNAME (\$host.name\$) -n HOSTDISPLAYNAME (\$host.display_name\$) @@ -26,6 +25,7 @@ Required parameters: -t NOTIFICATIONTYPE (\$notification.type\$) Optional parameters: + -4 HOSTADDRESS (\$address\$) -6 HOSTADDRESS6 (\$address6\$) -b NOTIFICATIONAUTHORNAME (\$notification.author\$) -c NOTIFICATIONCOMMENT (\$notification.comment\$) @@ -53,7 +53,7 @@ Error() { while getopts 4:6::b:c:d:f:hi:l:n:o:r:s:t:v: opt do case "$opt" in - 4) HOSTADDRESS=$OPTARG ;; # required + 4) HOSTADDRESS=$OPTARG ;; 6) HOSTADDRESS6=$OPTARG ;; b) NOTIFICATIONAUTHORNAME=$OPTARG ;; c) NOTIFICATIONCOMMENT=$OPTARG ;; @@ -81,7 +81,7 @@ shift $((OPTIND - 1)) ## Check required parameters (TODO: better error message) ## Keep formatting in sync with mail-service-notification.sh -if [ ! "$HOSTADDRESS" ] || [ ! "$LONGDATETIME" ] \ +if [ ! "$LONGDATETIME" ] \ || [ ! "$HOSTNAME" ] || [ ! "$HOSTDISPLAYNAME" ] \ || [ ! "$HOSTOUTPUT" ] || [ ! "$HOSTSTATE" ] \ || [ ! "$USEREMAIL" ] || [ ! "$NOTIFICATIONTYPE" ]; then @@ -101,10 +101,15 @@ Info: $HOSTOUTPUT When: $LONGDATETIME Host: $HOSTNAME (Display Name: "$HOSTDISPLAYNAME") -IPv4: $HOSTADDRESS EOF ` +## Check whether IPv4 was specified. +if [ -n "$HOSTADDRESS" ] ; then + NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE +IPv4: $HOSTADDRESS" +fi + ## Check whether IPv6 was specified. if [ -n "$HOSTADDRESS6" ] ; then NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE diff --git a/etc/icinga2/scripts/mail-service-notification.sh b/etc/icinga2/scripts/mail-service-notification.sh index dca23dc63..f99e3ed18 100755 --- a/etc/icinga2/scripts/mail-service-notification.sh +++ b/etc/icinga2/scripts/mail-service-notification.sh @@ -16,7 +16,6 @@ Usage() { cat << EOF Required parameters: - -4 HOSTADDRESS (\$address\$) -d LONGDATETIME (\$icinga.long_date_time\$) -e SERVICENAME (\$service.name\$) -l HOSTNAME (\$host.name\$) @@ -28,6 +27,7 @@ Required parameters: -u SERVICEDISPLAYNAME (\$service.display_name\$) Optional parameters: + -4 HOSTADDRESS (\$address\$) -6 HOSTADDRESS6 (\$address6\$) -b NOTIFICATIONAUTHORNAME (\$notification.author\$) -c NOTIFICATIONCOMMENT (\$notification.comment\$) @@ -55,7 +55,7 @@ Error() { while getopts 4:6:b:c:d:e:f:hi:l:n:o:r:s:t:u:v: opt do case "$opt" in - 4) HOSTADDRESS=$OPTARG ;; # required + 4) HOSTADDRESS=$OPTARG ;; 6) HOSTADDRESS6=$OPTARG ;; b) NOTIFICATIONAUTHORNAME=$OPTARG ;; c) NOTIFICATIONCOMMENT=$OPTARG ;; @@ -85,7 +85,7 @@ shift $((OPTIND - 1)) ## Check required parameters (TODO: better error message) ## Keep formatting in sync with mail-host-notification.sh -if [ ! "$HOSTADDRESS" ] || [ ! "$LONGDATETIME" ] \ +if [ ! "$LONGDATETIME" ] \ || [ ! "$HOSTNAME" ] || [ ! "$HOSTDISPLAYNAME" ] \ || [ ! "$SERVICENAME" ] || [ ! "$SERVICEDISPLAYNAME" ] \ || [ ! "$SERVICEOUTPUT" ] || [ ! "$SERVICESTATE" ] \ @@ -107,10 +107,15 @@ Info: $SERVICEOUTPUT When: $LONGDATETIME Service: $SERVICENAME (Display Name: "$SERVICEDISPLAYNAME") Host: $HOSTNAME (Display Name: "$HOSTDISPLAYNAME") -IPv4: $HOSTADDRESS EOF ` +## Check whether IPv4 was specified. +if [ -n "$HOSTADDRESS" ] ; then + NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE +IPv4: $HOSTADDRESS" +fi + ## Check whether IPv6 was specified. if [ -n "$HOSTADDRESS6" ] ; then NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE