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