mirror of https://github.com/Icinga/icinga2.git
Check for required parameters in notification scripts; add error helpers
refs #5170
This commit is contained in:
parent
91ab8551c7
commit
7b9f478bc1
|
@ -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
|
||||
|
||||
exit 1;
|
||||
}
|
||||
|
||||
Help() {
|
||||
Usage;
|
||||
exit 0;
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue