2019-12-18 21:56:16 +01:00
#!/bin/bash
2020-03-20 13:04:22 +01:00
# Default values
IGNORE_EXIST_CODE = 0
SCRIPT = "-1"
2020-04-15 06:47:16 +02:00
ALWAYS_SEND = 0
2019-12-18 21:56:16 +01:00
2020-03-20 13:04:22 +01:00
# Get arguments
while [ $# -gt 1 ] ; do
2020-04-15 06:47:16 +02:00
# single ARG
2020-03-20 13:04:22 +01:00
ARG_NAME = $1
shift
2020-04-15 06:47:16 +02:00
case $ARG_NAME in
--always-send)
ALWAYS_SEND = 1
continue
; ;
esac
# ARG + VALUE
2020-03-20 13:04:22 +01:00
ARG_VALUE = $1
shift
2020-04-15 06:47:16 +02:00
2020-03-20 13:04:22 +01:00
case $ARG_NAME in
--ignore-exit-code)
IGNORE_EXIT_CODE = $ARG_VALUE
; ;
--script)
SCRIPT = $ARG_VALUE
break
; ;
esac
done
# Exit if bad script
if [ " $SCRIPT " = "-1" ] ; then
echo No script specified
echo "Sending error mail"
echo -e " No script specified\n\n\n $( ps $PPID ) " | $( dirname $0 ) /mail.sh " [CRITITCAL] $( hostname) undefined script "
echo "Sent"
exit 1
fi
# Create temp logfile
2020-01-29 05:19:19 +01:00
FILE_ID = $( cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
touch /tmp/$FILE_ID .log
chmod a-rwx,u+r /tmp/$FILE_ID .log
2020-03-20 13:04:22 +01:00
# Execute script with remaining arguments
2020-01-29 05:19:19 +01:00
$SCRIPT " $@ " 2>& 1 | tee /tmp/$FILE_ID .log
2020-01-29 21:27:35 +01:00
ERR = ${ PIPESTATUS [0] }
2020-01-29 05:19:19 +01:00
OUTPUT = $( cat /tmp/$FILE_ID .log)
rm /tmp/$FILE_ID .log
2020-01-29 04:00:04 +01:00
2020-03-20 13:04:22 +01:00
# Send mail in case of error
2021-01-08 15:04:39 +01:00
if [ $ERR -ne 0 ] && [ " $ERR " != " $IGNORE_EXIT_CODE " ] ; then
2019-12-18 21:56:16 +01:00
echo "Sending error mail"
2022-09-16 11:16:34 +02:00
echo -e " An error occured during the execution of $SCRIPT ( $ERR ).\n\n\n $( ps $PPID ) \n\n $OUTPUT " | $( dirname $0 ) /mail.sh " [E] $( hostname) $SCRIPT "
if [ $? -eq 0 ] ; then
echo "Sent"
else
echo "Could not send mail, sending safe-body mail..."
2022-09-17 12:23:42 +02:00
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 "
2022-09-16 11:16:34 +02:00
echo "Sent"
fi
2019-12-18 21:56:16 +01:00
exit 1
2021-01-08 15:04:39 +01:00
elif [ $ALWAYS_SEND -ne 0 ] ; then
echo "Sending log mail"
2022-09-16 11:16:34 +02:00
echo -e " Logging execution of $SCRIPT ( $ERR ).\n\n\n $( ps $PPID ) \n\n $OUTPUT " | $( dirname $0 ) /mail.sh " [I] $( hostname) $SCRIPT "
if [ $? -eq 0 ] ; then
echo "Sent"
else
echo "Could not send mail, sending safe-body mail..."
2022-09-17 12:23:42 +02:00
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 "
2022-09-16 11:16:34 +02:00
echo "Sent"
fi
2019-12-18 21:56:16 +01:00
fi