Skip to content

Instantly share code, notes, and snippets.

@fedecarg
Last active September 16, 2016 10:22
Show Gist options
  • Select an option

  • Save fedecarg/bf1b594ff30f2e9a2f01 to your computer and use it in GitHub Desktop.

Select an option

Save fedecarg/bf1b594ff30f2e9a2f01 to your computer and use it in GitHub Desktop.
BBC Error Log parser. Shows the most frequent error messages in an error.log file.
#!/bin/sh
# set unique $FILENAME
FILENAME=$(date +%d-%m-%y)-${APP_NAME}-${APP_ENV}
# set $LOG_URI (tail/download)
LOG_URI=https://bbc/logs/error_log/download/${APP_ENV}/error_log
# limit the $NUMBER_OF_LINES when processing large files (default: 4000 lines)
NUMBER_OF_LINES=${NUMBER_OF_LINES:=4000}
# set $TEMPLATE with placeholder %s
TEMPLATE="<html><body><pre style='background-color:#333;color:#ededed;padding:10px;overflow:scroll'>%s</pre></body></html>"
#
# remove previous log file
#
rm -f *.log
rm -f ${FILENAME}*.html
#
# remove html files older than 2 days
#
find . -type f -name "*.html" -mtime +2 -exec rm {} \;
#
# download log file
#
curl --cert /etc/pki/news.pem -o ${FILENAME}.tmp ${LOG_URI}
head -${NUMBER_OF_LINES} ${FILENAME}.tmp > ${FILENAME}.log 2>&1
rm -f ${FILENAME}.tmp
#
# find application errors
#
sed 's^\[.*[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\]^^g' ${FILENAME}.log | grep "^ ${APP_NAME}-${APP_ENV}.\(DEBUG\|INFO\|WARN\|ERROR\|CRITICAL\)" | sort | uniq -c | sort -nr | sed -n 1,100p > ${FILENAME}-app-errors.html 2>&1
#
# find critical errors
#
sed 's^\[.*[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\]^^g' ${FILENAME}.log | grep "^ ${APP_NAME}-${APP_ENV}.CRITICAL" | sort | uniq -c | sort -nr | sed -n 1,100p > ${FILENAME}-critical-errors.html 2>&1
#
# find php errors
#
sed 's^\[.*[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}\]^^g' ${FILENAME}.log | grep "^ PHP \(Fatal\|Catchable\|Error\|Warning\|Notice\)" | sort | uniq -c | sort -nr | sed -n 1,20p > ${FILENAME}-php-errors.html 2>&1
#
# convert to html
#
printf "$TEMPLATE" "$(cat ${FILENAME}-critical-errors.html 2>&1)" > ${APP_NAME}-${APP_ENV}-critical-errors.html 2>&1
printf "$TEMPLATE" "$(cat ${FILENAME}-app-errors.html 2>&1)" > ${APP_NAME}-${APP_ENV}-app-errors.html 2>&1
printf "$TEMPLATE" "$(cat ${FILENAME}-php-errors.html 2>&1)" > ${APP_NAME}-${APP_ENV}-php-errors.html 2>&1
#
# search for a specific error message
# if error is found, exit and send email to [email protected]
#
ERRORS=("$EXIT_IF_ERROR_1" "$EXIT_IF_ERROR_2")
for err in "${ERRORS[@]}"
do
[[ "$err" == "" ]] && { break; }
if grep "$err" ${FILENAME}.log; then
echo "found error: $err"
exit 1
fi
done
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment