1#!/bin/sh
2# shellcheck disable=SC2154
3#
4# Send notification in response to a given zevent.
5#
6# This is a generic script than can be symlinked to a file in the
7# enabled-zedlets directory to have a notification sent when a particular
8# class of zevents occurs.  The symlink filename must begin with the zevent
9# (sub)class string (e.g., "probe_failure-notify.sh" for the "probe_failure"
10# subclass).  Refer to the zed(8) manpage for details.
11#
12# Only one notification per ZED_NOTIFY_INTERVAL_SECS will be sent for a given
13# class/pool combination.  This protects against spamming the recipient
14# should multiple events occur together in time for the same pool.
15#
16# Exit codes:
17#   0: notification sent
18#   1: notification failed
19#   2: notification not configured
20#   3: notification suppressed
21
22[ -f "${ZED_ZEDLET_DIR}/zed.rc" ] && . "${ZED_ZEDLET_DIR}/zed.rc"
23. "${ZED_ZEDLET_DIR}/zed-functions.sh"
24
25# Rate-limit the notification based in part on the filename.
26#
27rate_limit_tag="${ZEVENT_POOL};${ZEVENT_SUBCLASS};${0##*/}"
28rate_limit_interval="${ZED_NOTIFY_INTERVAL_SECS}"
29zed_rate_limit "${rate_limit_tag}" "${rate_limit_interval}" || exit 3
30
31umask 077
32pool_str="${ZEVENT_POOL:+" for ${ZEVENT_POOL}"}"
33host_str=" on $(hostname)"
34note_subject="ZFS ${ZEVENT_SUBCLASS} event${pool_str}${host_str}"
35note_pathname="$(mktemp)"
36{
37    echo "ZFS has posted the following event:"
38    echo
39    echo "   eid: ${ZEVENT_EID}"
40    echo " class: ${ZEVENT_SUBCLASS}"
41    echo "  host: $(hostname)"
42    echo "  time: ${ZEVENT_TIME_STRING}"
43
44    [ -n "${ZEVENT_VDEV_TYPE}" ] && echo " vtype: ${ZEVENT_VDEV_TYPE}"
45    [ -n "${ZEVENT_VDEV_PATH}" ] && echo " vpath: ${ZEVENT_VDEV_PATH}"
46    [ -n "${ZEVENT_VDEV_GUID}" ] && echo " vguid: ${ZEVENT_VDEV_GUID}"
47
48    [ -n "${ZEVENT_POOL}" ] && [ -x "${ZPOOL}" ] \
49        && "${ZPOOL}" status "${ZEVENT_POOL}"
50
51} > "${note_pathname}"
52
53zed_notify "${note_subject}" "${note_pathname}"; rv=$?
54rm -f "${note_pathname}"
55exit "${rv}"
56