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