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