mirror_zfs/cmd/zed/zed.d/io-email.sh
Chris Dunlap aded9a6814 Cleanup ZEDLETs
This commit factors out several common ZEDLET code blocks into
zed-functions.sh.  This shortens the length of the scripts, thereby
(hopefully) making them easier to understand and maintain.

In addition, this commit revamps the coding style used by the
scripts to be more consistent and (again, hopefully) maintainable.
It now mostly follows the Google Shell Style Guide.  I've tried to
assimilate the following resources:

  Google Shell Style Guide
  https://google-styleguide.googlecode.com/svn/trunk/shell.xml

  Dash as /bin/sh
  https://wiki.ubuntu.com/DashAsBinSh

  Filenames and Pathnames in Shell: How to do it Correctly
  http://www.dwheeler.com/essays/filenames-in-shell.html

  Common shell script mistakes
  http://www.pixelbeat.org/programming/shell_script_mistakes.html

Finally, this commit updates the exit codes used by the ZEDLETs to be
more consistent with one another.

All scripts run cleanly through ShellCheck <http://www.shellcheck.net/>.
All scripts have been tested on bash and dash.

Signed-off-by: Chris Dunlap <cdunlap@llnl.gov>
2015-04-27 12:08:01 -07:00

58 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
#
# Send email to ZED_EMAIL in response to a CHECKSUM or IO error.
#
# Only one email per ZED_EMAIL_INTERVAL_SECS will be sent for a given
# class/pool/vdev combination. This protects against spamming the recipient
# should multiple events occur together in time for the same pool/device.
#
# Exit codes:
# 0: email sent
# 1: email failed
# 2: email not configured
# 3: email suppressed
# 9: internal error
[ -f "${ZED_ZEDLET_DIR}/zed.rc" ] && . "${ZED_ZEDLET_DIR}/zed.rc"
. "${ZED_ZEDLET_DIR}/zed-functions.sh"
[ -n "${ZED_EMAIL}" ] || exit 2
[ -n "${ZEVENT_POOL}" ] || exit 9
[ -n "${ZEVENT_SUBCLASS}" ] || exit 9
[ -n "${ZEVENT_VDEV_PATH}" ] || exit 9
if [ "${ZEVENT_SUBCLASS}" != "checksum" ] \
&& [ "${ZEVENT_SUBCLASS}" != "io" ]; then
zed_log_err "unsupported event class \"${ZEVENT_SUBCLASS}\""
exit 9
fi
zed_check_cmd "mail" || exit 9
zed_rate_limit "${ZEVENT_POOL};${ZEVENT_VDEV_PATH};${ZEVENT_SUBCLASS};email" \
|| exit 3
umask 077
email_subject="ZFS ${ZEVENT_SUBCLASS} error for ${ZEVENT_POOL} on $(hostname)"
email_pathname="${TMPDIR:="/tmp"}/$(basename -- "$0").${ZEVENT_EID}.$$"
cat > "${email_pathname}" <<EOF
A ZFS ${ZEVENT_SUBCLASS} error has been detected:
eid: ${ZEVENT_EID}
host: $(hostname)
time: ${ZEVENT_TIME_STRING}
pool: ${ZEVENT_POOL}
vdev: ${ZEVENT_VDEV_TYPE}:${ZEVENT_VDEV_PATH}
EOF
mail -s "${email_subject}" "${ZED_EMAIL}" < "${email_pathname}"
mail_status=$?
if [ "${mail_status}" -ne 0 ]; then
zed_log_msg "mail exit=${mail_status}"
exit 1
fi
rm -f "${email_pathname}"
exit 0