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