From d7abf805355ce9ddeb2f95c055f90b0d52697980 Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Sat, 17 Sep 2022 10:23:42 +0000 Subject: [PATCH] mail scripts : add attachment support, use it for too long mails --- mail.sh | 8 +++++++- mail_wrap_script.sh | 10 ++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/mail.sh b/mail.sh index a189c00..86ae88b 100755 --- a/mail.sh +++ b/mail.sh @@ -6,4 +6,10 @@ if [ -f "/root/.host_mail" ]; then fi SUBJECT=$1 -cat | mailx -s "$SUBJECT" -r "$MAILFROM" "$MAILTO" + +ATTACHMENT="" +if [ ! -z "$2" ]; then + ATTACHMENT="-a $2" +fi + +cat | mailx -s "$SUBJECT" -r "$MAILFROM" $ATTACHMENT "$MAILTO" diff --git a/mail_wrap_script.sh b/mail_wrap_script.sh index f118486..6f83302 100755 --- a/mail_wrap_script.sh +++ b/mail_wrap_script.sh @@ -62,7 +62,10 @@ if [ $ERR -ne 0 ] && [ "$ERR" != "$IGNORE_EXIT_CODE" ]; then echo "Sent" else echo "Could not send mail, sending safe-body mail..." - echo -e "An error occured during the execution of $SCRIPT ($ERR).\n\n\n$(ps $PPID)\n\n(Could not send full output, see dead.letter)" | $(dirname $0)/mail.sh "[E+M] $(hostname) $SCRIPT" + attachment=$(mktemp log.XXXXXXXXX.txt.zstd) + echo -e "$OUTPUT" | zstd > $attachment + echo -e "An error occured during the execution of $SCRIPT ($ERR).\n\n\n$(ps $PPID)\n\n(Too long log was attached as a file)" | $(dirname $0)/mail.sh "[E] $(hostname) $SCRIPT" "$attachment" + rm "$attachment" echo "Sent" fi exit 1 @@ -74,7 +77,10 @@ elif [ $ALWAYS_SEND -ne 0 ]; then echo "Sent" else echo "Could not send mail, sending safe-body mail..." - echo -e "Logging execution of $SCRIPT ($ERR).\n\n\n$(ps $PPID)\n\n(Could not send full output, see dead.letter)" | $(dirname $0)/mail.sh "[I/M] $(hostname) $SCRIPT" + attachment=$(mktemp log.XXXXXXXXX.txt.zstd) + echo -e "$OUTPUT" | zstd > $attachment + echo -e "Logging execution of $SCRIPT ($ERR).\n\n\n$(ps $PPID)\n\n(Too long log was attached as a file)" | $(dirname $0)/mail.sh "[I] $(hostname) $SCRIPT" "$attachment" + rm "$attachment" echo "Sent" fi fi