Skip to content

Commit a6f6c88

Browse files
slash2314tonyhutter
authored andcommitted
Add Ntfy notification support to ZED
This commit adds the zed_notify_ntfy() function and hooks it into zed_notify(). This will allow ZED to send notifications to ntfy.sh or a self-hosted Ntfy service, which can be received on a desktop or mobile device. It is configured with ZED_NTFY_TOPIC, ZED_NTFY_URL, and ZED_NTFY_ACCESS_TOKEN variables in zed.rc. Reviewed-by: @classabbyamp Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Dex Wood <[email protected]> Closes #15584
1 parent fc3d34b commit a6f6c88

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

cmd/zed/zed.d/zed-functions.sh

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ zed_notify()
205205
[ "${rv}" -eq 0 ] && num_success=$((num_success + 1))
206206
[ "${rv}" -eq 1 ] && num_failure=$((num_failure + 1))
207207

208+
zed_notify_ntfy "${subject}" "${pathname}"; rv=$?
209+
[ "${rv}" -eq 0 ] && num_success=$((num_success + 1))
210+
[ "${rv}" -eq 1 ] && num_failure=$((num_failure + 1))
211+
208212
[ "${num_success}" -gt 0 ] && return 0
209213
[ "${num_failure}" -gt 0 ] && return 1
210214
return 2
@@ -527,6 +531,100 @@ zed_notify_pushover()
527531
}
528532

529533

534+
# zed_notify_ntfy (subject, pathname)
535+
#
536+
# Send a notification via Ntfy.sh <https://ntfy.sh/>.
537+
# The ntfy topic (ZED_NTFY_TOPIC) identifies the topic that the notification
538+
# will be sent to Ntfy.sh server. The ntfy url (ZED_NTFY_URL) defines the
539+
# self-hosted or provided hosted ntfy service location. The ntfy access token
540+
# <https://docs.ntfy.sh/publish/#access-tokens> (ZED_NTFY_ACCESS_TOKEN) reprsents an
541+
# access token that could be used if a topic is read/write protected. If a
542+
# topic can be written to publicaly, a ZED_NTFY_ACCESS_TOKEN is not required.
543+
#
544+
# Requires curl and sed executables to be installed in the standard PATH.
545+
#
546+
# References
547+
# https://docs.ntfy.sh
548+
#
549+
# Arguments
550+
# subject: notification subject
551+
# pathname: pathname containing the notification message (OPTIONAL)
552+
#
553+
# Globals
554+
# ZED_NTFY_TOPIC
555+
# ZED_NTFY_ACCESS_TOKEN (OPTIONAL)
556+
# ZED_NTFY_URL
557+
#
558+
# Return
559+
# 0: notification sent
560+
# 1: notification failed
561+
# 2: not configured
562+
#
563+
zed_notify_ntfy()
564+
{
565+
local subject="$1"
566+
local pathname="${2:-"/dev/null"}"
567+
local msg_body
568+
local msg_out
569+
local msg_err
570+
571+
[ -n "${ZED_NTFY_TOPIC}" ] || return 2
572+
local url="${ZED_NTFY_URL:-"https://ntfy.sh"}/${ZED_NTFY_TOPIC}"
573+
574+
if [ ! -r "${pathname}" ]; then
575+
zed_log_err "ntfy cannot read \"${pathname}\""
576+
return 1
577+
fi
578+
579+
zed_check_cmd "curl" "sed" || return 1
580+
581+
# Read the message body in.
582+
#
583+
msg_body="$(cat "${pathname}")"
584+
585+
if [ -z "${msg_body}" ]
586+
then
587+
msg_body=$subject
588+
subject=""
589+
fi
590+
591+
# Send the POST request and check for errors.
592+
#
593+
if [ -n "${ZED_NTFY_ACCESS_TOKEN}" ]; then
594+
msg_out="$( \
595+
curl \
596+
-u ":${ZED_NTFY_ACCESS_TOKEN}" \
597+
-H "Title: ${subject}" \
598+
-d "${msg_body}" \
599+
-H "Priority: high" \
600+
"${url}" \
601+
2>/dev/null \
602+
)"; rv=$?
603+
else
604+
msg_out="$( \
605+
curl \
606+
-H "Title: ${subject}" \
607+
-d "${msg_body}" \
608+
-H "Priority: high" \
609+
"${url}" \
610+
2>/dev/null \
611+
)"; rv=$?
612+
fi
613+
if [ "${rv}" -ne 0 ]; then
614+
zed_log_err "curl exit=${rv}"
615+
return 1
616+
fi
617+
msg_err="$(echo "${msg_out}" \
618+
| sed -n -e 's/.*"errors" *:.*\[\(.*\)\].*/\1/p')"
619+
if [ -n "${msg_err}" ]; then
620+
zed_log_err "ntfy \"${msg_err}"\"
621+
return 1
622+
fi
623+
return 0
624+
}
625+
626+
627+
530628
# zed_rate_limit (tag, [interval])
531629
#
532630
# Check whether an event of a given type [tag] has already occurred within the

cmd/zed/zed.d/zed.rc

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,25 @@ ZED_SYSLOG_SUBCLASS_EXCLUDE="history_event"
147147
# help silence misbehaving drives. This assumes your drive enclosure fully
148148
# supports slot power control via sysfs.
149149
#ZED_POWER_OFF_ENCLOSURE_SLOT_ON_FAULT=1
150+
151+
##
152+
# Ntfy topic
153+
# This defines which topic will receive the ntfy notification.
154+
# <https://docs.ntfy.sh/publish/>
155+
# Disabled by default; uncomment to enable.
156+
#ZED_NTFY_TOPIC=""
157+
158+
##
159+
# Ntfy access token (optional for public topics)
160+
# This defines an access token which can be used
161+
# to allow you to authenticate when sending to topics
162+
# <https://docs.ntfy.sh/publish/#access-tokens>
163+
# Disabled by default; uncomment to enable.
164+
#ZED_NTFY_ACCESS_TOKEN=""
165+
166+
##
167+
# Ntfy Service URL
168+
# This defines which service the ntfy call will be directed toward
169+
# <https://docs.ntfy.sh/install/>
170+
# https://ntfy.sh by default; uncomment to enable an alternative service url.
171+
#ZED_NTFY_URL="https://ntfy.sh"

0 commit comments

Comments
 (0)