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