1#!/bin/sh
2#
3# Send notification in response to a RESILVER_FINISH or SCRUB_FINISH.
4#
5# By default, "zpool status" output will only be included for a scrub_finish
6# zevent if the pool is not healthy; to always include its output, set
7# ZED_NOTIFY_VERBOSE=1.
8#
9# Exit codes:
10#   0: notification sent
11#   1: notification failed
12#   2: notification not configured
13#   3: notification suppressed
14#   9: internal error
15
16[ -f "${ZED_ZEDLET_DIR}/zed.rc" ] && . "${ZED_ZEDLET_DIR}/zed.rc"
17. "${ZED_ZEDLET_DIR}/zed-functions.sh"
18
19[ -n "${ZEVENT_POOL}" ] || exit 9
20[ -n "${ZEVENT_SUBCLASS}" ] || exit 9
21
22if   [ "${ZEVENT_SUBCLASS}" = "resilver_finish" ]; then
23    action="resilver"
24elif [ "${ZEVENT_SUBCLASS}" = "scrub_finish" ]; then
25    action="scrub"
26else
27    zed_log_err "unsupported event class \"${ZEVENT_SUBCLASS}\""
28    exit 9
29fi
30
31zed_check_cmd "${ZPOOL}" || exit 9
32
33# For scrub, suppress notification if the pool is healthy
34# and verbosity is not enabled.
35#
36if [ "${ZEVENT_SUBCLASS}" = "scrub_finish" ]; then
37    healthy="$("${ZPOOL}" status -x "${ZEVENT_POOL}" \
38        | grep "'${ZEVENT_POOL}' is healthy")"
39    [ -n "${healthy}" ] && [ "${ZED_NOTIFY_VERBOSE}" -eq 0 ] && exit 3
40fi
41
42umask 077
43note_subject="ZFS ${ZEVENT_SUBCLASS} event for ${ZEVENT_POOL} on $(hostname)"
44note_pathname="${TMPDIR:="/tmp"}/$(basename -- "$0").${ZEVENT_EID}.$$"
45{
46    echo "ZFS has finished a ${action}:"
47    echo
48    echo "   eid: ${ZEVENT_EID}"
49    echo " class: ${ZEVENT_SUBCLASS}"
50    echo "  host: $(hostname)"
51    echo "  time: ${ZEVENT_TIME_STRING}"
52
53    "${ZPOOL}" status "${ZEVENT_POOL}"
54
55} > "${note_pathname}"
56
57zed_notify "${note_subject}" "${note_pathname}"; rv=$?
58rm -f "${note_pathname}"
59exit "${rv}"
60