3 # mail-wrapper -- Report results of a command by email
4 # Copyright (c)2017 Alexander Barton (alex@barton.de)
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 2 of the License, or
9 # (at your option) any later version.
14 # Include "ax-common.sh":
16 for dir in "$HOME/lib" "$HOME/.ax" /usr/local /opt/ax /usr; do
17 [ -z "$ax_common_sourced" ] || break
18 ax_common="${dir}/lib/ax/ax-common.sh"
19 # shellcheck source=/usr/local/lib/ax/ax-common.sh
20 [ -r "$ax_common" ] && . "$ax_common"
22 if [ -z "$ax_common_sourced" ]; then
23 echo "Error ($NAME): \"ax-common.sh\" not found, aborting!" >&2
24 echo "Please install 'ax-unix', \"Alex' UNIX Tools & Scripts\", and try again."
27 unset dir ax_common ax_common_sourced
33 echo " $NAME [--help|--usage]"
34 echo " $NAME {parameters} [<command> [<arg> [<…>]]]"
36 echo " -C Use the \"C\" locale, no localized (error) messages."
37 echo " --errors|-e Generate email on errors only."
38 echo " --from|-f Email address of the sender of the email."
39 echo " --subject|-s <subject> Subject for the email."
40 echo " --to|-t <address> Email address to send the email to."
42 echo "When no <command> is given, $NAME reads from standard input."
49 ax_error -l "Syntax error!"
53 # Initialize internal state.
55 host=$(hostname -f 2>/dev/null || hostname)
57 # Some defaults (can be adjusted by command line parameters).
60 from="${LOGNAME:-root} <${LOGNAME:-root}@$host>"
63 # Parse the command line ...
64 while [[ $# -gt 0 ]]; do
78 [[ $# -gt 0 ]] || syntax_error
86 [[ $# -gt 0 ]] || syntax_error
90 # Ignore this switch for compatibility with an other
91 # "mail-wrapper" script. This is the default anyway!
95 [[ $# -gt 0 ]] || syntax_error
102 # Command to execute follows in command line.
109 # Initialize the "buffer file" on file handle #3. This file will store all
110 # output, stdout and stderr combined. The file is immediately unliked so that
111 # we can't leak stale files. Afterwards this script accesses the "buffer file"
112 # by its file descriptor only.
113 buffer_file=$(mktemp) \
114 || ax_abort -l "Failed to create buffer file: \"$buffer_file\"!"
115 ax_debug "buffer_file=\"$buffer_file\""
116 exec 3>"$buffer_file" \
117 || ax_abort -l "Failed to redirect FD #3 to buffer file!"
119 || ax_error -l "Failed to delete buffer file: \"$buffer_file\"!"
120 buffer_file="/dev/fd/3"
122 if [[ $# -gt 0 ]]; then
123 # Execute command and save output in buffer file.
124 # Use a sub-shell to not pollute our name space!
125 error_file=$(mktemp) \
126 || ax_abort -l "Failed to create error buffer file: \"$error_file\"!"
127 ax_debug "error_file=\"$error_file\""
128 exec 4>"$error_file" \
129 || ax_abort -l "Failed to redirect FD #4 to error file!"
131 || ax_error -l "Failed to delete error buffer file: \"$error_file\"!"
132 error_file="/dev/fd/4"
136 ax_debug "Running command \"$*\" ..."
138 "$@" 2>&1 1>&3 | tee "$error_file" >&3
139 echo "${PIPESTATUS[0]}"
142 # Read from stdin and save it to the buffer file.
143 error_file="/dev/null"
146 ax_debug "Reading from stdin ..."
147 while read -r line; do
149 || ax_abort -l "Failed to write to buffer file!"
154 ax_debug "exit_code=$exit_code"
156 count_all=$(wc -l <"$buffer_file" || ax_abort -l "Failed to count buffer file!")
157 count_err=$(wc -l <"$error_file" || ax_abort -l "Failed to count error file!")
159 # Error or no error -- that's the question! An error is assumed when either the
160 # exit code of the command was non-zero or there was output to stderr.
161 [[ $count_err -gt 0 || $exit_code -ne 0 ]] && is_error=1
163 # Construct email subject ...
164 [[ -z "$subject" ]] && subject="$host: $job report"
165 [[ -n "$is_error" ]] && subject="$subject - ERROR!" || subject="$subject - success"
167 ax_debug "from=\"$from\""
168 ax_debug "to=\"$to\""
169 ax_debug "subject=$subject"
171 if [[ -n "$DEBUG" ]]; then
172 echo "--- stdout+stderr ---"
174 echo "--- stderr ---"
179 ax_debug "count_all=$count_all"
180 ax_debug "count_err=$count_err"
181 ax_debug "is_error=$is_error"
183 # No errors detected (exit code & stderr), and email should be sent on errors
184 # only: so exit early!
185 [[ -z "$is_error" && -n "$do_errors_only" ]] && exit $exit_code
187 # No error detected and no output at all: skip email, exit early:
188 [[ -z "$is_error" && $count_all -eq 0 ]] && exit $exit_code
190 # Build the report mail.
191 # Make sure to ignore all mail(1) configuration files, system wide /etc/mailrc
192 # (by using the "-n" option) as well as ~/.mailrc (by setting the MAILRC
193 # environment varialbe).
194 export MAILRC=/dev/null
198 echo " - Host: $host"
199 echo " - User: $(id -un)"
200 echo " - Exit code: $exit_code"
202 if [[ $# -gt 0 ]]; then
203 # A command name is known (not stdin), show it!
208 if [[ $count_err -gt 0 ]]; then
209 # Prefix mail with all error messages.
210 echo "Error summary:"
211 echo "-----------------------------------------------------------------------------"
213 || ax_abort -l "Failed to dump error file!"
214 echo "-----------------------------------------------------------------------------"
217 if [[ $count_all -ne $count_err ]]; then
218 # Show full output when different to "error output" only.
220 || ax_abort -l "Failed to dump buffer file!"
222 ) | mail -n -a "From: $from" -s "$subject" "$to" \
223 || ax_abort -l "Failed to send email to \"$to\"!"