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"
|
2020-03-20 13:04:22 +01:00
|
|
|
echo -e "An error occured during the execution of $SCRIPT ($ERR).\n\n\n$(ps $PPID)\n\n$OUTPUT" | $(dirname $0)/mail.sh "$(hostname) $SCRIPT failure"
|
2019-12-18 21:56:16 +01:00
|
|
|
echo "Sent"
|
|
|
|
exit 1
|
2021-01-08 15:04:39 +01:00
|
|
|
elif [ $ALWAYS_SEND -ne 0 ]; then
|
|
|
|
echo "Sending log mail"
|
|
|
|
echo -e "Logging execution of $SCRIPT ($ERR).\n\n\n$(ps $PPID)\n\n$OUTPUT" | $(dirname $0)/mail.sh "$(hostname) $SCRIPT log"
|
|
|
|
echo "Sent"
|
2019-12-18 21:56:16 +01:00
|
|
|
fi
|