Rework zed_notify_email for configurable PROG/OPTS

This commit reworks the zed_notify_email() function to allow
configuration of the mail executable and command-line arguments.

ZED_EMAIL_PROG specifies the name or path of the executable responsible
for sending notifications via email.  This variable defaults to "mail".

ZED_EMAIL_OPTS specifies command-line options passed to ZED_EMAIL_PROG.
The following keyword substitutions are performed:
- @ADDRESS@ is replaced with the recipient email address(es)
- @SUBJECT@ is replaced with the notification subject
This variable defaults to "-s '@SUBJECT@' @ADDRESS@".

ZED_EMAIL_ADDR replaces ZED_EMAIL (although the latter is retained
for backward compatibility).  This variable can contain multiple
addresses as long as they are delimited by whitespace.

Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>
Signed-off-by: Brian Behlendorf <behlendorf1@llnl.gov>
Closes #3634
Closes #3631
This commit is contained in:
Chris Dunlap 2015-07-28 15:52:40 -07:00 committed by Brian Behlendorf
parent 6f1eccff2c
commit 69520d6855
2 changed files with 51 additions and 11 deletions

View File

@ -207,16 +207,25 @@ zed_notify()
# zed_notify_email (subject, pathname) # zed_notify_email (subject, pathname)
# #
# Send a notification via email to the address specified by ZED_EMAIL. # Send a notification via email to the address specified by ZED_EMAIL_ADDR.
# #
# Requires the mail executable to be installed in the standard PATH. # Requires the mail executable to be installed in the standard PATH, or
# ZED_EMAIL_PROG to be defined with the pathname of an executable capable of
# reading a message body from stdin.
#
# Command-line options to the mail executable can be specified in
# ZED_EMAIL_OPTS. This undergoes the following keyword substitutions:
# - @ADDRESS@ is replaced with the space-delimited recipient email address(es)
# - @SUBJECT@ is replaced with the notification subject
# #
# Arguments # Arguments
# subject: notification subject # subject: notification subject
# pathname: pathname containing the notification message (OPTIONAL) # pathname: pathname containing the notification message (OPTIONAL)
# #
# Globals # Globals
# ZED_EMAIL # ZED_EMAIL_PROG
# ZED_EMAIL_OPTS
# ZED_EMAIL_ADDR
# #
# Return # Return
# 0: notification sent # 0: notification sent
@ -228,19 +237,33 @@ zed_notify_email()
local subject="$1" local subject="$1"
local pathname="${2:-"/dev/null"}" local pathname="${2:-"/dev/null"}"
[ -n "${ZED_EMAIL}" ] || return 2 : "${ZED_EMAIL_PROG:="mail"}"
: "${ZED_EMAIL_OPTS:="-s '@SUBJECT@' @ADDRESS@"}"
# For backward compatibility with ZED_EMAIL.
if [ -n "${ZED_EMAIL}" ] && [ -z "${ZED_EMAIL_ADDR}" ]; then
ZED_EMAIL_ADDR="${ZED_EMAIL}"
fi
[ -n "${ZED_EMAIL_ADDR}" ] || return 2
zed_check_cmd "${ZED_EMAIL_PROG}" || return 1
[ -n "${subject}" ] || return 1 [ -n "${subject}" ] || return 1
if [ ! -r "${pathname}" ]; then if [ ! -r "${pathname}" ]; then
zed_log_err "mail cannot read \"${pathname}\"" zed_log_err \
"$(basename "${ZED_EMAIL_PROG}") cannot read \"${pathname}\""
return 1 return 1
fi fi
zed_check_cmd "mail" || return 1 ZED_EMAIL_OPTS="$(echo "${ZED_EMAIL_OPTS}" \
| sed -e "s/@ADDRESS@/${ZED_EMAIL_ADDR}/g" \
-e "s/@SUBJECT@/${subject}/g")"
mail -s "${subject}" "${ZED_EMAIL}" < "${pathname}" >/dev/null 2>&1; rv=$? # shellcheck disable=SC2086
eval "${ZED_EMAIL_PROG}" ${ZED_EMAIL_OPTS} < "${pathname}" >/dev/null 2>&1
rv=$?
if [ "${rv}" -ne 0 ]; then if [ "${rv}" -ne 0 ]; then
zed_log_err "mail exit=${rv}" zed_log_err "$(basename "${ZED_EMAIL_PROG}") exit=${rv}"
return 1 return 1
fi fi
return 0 return 0

View File

@ -10,11 +10,28 @@
#ZED_DEBUG_LOG="/tmp/zed.debug.log" #ZED_DEBUG_LOG="/tmp/zed.debug.log"
## ##
# Email address of the zpool administrator for receipt of notifications. # Email address of the zpool administrator for receipt of notifications;
# Email will only be sent if ZED_EMAIL is defined. # multiple addresses can be specified if they are delimited by whitespace.
# Email will only be sent if ZED_EMAIL_ADDR is defined.
# Disabled by default; uncomment to enable. # Disabled by default; uncomment to enable.
# #
#ZED_EMAIL="root" #ZED_EMAIL_ADDR="root"
##
# Name or path of executable responsible for sending notifications via email;
# the mail program must be capable of reading a message body from stdin.
# Email will only be sent if ZED_EMAIL_ADDR is defined.
#
#ZED_EMAIL_PROG="mail"
##
# Command-line options for ZED_EMAIL_PROG.
# The string @ADDRESS@ will be replaced with the recipient email address(es).
# The string @SUBJECT@ will be replaced with the notification subject;
# this should be protected with quotes to prevent word-splitting.
# Email will only be sent if ZED_EMAIL_ADDR is defined.
#
#ZED_EMAIL_OPTS="-s '@SUBJECT@' @ADDRESS@"
## ##
# Default directory for zed lock files. # Default directory for zed lock files.