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