sandbox
Sandbox page
Git test 8.

#!/bin/bash

#Starting - JBAS015899
#Started Success - JBAS015874
#Started Failed - JBAS015875

function version() {
	echo "esc (esc startup checker) 0.1
Copyright (C) 2014 nomike <nomike@nomike.com>
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by nomike <nomike@nomike.com>"
}

function usage() {
	echo "Usage: $0 [options]... [FILE]...
Check if last JBoss start was successfull.

  -h, --help                  display this help text and exit
  -s, --success               display log lines for successfull startups too
      --version               display version info and exit

Exit stati for multiple log files are multiplied with a bitwise AND.

Exit status:
 0   if JBoss start was successfull or no logfile specified on command line,
 1   if JBoss start was unsuccessfull,
 2   if JBoss startup complete message could not be found (could indicate
 that JBoss is still starting up)
 4   if JBoss startup message could not be found
 8   if at least one file could not be accessed.
 128 if another error has occured
   
   Report esc bugs to nomike@nomike.com
   Home page: <http://www.nomike.com/>
"	  	
}

function checkStack() {
	# sanity check for file
	if ! [ -f "${1}" -a -r "${1}" ] ; then
		echo "File could not be read" >&2
		return 128
	fi
	
	declare -i startline
	
	# check for last "JBboss is starting" message
	startline="$( grep -n 'JBAS015899' ${1} | tail -n 1 | cut -d ":" -f1)"
	
	if ! [ "${startline}" -eq "${startline}" ] ; then
		echo "JBoss start line not found in \"${1}\"" >&2
		return 4
	fi
	
	# check for successfull start
	error="$(tail -n +${startline} ${1} | grep -m 1 'JBAS015875')"
	success="$(tail -n +${startline} ${1} | grep -m 1 'JBAS015874')"
	
	if [ -n "${error}" ] ; then
		# Logfile contained a "startup unsuccessfull" message
		echo "${error}"
		return 1
	elif [ -n "${success}" ] ; then
		# logfile contained a "startup successfull" message
		if [ ${display_success} -eq 0 ]  ; then
			echo "${success}"
		fi
		return 0
	else
		echo "JBoss startup-complete message not found" >&2
		return 2
	fi
}


display_success=1
args=`getopt -n "$0" -o hs --long help,success,version -- "$@"` 1>&2
if [ $? -ne 0 ]; then
	echo "Parameter error"
        usage
        exit 128
fi
eval set -- "$args"
while true ; do
        case "$1" in
                -h|--help) usage ; exit 0 ; shift ;;
                -s|--success) display_success=0 ; shift ;;
                --version) version ; exit 0 ; shift ;;
                --) shift ; break ;;
                *) echo "Invalid parameter" ; exit 128 ;;
        esac
done


result=0
for file in "${@}" ; do
	checkStack "${file}"
	result=$(( ${result} | ${?} ))
done

exit ${result}