Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
if [ $# -lt 1 -o $# -gt 4 ]
then
echo "Usage: $0 log_to_be_checked [cc-emails [to-email [subject]]]" >&2
exit 1
fi
LOGFILE="$1"
TSFILE="${LOGFILE}.ts"
MAXLOGLINES=50
MAIL_CC=${2:-user@example.com}
MAIL_TO=${3:-owner@example.com}
MAIL_SUBJ_TPL=${4:-New data in ${LOGFILE}}
if [ ! -e "${LOGFILE}" ]
then
exit 0
fi
if [ ! "${LOGFILE}" -nt "${TSFILE}" ]
then
exit 0
fi
TMPLOGFILE="${LOGFILE}.`date +%s`"
mv "${LOGFILE}" "${TMPLOGFILE}"
touch --reference="${TMPLOGFILE}" "${LOGFILE}"
chown --reference="${TMPLOGFILE}" "${LOGFILE}"
chmod --reference="${TMPLOGFILE}" "${LOGFILE}"
since_line="."
if [ -e "${TSFILE}" ]
then
ts_mtime=`stat --print='%y' "${TSFILE}"` 2>/dev/null
if [ $? -eq 0 ]
then
since_line="\nsince ${ts_mtime}${since_line}"
fi
fi
# Get record id
RID=`sed '1,/Stack trace/d; /^$/,$d; s/\s.*//' "${TMPLOGFILE}" | md5sum | head -c 4`
# Get project name
PRID=`echo ${LOGFILE} | sed -e "s:/www/\([^/]\+\)/\([^/]\+\)/.*:\2.\1:"`
# Get subject
MAIL_SUBJ=`eval "echo ${MAIL_SUBJ_TPL}"`
( echo -e "There are new records in ${LOGFILE}${since_line}
It was renamed into ${TMPLOGFILE}.
Showing at most ${MAXLOGLINES} lines of it:
===========================================================
"; tail -n ${MAXLOGLINES} "${TMPLOGFILE}"; echo -e "\n===========================================================\n" ) | /bin/mail -c "${MAIL_CC}" -s "${MAIL_SUBJ}" "${MAIL_TO}"
touch "${TSFILE}"
# vim: tabstop=8 noexpandtab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.