#!/bin/bash
#< Script to parse JBoss log files
#
# ./check_logs.sh -f /var/log/jboss/instance1-server.log -l ERROR -i -e exclude_me,and_me,and_me_too
BASENAME="/usr/bin/basename"
ECHO="/bin/echo"
EGREP="/usr/bin/egrep"
TR="/usr/bin/tr"
THIS_PROG="$( ${BASENAME} $0 )"
FILENAME=""
# Specify default minimum alerting level - the levels used in our logs
# are DEBUG->INFO->WARN->ERROR
LEVEL="ERROR"
VERBOSE=0
function print_error {
${ECHO} "Error: $@" >&2
}
function printv {
(( VERBOSE )) && {
${ECHO} "--> $@"
}
}
function print_usage {
{
${ECHO} "Usage: ${THIS_PROG} [-hvi] [-l <level>] [-e <exclusions>] -f <log_filename>"
${ECHO} " -e Comma separated list of exceptions to exclude"
${ECHO} " -f Specify logfile to parse"
${ECHO} " -h Display this usage message"
${ECHO} " -i Perform all searches case-insensitively"
${ECHO} " -l Minimum alerting level"
${ECHO} " -v Operate in verbose mode"
} >&2
}
function check_logfile {
if [ ! -f "${FILENAME}" ]; then
print_error "${FILENAME}: Could not open file"
exit 1
fi
}
function parse_logfile {
case "${LEVEL}" in
DEBUG) REGEX="DEBUG|INFO|WARN|ERROR" ;;
INFO ) REGEX="INFO|WARN|ERROR" ;;
WARN ) REGEX="WARN|ERROR" ;;
ERROR) REGEX="ERROR" ;;
esac
printv "Level REGEX: \"${REGEX}\""
if [ -n "${EXCLUDE}" ]; then
printv "EXCLUDE before translate: \"${EXCLUDE}\""
EXCLUDE="$( ${ECHO} "${EXCLUDE}" | ${TR} ',' '|' )"
printv "EXCLUDE after translate: \"${EXCLUDE}\""
printv "SEARCH CMD: ${EGREP} ${CASEINS} \"${REGEX}\" ${FILENAME} | ${EGREP} ${CASEINS} -v \"${EXCLUDE}\""
${EGREP} ${CASEINS} "${REGEX}" ${FILENAME} | ${EGREP} ${CASEINS} -v "${EXCLUDE}"
else
printv "SEARCH CMD: ${EGREP} ${CASEINS} \"${REGEX}\" ${FILENAME}"
${EGREP} ${CASEINS} "${REGEX}" ${FILENAME}
fi
}
#
# main()
#
while getopts ":hie:f:l:v" OPTION; do
case ${OPTION} in
"h") print_usage && exit 0 ;;
"e") EXCLUDE="${OPTARG}" ;;
"f") FILENAME="${OPTARG}" ;;
"i") CASEINS="-i" ;;
"l") LEVEL="${OPTARG}" ;;
"v") VERBOSE=1 ;;
* ) print_usage && exit 1 ;;
esac
done
shift $(( ${OPTIND} - 1 ))
if [ "$#" -ne "0" ]; then
print_usage && exit 1
fi
if [ "${FILENAME}" = "" ]; then
print_error "-f option mandatory"
exit 1
fi
case "${LEVEL}" in
DEBUG|INFO|WARN|ERROR ) ;;
* ) print_error "Available levels: DEBUG INFO WARN ERROR" && exit 1 ;;
esac
check_logfile
parse_logfile
exit 0