zdb: fix printf() length for uint64_t devid
[zfs.git] / cmd / zed / zed.d / zed-functions.sh
blob49b6b54029aa2522904a180a19fa1a86b6dcfda6
1 #!/bin/sh
2 # shellcheck disable=SC2154,SC3043
3 # zed-functions.sh
5 # ZED helper functions for use in ZEDLETs
8 # Variable Defaults
10 : "${ZED_LOCKDIR:="/var/lock"}"
11 : "${ZED_NOTIFY_INTERVAL_SECS:=3600}"
12 : "${ZED_NOTIFY_VERBOSE:=0}"
13 : "${ZED_RUNDIR:="/var/run"}"
14 : "${ZED_SYSLOG_PRIORITY:="daemon.notice"}"
15 : "${ZED_SYSLOG_TAG:="zed"}"
17 ZED_FLOCK_FD=8
20 # zed_check_cmd (cmd, ...)
22 # For each argument given, search PATH for the executable command [cmd].
23 # Log a message if [cmd] is not found.
25 # Arguments
26 # cmd: name of executable command for which to search
28 # Return
29 # 0 if all commands are found in PATH and are executable
30 # n for a count of the command executables that are not found
32 zed_check_cmd()
34 local cmd
35 local rv=0
37 for cmd; do
38 if ! command -v "${cmd}" >/dev/null 2>&1; then
39 zed_log_err "\"${cmd}\" not installed"
40 rv=$((rv + 1))
42 done
43 return "${rv}"
47 # zed_log_msg (msg, ...)
49 # Write all argument strings to the system log.
51 # Globals
52 # ZED_SYSLOG_PRIORITY
53 # ZED_SYSLOG_TAG
55 # Return
56 # nothing
58 zed_log_msg()
60 logger -p "${ZED_SYSLOG_PRIORITY}" -t "${ZED_SYSLOG_TAG}" -- "$@"
64 # zed_log_err (msg, ...)
66 # Write an error message to the system log. This message will contain the
67 # script name, EID, and all argument strings.
69 # Globals
70 # ZED_SYSLOG_PRIORITY
71 # ZED_SYSLOG_TAG
72 # ZEVENT_EID
74 # Return
75 # nothing
77 zed_log_err()
79 zed_log_msg "error: ${0##*/}:""${ZEVENT_EID:+" eid=${ZEVENT_EID}:"}" "$@"
83 # zed_lock (lockfile, [fd])
85 # Obtain an exclusive (write) lock on [lockfile]. If the lock cannot be
86 # immediately acquired, wait until it becomes available.
88 # Every zed_lock() must be paired with a corresponding zed_unlock().
90 # By default, flock-style locks associate the lockfile with file descriptor 8.
91 # The bash manpage warns that file descriptors >9 should be used with care as
92 # they may conflict with file descriptors used internally by the shell. File
93 # descriptor 9 is reserved for zed_rate_limit(). If concurrent locks are held
94 # within the same process, they must use different file descriptors (preferably
95 # decrementing from 8); otherwise, obtaining a new lock with a given file
96 # descriptor will release the previous lock associated with that descriptor.
98 # Arguments
99 # lockfile: pathname of the lock file; the lock will be stored in
100 # ZED_LOCKDIR unless the pathname contains a "/".
101 # fd: integer for the file descriptor used by flock (OPTIONAL unless holding
102 # concurrent locks)
104 # Globals
105 # ZED_FLOCK_FD
106 # ZED_LOCKDIR
108 # Return
109 # nothing
111 zed_lock()
113 local lockfile="$1"
114 local fd="${2:-${ZED_FLOCK_FD}}"
115 local umask_bak
116 local err
118 [ -n "${lockfile}" ] || return
119 if ! expr "${lockfile}" : '.*/' >/dev/null 2>&1; then
120 lockfile="${ZED_LOCKDIR}/${lockfile}"
123 umask_bak="$(umask)"
124 umask 077
126 # Obtain a lock on the file bound to the given file descriptor.
128 eval "exec ${fd}>> '${lockfile}'"
129 if ! err="$(flock --exclusive "${fd}" 2>&1)"; then
130 zed_log_err "failed to lock \"${lockfile}\": ${err}"
133 umask "${umask_bak}"
137 # zed_unlock (lockfile, [fd])
139 # Release the lock on [lockfile].
141 # Arguments
142 # lockfile: pathname of the lock file
143 # fd: integer for the file descriptor used by flock (must match the file
144 # descriptor passed to the zed_lock function call)
146 # Globals
147 # ZED_FLOCK_FD
148 # ZED_LOCKDIR
150 # Return
151 # nothing
153 zed_unlock()
155 local lockfile="$1"
156 local fd="${2:-${ZED_FLOCK_FD}}"
157 local err
159 [ -n "${lockfile}" ] || return
160 if ! expr "${lockfile}" : '.*/' >/dev/null 2>&1; then
161 lockfile="${ZED_LOCKDIR}/${lockfile}"
164 # Release the lock and close the file descriptor.
165 if ! err="$(flock --unlock "${fd}" 2>&1)"; then
166 zed_log_err "failed to unlock \"${lockfile}\": ${err}"
168 eval "exec ${fd}>&-"
172 # zed_notify (subject, pathname)
174 # Send a notification via all available methods.
176 # Arguments
177 # subject: notification subject
178 # pathname: pathname containing the notification message (OPTIONAL)
180 # Return
181 # 0: notification succeeded via at least one method
182 # 1: notification failed
183 # 2: no notification methods configured
185 zed_notify()
187 local subject="$1"
188 local pathname="$2"
189 local num_success=0
190 local num_failure=0
192 zed_notify_email "${subject}" "${pathname}"; rv=$?
193 [ "${rv}" -eq 0 ] && num_success=$((num_success + 1))
194 [ "${rv}" -eq 1 ] && num_failure=$((num_failure + 1))
196 zed_notify_pushbullet "${subject}" "${pathname}"; rv=$?
197 [ "${rv}" -eq 0 ] && num_success=$((num_success + 1))
198 [ "${rv}" -eq 1 ] && num_failure=$((num_failure + 1))
200 zed_notify_slack_webhook "${subject}" "${pathname}"; rv=$?
201 [ "${rv}" -eq 0 ] && num_success=$((num_success + 1))
202 [ "${rv}" -eq 1 ] && num_failure=$((num_failure + 1))
204 zed_notify_pushover "${subject}" "${pathname}"; rv=$?
205 [ "${rv}" -eq 0 ] && num_success=$((num_success + 1))
206 [ "${rv}" -eq 1 ] && num_failure=$((num_failure + 1))
208 [ "${num_success}" -gt 0 ] && return 0
209 [ "${num_failure}" -gt 0 ] && return 1
210 return 2
214 # zed_notify_email (subject, pathname)
216 # Send a notification via email to the address specified by ZED_EMAIL_ADDR.
218 # Requires the mail executable to be installed in the standard PATH, or
219 # ZED_EMAIL_PROG to be defined with the pathname of an executable capable of
220 # reading a message body from stdin.
222 # Command-line options to the mail executable can be specified in
223 # ZED_EMAIL_OPTS. This undergoes the following keyword substitutions:
224 # - @ADDRESS@ is replaced with the space-delimited recipient email address(es)
225 # - @SUBJECT@ is replaced with the notification subject
226 # If @SUBJECT@ was omited here, a "Subject: ..." header will be added to notification
229 # Arguments
230 # subject: notification subject
231 # pathname: pathname containing the notification message (OPTIONAL)
233 # Globals
234 # ZED_EMAIL_PROG
235 # ZED_EMAIL_OPTS
236 # ZED_EMAIL_ADDR
238 # Return
239 # 0: notification sent
240 # 1: notification failed
241 # 2: not configured
243 zed_notify_email()
245 local subject="${1:-"ZED notification"}"
246 local pathname="${2:-"/dev/null"}"
248 : "${ZED_EMAIL_PROG:="mail"}"
249 : "${ZED_EMAIL_OPTS:="-s '@SUBJECT@' @ADDRESS@"}"
251 # For backward compatibility with ZED_EMAIL.
252 if [ -n "${ZED_EMAIL}" ] && [ -z "${ZED_EMAIL_ADDR}" ]; then
253 ZED_EMAIL_ADDR="${ZED_EMAIL}"
255 [ -n "${ZED_EMAIL_ADDR}" ] || return 2
257 zed_check_cmd "${ZED_EMAIL_PROG}" || return 1
259 [ -n "${subject}" ] || return 1
260 if [ ! -r "${pathname}" ]; then
261 zed_log_err \
262 "${ZED_EMAIL_PROG##*/} cannot read \"${pathname}\""
263 return 1
266 # construct cmdline options
267 ZED_EMAIL_OPTS_PARSED="$(echo "${ZED_EMAIL_OPTS}" \
268 | sed -e "s/@ADDRESS@/${ZED_EMAIL_ADDR}/g" \
269 -e "s/@SUBJECT@/${subject}/g")"
271 # pipe message to email prog
272 # shellcheck disable=SC2086,SC2248
274 # no subject passed as option?
275 if [ "${ZED_EMAIL_OPTS%@SUBJECT@*}" = "${ZED_EMAIL_OPTS}" ] ; then
276 # inject subject header
277 printf "Subject: %s\n" "${subject}"
279 # output message
280 cat "${pathname}"
282 eval ${ZED_EMAIL_PROG} ${ZED_EMAIL_OPTS_PARSED} >/dev/null 2>&1
283 rv=$?
284 if [ "${rv}" -ne 0 ]; then
285 zed_log_err "${ZED_EMAIL_PROG##*/} exit=${rv}"
286 return 1
288 return 0
292 # zed_notify_pushbullet (subject, pathname)
294 # Send a notification via Pushbullet <https://www.pushbullet.com/>.
295 # The access token (ZED_PUSHBULLET_ACCESS_TOKEN) identifies this client to the
296 # Pushbullet server. The optional channel tag (ZED_PUSHBULLET_CHANNEL_TAG) is
297 # for pushing to notification feeds that can be subscribed to; if a channel is
298 # not defined, push notifications will instead be sent to all devices
299 # associated with the account specified by the access token.
301 # Requires awk, curl, and sed executables to be installed in the standard PATH.
303 # References
304 # https://docs.pushbullet.com/
305 # https://www.pushbullet.com/security
307 # Arguments
308 # subject: notification subject
309 # pathname: pathname containing the notification message (OPTIONAL)
311 # Globals
312 # ZED_PUSHBULLET_ACCESS_TOKEN
313 # ZED_PUSHBULLET_CHANNEL_TAG
315 # Return
316 # 0: notification sent
317 # 1: notification failed
318 # 2: not configured
320 zed_notify_pushbullet()
322 local subject="$1"
323 local pathname="${2:-"/dev/null"}"
324 local msg_body
325 local msg_tag
326 local msg_json
327 local msg_out
328 local msg_err
329 local url="https://api.pushbullet.com/v2/pushes"
331 [ -n "${ZED_PUSHBULLET_ACCESS_TOKEN}" ] || return 2
333 [ -n "${subject}" ] || return 1
334 if [ ! -r "${pathname}" ]; then
335 zed_log_err "pushbullet cannot read \"${pathname}\""
336 return 1
339 zed_check_cmd "awk" "curl" "sed" || return 1
341 # Escape the following characters in the message body for JSON:
342 # newline, backslash, double quote, horizontal tab, vertical tab,
343 # and carriage return.
345 msg_body="$(awk '{ ORS="\\n" } { gsub(/\\/, "\\\\"); gsub(/"/, "\\\"");
346 gsub(/\t/, "\\t"); gsub(/\f/, "\\f"); gsub(/\r/, "\\r"); print }' \
347 "${pathname}")"
349 # Push to a channel if one is configured.
351 [ -n "${ZED_PUSHBULLET_CHANNEL_TAG}" ] && msg_tag="$(printf \
352 '"channel_tag": "%s", ' "${ZED_PUSHBULLET_CHANNEL_TAG}")"
354 # Construct the JSON message for pushing a note.
356 msg_json="$(printf '{%s"type": "note", "title": "%s", "body": "%s"}' \
357 "${msg_tag}" "${subject}" "${msg_body}")"
359 # Send the POST request and check for errors.
361 msg_out="$(curl -u "${ZED_PUSHBULLET_ACCESS_TOKEN}:" -X POST "${url}" \
362 --header "Content-Type: application/json" --data-binary "${msg_json}" \
363 2>/dev/null)"; rv=$?
364 if [ "${rv}" -ne 0 ]; then
365 zed_log_err "curl exit=${rv}"
366 return 1
368 msg_err="$(echo "${msg_out}" \
369 | sed -n -e 's/.*"error" *:.*"message" *: *"\([^"]*\)".*/\1/p')"
370 if [ -n "${msg_err}" ]; then
371 zed_log_err "pushbullet \"${msg_err}"\"
372 return 1
374 return 0
378 # zed_notify_slack_webhook (subject, pathname)
380 # Notification via Slack Webhook <https://api.slack.com/incoming-webhooks>.
381 # The Webhook URL (ZED_SLACK_WEBHOOK_URL) identifies this client to the
382 # Slack channel.
384 # Requires awk, curl, and sed executables to be installed in the standard PATH.
386 # References
387 # https://api.slack.com/incoming-webhooks
389 # Arguments
390 # subject: notification subject
391 # pathname: pathname containing the notification message (OPTIONAL)
393 # Globals
394 # ZED_SLACK_WEBHOOK_URL
396 # Return
397 # 0: notification sent
398 # 1: notification failed
399 # 2: not configured
401 zed_notify_slack_webhook()
403 [ -n "${ZED_SLACK_WEBHOOK_URL}" ] || return 2
405 local subject="$1"
406 local pathname="${2:-"/dev/null"}"
407 local msg_body
408 local msg_tag
409 local msg_json
410 local msg_out
411 local msg_err
412 local url="${ZED_SLACK_WEBHOOK_URL}"
414 [ -n "${subject}" ] || return 1
415 if [ ! -r "${pathname}" ]; then
416 zed_log_err "slack webhook cannot read \"${pathname}\""
417 return 1
420 zed_check_cmd "awk" "curl" "sed" || return 1
422 # Escape the following characters in the message body for JSON:
423 # newline, backslash, double quote, horizontal tab, vertical tab,
424 # and carriage return.
426 msg_body="$(awk '{ ORS="\\n" } { gsub(/\\/, "\\\\"); gsub(/"/, "\\\"");
427 gsub(/\t/, "\\t"); gsub(/\f/, "\\f"); gsub(/\r/, "\\r"); print }' \
428 "${pathname}")"
430 # Construct the JSON message for posting.
432 msg_json="$(printf '{"text": "*%s*\\n%s"}' "${subject}" "${msg_body}" )"
434 # Send the POST request and check for errors.
436 msg_out="$(curl -X POST "${url}" \
437 --header "Content-Type: application/json" --data-binary "${msg_json}" \
438 2>/dev/null)"; rv=$?
439 if [ "${rv}" -ne 0 ]; then
440 zed_log_err "curl exit=${rv}"
441 return 1
443 msg_err="$(echo "${msg_out}" \
444 | sed -n -e 's/.*"error" *:.*"message" *: *"\([^"]*\)".*/\1/p')"
445 if [ -n "${msg_err}" ]; then
446 zed_log_err "slack webhook \"${msg_err}"\"
447 return 1
449 return 0
452 # zed_notify_pushover (subject, pathname)
454 # Send a notification via Pushover <https://pushover.net/>.
455 # The access token (ZED_PUSHOVER_TOKEN) identifies this client to the
456 # Pushover server. The user token (ZED_PUSHOVER_USER) defines the user or
457 # group to which the notification will be sent.
459 # Requires curl and sed executables to be installed in the standard PATH.
461 # References
462 # https://pushover.net/api
464 # Arguments
465 # subject: notification subject
466 # pathname: pathname containing the notification message (OPTIONAL)
468 # Globals
469 # ZED_PUSHOVER_TOKEN
470 # ZED_PUSHOVER_USER
472 # Return
473 # 0: notification sent
474 # 1: notification failed
475 # 2: not configured
477 zed_notify_pushover()
479 local subject="$1"
480 local pathname="${2:-"/dev/null"}"
481 local msg_body
482 local msg_out
483 local msg_err
484 local url="https://api.pushover.net/1/messages.json"
486 [ -n "${ZED_PUSHOVER_TOKEN}" ] && [ -n "${ZED_PUSHOVER_USER}" ] || return 2
488 if [ ! -r "${pathname}" ]; then
489 zed_log_err "pushover cannot read \"${pathname}\""
490 return 1
493 zed_check_cmd "curl" "sed" || return 1
495 # Read the message body in.
497 msg_body="$(cat "${pathname}")"
499 if [ -z "${msg_body}" ]
500 then
501 msg_body=$subject
502 subject=""
505 # Send the POST request and check for errors.
507 msg_out="$( \
508 curl \
509 --form-string "token=${ZED_PUSHOVER_TOKEN}" \
510 --form-string "user=${ZED_PUSHOVER_USER}" \
511 --form-string "message=${msg_body}" \
512 --form-string "title=${subject}" \
513 "${url}" \
514 2>/dev/null \
515 )"; rv=$?
516 if [ "${rv}" -ne 0 ]; then
517 zed_log_err "curl exit=${rv}"
518 return 1
520 msg_err="$(echo "${msg_out}" \
521 | sed -n -e 's/.*"errors" *:.*\[\(.*\)\].*/\1/p')"
522 if [ -n "${msg_err}" ]; then
523 zed_log_err "pushover \"${msg_err}"\"
524 return 1
526 return 0
530 # zed_rate_limit (tag, [interval])
532 # Check whether an event of a given type [tag] has already occurred within the
533 # last [interval] seconds.
535 # This function obtains a lock on the statefile using file descriptor 9.
537 # Arguments
538 # tag: arbitrary string for grouping related events to rate-limit
539 # interval: time interval in seconds (OPTIONAL)
541 # Globals
542 # ZED_NOTIFY_INTERVAL_SECS
543 # ZED_RUNDIR
545 # Return
546 # 0 if the event should be processed
547 # 1 if the event should be dropped
549 # State File Format
550 # time;tag
552 zed_rate_limit()
554 local tag="$1"
555 local interval="${2:-${ZED_NOTIFY_INTERVAL_SECS}}"
556 local lockfile="zed.zedlet.state.lock"
557 local lockfile_fd=9
558 local statefile="${ZED_RUNDIR}/zed.zedlet.state"
559 local time_now
560 local time_prev
561 local umask_bak
562 local rv=0
564 [ -n "${tag}" ] || return 0
566 zed_lock "${lockfile}" "${lockfile_fd}"
567 time_now="$(date +%s)"
568 time_prev="$(grep -E "^[0-9]+;${tag}\$" "${statefile}" 2>/dev/null \
569 | tail -1 | cut -d\; -f1)"
571 if [ -n "${time_prev}" ] \
572 && [ "$((time_now - time_prev))" -lt "${interval}" ]; then
573 rv=1
574 else
575 umask_bak="$(umask)"
576 umask 077
577 grep -E -v "^[0-9]+;${tag}\$" "${statefile}" 2>/dev/null \
578 > "${statefile}.$$"
579 echo "${time_now};${tag}" >> "${statefile}.$$"
580 mv -f "${statefile}.$$" "${statefile}"
581 umask "${umask_bak}"
584 zed_unlock "${lockfile}" "${lockfile_fd}"
585 return "${rv}"
589 # zed_guid_to_pool (guid)
591 # Convert a pool GUID into its pool name (like "tank")
592 # Arguments
593 # guid: pool GUID (decimal or hex)
595 # Return
596 # Pool name
598 zed_guid_to_pool()
600 if [ -z "$1" ] ; then
601 return
604 guid="$(printf "%u" "$1")"
605 $ZPOOL get -H -ovalue,name guid | awk '$1 == '"$guid"' {print $2; exit}'
608 # zed_exit_if_ignoring_this_event
610 # Exit the script if we should ignore this event, as determined by
611 # $ZED_SYSLOG_SUBCLASS_INCLUDE and $ZED_SYSLOG_SUBCLASS_EXCLUDE in zed.rc.
612 # This function assumes you've imported the normal zed variables.
613 zed_exit_if_ignoring_this_event()
615 if [ -n "${ZED_SYSLOG_SUBCLASS_INCLUDE}" ]; then
616 eval "case ${ZEVENT_SUBCLASS} in
617 ${ZED_SYSLOG_SUBCLASS_INCLUDE});;
618 *) exit 0;;
619 esac"
620 elif [ -n "${ZED_SYSLOG_SUBCLASS_EXCLUDE}" ]; then
621 eval "case ${ZEVENT_SUBCLASS} in
622 ${ZED_SYSLOG_SUBCLASS_EXCLUDE}) exit 0;;
623 *);;
624 esac"