]> granicus.if.org Git - icinga2/commitdiff
Check for required parameters in notification scripts; add error helpers 5170/head
authorMichael Friedrich <michael.friedrich@icinga.com>
Wed, 10 May 2017 11:22:44 +0000 (13:22 +0200)
committerMichael Friedrich <michael.friedrich@icinga.com>
Wed, 10 May 2017 11:23:39 +0000 (13:23 +0200)
refs #5170

etc/icinga2/scripts/mail-host-notification.sh
etc/icinga2/scripts/mail-service-notification.sh

index 34d0ced7e438a7eb17bec43022494803fbe9e4c4..fba41c8907413de99d5097247d7c832f515e9d23 100755 (executable)
@@ -5,65 +5,87 @@ HOSTNAME="`hostname`"
 MAILBIN="mail"
 
 if [ -z "`which $MAILBIN`" ] ; then
-  echo "$MAILBIN not in \$PATH. Consider installing it."
+  echo "$MAILBIN not found in \$PATH. Consider installing it."
   exit 1
 fi
 
+## Function helpers
 Usage() {
 cat << EOF
 
 Required parameters:
-  -4 HOSTADDRESS (\$address$)
-  -6 HOSTADDRESS6 (\$address6$)
-  -d LONGDATETIME (\$icinga.long_date_time$)
-  -l HOSTALIAS (\$host.name$)
-  -n HOSTDISPLAYNAME (\$host.display_name$)
-  -o HOSTOUTPUT (\$host.output$)
-  -r USEREMAIL (\$user.email$)
-  -s HOSTSTATE (\$host.state$)
-  -t NOTIFICATIONTYPE (\$notification.type$)
+  -4 HOSTADDRESS (\$address\$)
+  -d LONGDATETIME (\$icinga.long_date_time\$)
+  -l HOSTNAME (\$host.name\$)
+  -n HOSTDISPLAYNAME (\$host.display_name\$)
+  -o HOSTOUTPUT (\$host.output\$)
+  -r USEREMAIL (\$user.email\$)
+  -s HOSTSTATE (\$host.state\$)
+  -t NOTIFICATIONTYPE (\$notification.type\$)
 
 Optional parameters:
-  -b NOTIFICATIONAUTHORNAME (\$notification.author$)
-  -c NOTIFICATIONCOMMENT (\$notification.comment$)
-  -i ICINGAWEB2URL (\$notification_icingaweb2url$, Default: unset)
-  -f MAILFROM (\$notification_mailfrom$, requires GNU mailutils)
-  -v (\$notification_sendtosyslog$, Default: false)
+  -6 HOSTADDRESS6 (\$address6\$)
+  -b NOTIFICATIONAUTHORNAME (\$notification.author\$)
+  -c NOTIFICATIONCOMMENT (\$notification.comment\$)
+  -i ICINGAWEB2URL (\$notification_icingaweb2url\$, Default: unset)
+  -f MAILFROM (\$notification_mailfrom\$, requires GNU mailutils)
+  -v (\$notification_sendtosyslog\$, Default: false)
 
 EOF
+}
+
+Help() {
+  Usage;
+  exit 0;
+}
 
-exit 1;
+Error() {
+  if [ "$1" ]; then
+    echo $1
+  fi
+  Usage;
+  exit 1;
 }
 
+## Main
 while getopts 4:6::b:c:d:f:hi:l:n:o:r:s:t:v: opt
 do
   case "$opt" in
-    4) HOSTADDRESS=$OPTARG ;;
+    4) HOSTADDRESS=$OPTARG ;; # required
     6) HOSTADDRESS6=$OPTARG ;;
     b) NOTIFICATIONAUTHORNAME=$OPTARG ;;
     c) NOTIFICATIONCOMMENT=$OPTARG ;;
-    d) LONGDATETIME=$OPTARG ;;
+    d) LONGDATETIME=$OPTARG ;; # required
     f) MAILFROM=$OPTARG ;;
-    h) Usage ;;
+    h) Help ;;
     i) ICINGAWEB2URL=$OPTARG ;;
-    l) HOSTALIAS=$OPTARG ;;
-    n) HOSTDISPLAYNAME=$OPTARG ;;
-    o) HOSTOUTPUT=$OPTARG ;;
-    r) USEREMAIL=$OPTARG ;;
-    s) HOSTSTATE=$OPTARG ;;
-    t) NOTIFICATIONTYPE=$OPTARG ;;
+    l) HOSTNAME=$OPTARG ;; # required
+    n) HOSTDISPLAYNAME=$OPTARG ;; # required
+    o) HOSTOUTPUT=$OPTARG ;; # required
+    r) USEREMAIL=$OPTARG ;; # required
+    s) HOSTSTATE=$OPTARG ;; # required
+    t) NOTIFICATIONTYPE=$OPTARG ;; # required
     v) VERBOSE=$OPTARG ;;
    \?) echo "ERROR: Invalid option -$OPTARG" >&2
-       Usage ;;
+       Error ;;
     :) echo "Missing option argument for -$OPTARG" >&2
-       Usage ;;
+       Error ;;
     *) echo "Unimplemented option: -$OPTARG" >&2
-       Usage ;;
+       Error ;;
   esac
 done
 
 shift $((OPTIND - 1))
 
+## Check required parameters (TODO: better error message)
+## Keep formatting in sync with mail-service-notification.sh
+if [ ! "$HOSTADDRESS" ] || [ ! "$LONGDATETIME" ] \
+|| [ ! "$HOSTNAME" ] || [ ! "$HOSTDISPLAYNAME" ] \
+|| [ ! "$HOSTOUTPUT" ] || [ ! "$HOSTSTATE" ] \
+|| [ ! "$USEREMAIL" ] || [ ! "$NOTIFICATIONTYPE" ]; then
+  Error "Requirement parameters are missing."
+fi
+
 ## Build the message's subject
 SUBJECT="[$NOTIFICATIONTYPE] Host $HOSTDISPLAYNAME is $HOSTSTATE!"
 
@@ -81,13 +103,13 @@ IPv4:    $HOSTADDRESS
 EOF
 `
 
-## Is this host IPv6 capable? Put its address into the message.
+## Check whether IPv6 was specified.
 if [ -n "$HOSTADDRESS6" ] ; then
   NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
 IPv6:   $HOSTADDRESS6"
 fi
 
-## Are there any comments? Put them into the message.
+## Check whether author and comment was specified.
 if [ -n "$NOTIFICATIONCOMMENT" ] ; then
   NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
 
@@ -95,21 +117,21 @@ Comment by $NOTIFICATIONAUTHORNAME:
   $NOTIFICATIONCOMMENT"
 fi
 
-## Are we using Icinga Web 2? Put the URL into the message.
+## Check whether Icinga Web 2 URL was specified.
 if [ -n "$ICINGAWEB2URL" ] ; then
   NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
 
 URL:
-  $ICINGAWEB2URL/monitoring/host/show?host=$HOSTALIAS"
+  $ICINGAWEB2URL/monitoring/host/show?host=$HOSTNAME"
 fi
 
-## Are we verbose? Then put a message to syslog.
+## Check whether verbose mode was enabled and log to syslog.
 if [ "$VERBOSE" == "true" ] ; then
   logger "$PROG sends $SUBJECT => $USEREMAIL"
 fi
 
-## And finally: send the message using $MAILBIN command.
-## Do we have an explicit sender? Then we'll use it.
+## Send the mail using the $MAILBIN command.
+## If an explicit sender was specified, try to set it.
 if [ -n "$MAILFROM" ] ; then
   /usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" \
   | $MAILBIN -a "From: $MAILFROM" -s "$SUBJECT" $USEREMAIL
index c37b45ac949ce5fed0d781b5707352df90ef13e2..48fe420434b8056b1f7f3198cfbfd25e881adb00 100755 (executable)
@@ -5,56 +5,70 @@ HOSTNAME="`hostname`"
 MAILBIN="mail"
 
 if [ -z "`which $MAILBIN`" ] ; then
-  echo "$MAILBIN not in \$PATH. Consider installing it."
+  echo "$MAILBIN not found in \$PATH. Consider installing it."
   exit 1
 fi
 
+## Function helpers
 Usage() {
 cat << EOF
 
 Required parameters:
-  -4 HOSTADDRESS (\$address$)
-  -6 HOSTADDRESS (\$address6$)
-  -d LONGDATETIME (\$icinga.long_date_time$)
-  -e SERVICENAME (\$service.name$)
-  -l HOSTALIAS (\$host.name$)
-  -n HOSTDISPLAYNAME (\$host.display_name$)
-  -o SERVICEOUTPUT (\$service.output$)
-  -r USEREMAIL (\$user.email$)
-  -s SERVICESTATE (\$service.state$)
-  -t NOTIFICATIONTYPE (\$notification.type$)
-  -u SERVICEDISPLAYNAME (\$service.display_name$)
+  -4 HOSTADDRESS (\$address\$)
+  -d LONGDATETIME (\$icinga.long_date_time\$)
+  -e SERVICENAME (\$service.name\$)
+  -l HOSTNAME (\$host.name\$)
+  -n HOSTDISPLAYNAME (\$host.display_name\$)
+  -o SERVICEOUTPUT (\$service.output\$)
+  -r USEREMAIL (\$user.email\$)
+  -s SERVICESTATE (\$service.state\$)
+  -t NOTIFICATIONTYPE (\$notification.type\$)
+  -u SERVICEDISPLAYNAME (\$service.display_name\$)
 
 Optional parameters:
-  -b NOTIFICATIONAUTHORNAME (\$notification.author$)
-  -c NOTIFICATIONCOMMENT (\$notification.comment$)
-  -i ICINGAWEB2URL (\$notification_icingaweb2url$, Default: unset)
-  -f MAILFROM (\$notification_mailfrom$, requires GNU mailutils)
-e -v (\$notification_sendtosyslog$, Default: false)
+  -6 HOSTADDRESS6 (\$address6\$)
+  -b NOTIFICATIONAUTHORNAME (\$notification.author\$)
+  -c NOTIFICATIONCOMMENT (\$notification.comment\$)
+  -i ICINGAWEB2URL (\$notification_icingaweb2url\$, Default: unset)
+  -f MAILFROM (\$notification_mailfrom\$, requires GNU mailutils)
+  -v (\$notification_sendtosyslog\$, Default: false)
 
 EOF
-exit 1;
 }
 
+Help() {
+  Usage;
+  exit 0;
+}
+
+Error() {
+  if [ "$1" ]; then
+    echo $1
+  fi
+  Usage;
+  exit 1;
+}
+
+## Main
 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 ;;
+    4) HOSTADDRESS=$OPTARG ;; # required
     6) HOSTADDRESS6=$OPTARG ;;
     b) NOTIFICATIONAUTHORNAME=$OPTARG ;;
     c) NOTIFICATIONCOMMENT=$OPTARG ;;
-    d) LONGDATETIME=$OPTARG ;;
-    e) SERVICENAME=$OPTARG ;;
+    d) LONGDATETIME=$OPTARG ;; # required
+    e) SERVICENAME=$OPTARG ;; # required
     f) MAILFROM=$OPTARG ;;
     h) Usage ;;
     i) ICINGAWEB2URL=$OPTARG ;;
-    l) HOSTALIAS=$OPTARG ;;
-    n) HOSTDISPLAYNAME=$OPTARG ;;
-    o) SERVICEOUTPUT=$OPTARG ;;
-    r) USEREMAIL=$OPTARG ;;
-    s) SERVICESTATE=$OPTARG ;;
-    t) NOTIFICATIONTYPE=$OPTARG ;;
-    u) SERVICEDISPLAYNAME=$OPTARG ;;
+    l) HOSTNAME=$OPTARG ;; # required
+    n) HOSTDISPLAYNAME=$OPTARG ;; # required
+    o) SERVICEOUTPUT=$OPTARG ;; # required
+    r) USEREMAIL=$OPTARG ;; # required
+    s) SERVICESTATE=$OPTARG ;; # required
+    t) NOTIFICATIONTYPE=$OPTARG ;; # required
+    u) SERVICEDISPLAYNAME=$OPTARG ;; # required
     v) VERBOSE=$OPTARG ;;
    \?) echo "ERROR: Invalid option -$OPTARG" >&2
        Usage ;;
@@ -67,6 +81,16 @@ done
 
 shift $((OPTIND - 1))
 
+## Check required parameters (TODO: better error message)
+## Keep formatting in sync with mail-host-notification.sh
+if [ ! "$HOSTADDRESS" ] || [ ! "$LONGDATETIME" ] \
+|| [ ! "$HOSTNAME" ] || [ ! "$HOSTDISPLAYNAME" ] \
+|| [ ! "$SERVICENAME" ] || [ ! "$SERVICEDISPLAYNAME" ] \
+|| [ ! "$SERVICEOUTPUT" ] || [ ! "$SERVICESTATE" ] \
+|| [ ! "$USEREMAIL" ] || [ ! "$NOTIFICATIONTYPE" ]; then
+  Error "Requirement parameters are missing."
+fi
+
 ## Build the message's subject
 SUBJECT="[$NOTIFICATIONTYPE] $SERVICEDISPLAYNAME on $HOSTDISPLAYNAME is $SERVICESTATE!"
 
@@ -85,13 +109,13 @@ IPv4:    $HOSTADDRESS
 EOF
 `
 
-## Is this host IPv6 capable? Put its address into the message.
+## Check whether IPv6 was specified.
 if [ -n "$HOSTADDRESS6" ] ; then
   NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
 IPv6:    $HOSTADDRESS6"
 fi
 
-## Are there any comments? Put them into the message.
+## Check whether author and comment was specified.
 if [ -n "$NOTIFICATIONCOMMENT" ] ; then
   NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
 
@@ -99,21 +123,21 @@ Comment by $NOTIFICATIONAUTHORNAME:
   $NOTIFICATIONCOMMENT"
 fi
 
-## Are we using Icinga Web 2? Put the URL into the message.
+## Check whether Icinga Web 2 URL was specified.
 if [ -n "$ICINGAWEB2URL" ] ; then
   NOTIFICATION_MESSAGE="$NOTIFICATION_MESSAGE
 
-Get live status:
-  $ICINGAWEB2URL/monitoring/service/show?host=$HOSTALIAS&service=$SERVICENAME"
+URL:
+  $ICINGAWEB2URL/monitoring/service/show?host=$HOSTNAME&service=$SERVICENAME"
 fi
 
-## Are we verbose? Then put a message to syslog.
+## Check whether verbose mode was enabled and log to syslog.
 if [ "$VERBOSE" == "true" ] ; then
   logger "$PROG sends $SUBJECT => $USEREMAIL"
 fi
 
-## And finally: send the message using $MAILBIN command.
-## Do we have an explicit sender? Then we'll use it.
+## Send the mail using the $MAILBIN command.
+## If an explicit sender was specified, try to set it.
 if [ -n "$MAILFROM" ] ; then
   /usr/bin/printf "%b" "$NOTIFICATION_MESSAGE" \
   | $MAILBIN -a "From: $MAILFROM" -s "$SUBJECT" $USEREMAIL