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