X-Git-Url: https://arthur.barton.de/gitweb/?p=ax-unix.git;a=blobdiff_plain;f=mail%2Fwrapper%2Fmail-wrapper;h=ab5be707d70078b3f27e37456ee1eedf3b468715;hp=9097453abe36e49ec62dbd3f0e6d5e5d74fa9321;hb=1b342ca2265d8111e4d7f97cd4dbb1ff0cbf63d2;hpb=8de5b5e3d4db644274eba40b5696436406d3c81f diff --git a/mail/wrapper/mail-wrapper b/mail/wrapper/mail-wrapper index 9097453..ab5be70 100755 --- a/mail/wrapper/mail-wrapper +++ b/mail/wrapper/mail-wrapper @@ -36,6 +36,7 @@ usage() { echo " -C Use the \"C\" locale, no localized (error) messages." echo " --errors|-e Generate email on errors only." echo " --from|-f Email address of the sender of the email." + echo " --stderr-is-warning|-W Exit code indicates error; stderr is only warning." echo " --subject|-s Subject for the email." echo " --to|-t
Email address to send the email to." echo @@ -50,12 +51,33 @@ syntax_error() { usage 2 } +clean_up() { + if [[ -z "$proc_fd_works" ]]; then + ax_debug "Cleaning temporary files ..." + [[ -n "$buffer_file" ]] && rm -f "$buffer_file" + [[ -n "$error_file" ]] && rm -f "$error_file" + fi +} + +case "$(uname)" in + "Darwin") + unset proc_fd_works + ;; + *) + proc_fd_works=1 +esac + # Initialize internal state. -unset is_error +buffer_file="" +error_file="" +error_level=0 host=$(hostname -f 2>/dev/null || hostname) +trap clean_up EXIT + # Some defaults (can be adjusted by command line parameters). unset do_errors_only +unset stderr_is_warning unset subject from="${LOGNAME:-root} <${LOGNAME:-root}@$host>" to="$from" @@ -86,6 +108,9 @@ while [[ $# -gt 0 ]]; do [[ $# -gt 0 ]] || syntax_error subject="$1" ;; + "--stderr-is-warning"|"-W") + stderr_is_warning=1 + ;; "--suppress-empty") # Ignore this switch for compatibility with an other # "mail-wrapper" script. This is the default anyway! @@ -115,9 +140,11 @@ buffer_file=$(mktemp) \ ax_debug "buffer_file=\"$buffer_file\"" exec 3>"$buffer_file" \ || ax_abort -l "Failed to redirect FD #3 to buffer file!" -rm "$buffer_file" \ - || ax_error -l "Failed to delete buffer file: \"$buffer_file\"!" -buffer_file="/dev/fd/3" +if [[ -n "$proc_fd_works" ]]; then + rm "$buffer_file" \ + || ax_error -l "Failed to delete buffer file: \"$buffer_file\"!" + buffer_file="/dev/fd/3" +fi if [[ $# -gt 0 ]]; then # Execute command and save output in buffer file. @@ -127,9 +154,11 @@ if [[ $# -gt 0 ]]; then ax_debug "error_file=\"$error_file\"" exec 4>"$error_file" \ || ax_abort -l "Failed to redirect FD #4 to error file!" - rm "$error_file" \ - || ax_error -l "Failed to delete error buffer file: \"$error_file\"!" - error_file="/dev/fd/4" + if [[ -n "$proc_fd_works" ]]; then + rm "$error_file" \ + || ax_error -l "Failed to delete error buffer file: \"$error_file\"!" + error_file="/dev/fd/4" + fi job=$(basename "$1") @@ -153,16 +182,30 @@ fi ax_debug "exit_code=$exit_code" +declare -i count_all count_err count_all=$(wc -l <"$buffer_file" || ax_abort -l "Failed to count buffer file!") count_err=$(wc -l <"$error_file" || ax_abort -l "Failed to count error file!") # Error or no error -- that's the question! An error is assumed when either the # exit code of the command was non-zero or there was output to stderr. -[[ $count_err -gt 0 || $exit_code -ne 0 ]] && is_error=1 +# But when stderr_is_warning is set, messages on stderr result on a warning only! +if [[ $exit_code -ne 0 ]]; then + error_level=2 +elif [[ $count_err -gt 0 ]]; then + [[ -n $stderr_is_warning ]] && error_level=1 || error_level=2 +else + error_level=0 +fi # Construct email subject ... [[ -z "$subject" ]] && subject="$host: $job report" -[[ -n "$is_error" ]] && subject="$subject - ERROR!" || subject="$subject - success" +if [[ "$error_level" -eq 0 ]]; then + subject="$subject - success" +elif [[ "$error_level" -eq 1 ]]; then + subject="$subject - WARNING!" +else + subject="$subject - ERROR!" +fi ax_debug "from=\"$from\"" ax_debug "to=\"$to\"" @@ -178,14 +221,14 @@ fi ax_debug "count_all=$count_all" ax_debug "count_err=$count_err" -ax_debug "is_error=$is_error" +ax_debug "error_level=$error_level" # No errors detected (exit code & stderr), and email should be sent on errors # only: so exit early! -[[ -z "$is_error" && -n "$do_errors_only" ]] && exit $exit_code +[[ "$error_level" -lt 2 && -n "$do_errors_only" ]] && exit $exit_code # No error detected and no output at all: skip email, exit early: -[[ -z "$is_error" && $count_all -eq 0 ]] && exit $exit_code +[[ "$error_level" -eq 0 && $count_all -eq 0 ]] && exit $exit_code # Build the report mail. # Make sure to ignore all mail(1) configuration files, system wide /etc/mailrc