|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Error notification sendmail script |
| 4 | +# |
| 5 | +help() { |
| 6 | + cat - <<HELP |
| 7 | + Usage $1 [options] user1@domain user2@domain ... |
| 8 | + Options: |
| 9 | + -s Only send mail when operating on schedule (RESTICPROFILE_ON_SCHEDULE=1) |
| 10 | + -c command Set the profile command (instead of PROFILE_COMMAND) |
| 11 | + -n name Set the profile name (instead of PROFILE_NAME) |
| 12 | + -p Print mail to stdout instead of sending it |
| 13 | + -f Send mail even when no profile name is specified |
| 14 | +HELP |
| 15 | +} |
| 16 | + |
| 17 | +# Parse CLI args |
| 18 | +FORCE_SENDING=0 |
| 19 | +SEND_COMMAND="" |
| 20 | +while getopts 'c:fhn:ps' flag ; do |
| 21 | + case "${flag}" in |
| 22 | + c) PROFILE_COMMAND="${OPTARG}" ;; |
| 23 | + f) FORCE_SENDING=1 ;; |
| 24 | + n) PROFILE_NAME="${OPTARG}" ;; |
| 25 | + p) SEND_COMMAND="cat -" ;; |
| 26 | + s) (( ${RESTICPROFILE_ON_SCHEDULE:-0} > 0 )) || exit 0 ;; |
| 27 | + *) help "$0" ; exit 0 ;; |
| 28 | + esac |
| 29 | +done |
| 30 | +shift $((OPTIND-1)) |
| 31 | + |
| 32 | +# Parameters |
| 33 | +MAIL_TO="" |
| 34 | +MAIL_FROM="\"resticprofile $(hostname -f)\" <$USER@$(hostname -f)>" |
| 35 | +MAIL_SUBJECT="restic failed: \"${PROFILE_COMMAND}\" in \"${PROFILE_NAME}\"" |
| 36 | + |
| 37 | +SEND_COMMAND="${SEND_COMMAND:-sendmail -t}" |
| 38 | + |
| 39 | +DETAILS_COMMAND_RESULT="" |
| 40 | +DETAILS_COMMAND="" |
| 41 | + |
| 42 | +# Get command to capture output from scheduler ( if in use ) |
| 43 | +if [[ -d /etc/systemd/ ]] \ |
| 44 | + && (( ${RESTICPROFILE_ON_SCHEDULE:-0} > 0 )) \ |
| 45 | + && resticprofile --name "${PROFILE_NAME}" show | grep -v -q -E "scheduler:\s*cron" ; then |
| 46 | + DETAILS_COMMAND="systemctl status --full \"resticprofile-${PROFILE_COMMAND:-*}@profile-${PROFILE_NAME:-*}\"" |
| 47 | +fi |
| 48 | + |
| 49 | +# Load parameter overrides |
| 50 | +RC_FILE="/etc/resticprofile/$(basename "$0").rc}" |
| 51 | +[[ -f "${RC_FILE}" ]] && source "${RC_FILE}" |
| 52 | + |
| 53 | +main() { |
| 54 | + if [[ -n "${PROFILE_NAME}" || "${FORCE_SENDING}" == "1" ]] ; then |
| 55 | + if [[ -n "${DETAILS_COMMAND}" ]] ; then |
| 56 | + DETAILS_COMMAND_RESULT="$(${DETAILS_COMMAND})" |
| 57 | + fi |
| 58 | + |
| 59 | + for email in "$@" "${MAIL_TO}" ; do |
| 60 | + if [[ "${email}" =~ ^[a-zA-Z0-9_.%+-]+@[a-zA-Z0-9_]+[a-zA-Z0-9_.-]+$ ]] ; then |
| 61 | + send_mail "${email}" || echo "Failed sending to \"${email}\"" |
| 62 | + elif [[ -n "${email}" ]] ; then |
| 63 | + echo "Skipping notification for invalid address \"${email}\"" |
| 64 | + fi |
| 65 | + done |
| 66 | + fi |
| 67 | + return 0 |
| 68 | +} |
| 69 | + |
| 70 | +send_mail() { |
| 71 | + ${SEND_COMMAND} <<ERRMAIL |
| 72 | +To: $1 |
| 73 | +From: $MAIL_FROM |
| 74 | +Subject: $MAIL_SUBJECT |
| 75 | +Content-Transfer-Encoding: 8bit |
| 76 | +Content-Type: text/plain; charset=UTF-8 |
| 77 | +
|
| 78 | +${ERROR:-No error information available} |
| 79 | +
|
| 80 | +---- |
| 81 | +COMMANDLINE: |
| 82 | +
|
| 83 | +${ERROR_COMMANDLINE:-N/A} |
| 84 | +
|
| 85 | +---- |
| 86 | +STDERR: |
| 87 | +
|
| 88 | +${ERROR_STDERR:-N/A} |
| 89 | +
|
| 90 | +---- |
| 91 | +DETAILS: |
| 92 | +
|
| 93 | +${DETAILS_COMMAND_RESULT:-N/A} |
| 94 | +
|
| 95 | +---- |
| 96 | +CONFIG: |
| 97 | +
|
| 98 | +$(resticprofile --name "${PROFILE_NAME}" show) |
| 99 | +
|
| 100 | +ERRMAIL |
| 101 | +} |
| 102 | + |
| 103 | +# Invoke main and exit without error to ensure other error handlers run |
| 104 | +main "$@" |
| 105 | +exit 0 |
0 commit comments