#!/bin/bash
#
# cs_show_cluster_transition
#
# (c) 2011 SUSE Linux GmbH, Germany. Author: L.Pinne.
# GNU Public License. No warranty.
#
# Version: 0.1 2011-10-06
#

EXE="$0"

CFG="/etc/ClusterTools2/cs_show_cluster_transition"
test -s $CFG && source $CFG

test -z "/etc/corosync/corosync.conf" &&\
test -z "${CLUSTER_LOG}" &&\
	 CLUSTER_LOG=$(grep "logfile:.*/" /etc/corosync/corosync.conf |\
		 tr -d "    " | cut -d":" -f2)
test -z "${CLUSTER_LOG}" &&\
	 CLUSTER_LOG="/var/log/messages"

ZIPPED_LOG="${CLUSTER_LOG}*bz2"

#	 TRANST_PATTERN="
#"crmd.*State.transition.S_IDLE"
#"crmd.*State.transition.S_POLICY_ENGINE"
#"crmd.*State.transition.S_TRANSITION_ENGINE"
#"crmd.*State.transition.S_STARTING"
#"crmd.*State.transition.S_PENDING"
#"crmd.*State.transition.S_ELECTION"
#"crmd.*State.transition.S_INTEGRATION"
#"crmd.*State.transition.S_FINALIZE_JOIN"
#"crmd.*State.transition.S_NOT_DC"
#"

function help() {
	echo "	$(basename $0)"
	echo "	$(basename $0) OPTION"
	echo
	echo " --help		show help."
	echo " --version	show version."
#	echo " --zip		show transitions from compressed logs, too."
	echo " --error		show errors."
}


# TODO: sort: As LC_TIME differs from LC_CTYPE, the results may be strange.
# TODO: better awk
function awk_transition() {
	echo "logs: $LOG" >/dev/stderr
	for f in ${LOG}; do
		test -r $f && cat $f
	done | sort -M -k1,3 |\
	awk '	$0~/crmd.*State.transition/ {print $1,$2,$3,$4,$11,"->",$13}
		#	
		#$0~/crmd.*abort_transition_graph/ {print $1,$2,$3,$4,$8}
		#$0~/crmd.*do_dc_join_finalize/	   {print $1,$2,$3,$4,$8,$14}
		#$0~/crmd.*notice..crmd_peer_update/   {print $1,$2,$3,$4,$9,$12,$16}
		#
		$0~/syslog-ng.s...ting.[du][po]/ {print $1,$2,$3,$4,$6,$7,$8}
		$0~/crmd.*cluster.nodes.*eligible/ {print $1,$2,$3,$4,$10,$11,$12,$14}
		$0~/crmd.*status.\[o.*line\]/   {print $1,$2,$3,$4,$9,$12,$16}
		$0~/crmd.*update_dc.*Unset.DC/ 	   {print $1,$2,$3,$4,$9,$10,$11}
		$0~/crmd.*update_dc.*Set.DC.to/    {print $1,$2,$3,$4,$9,$10,$11,$12}' |\
	awk '$7=="S_IDLE"{print $0,"\n"}; $7!="S_IDLE"{print $0}'
}

function filter_transition() {
        echo "logs: $LOG" >/dev/stderr
        for f in ${LOG}; do
                test -r $f && cat $f
        done | sort -M -k1,3 |\
        perl -e 'while (<>) {
                  if ( /((\S+\s+){4}).*crmd.*State.transition.*(S_\w+)\s*->\s*(S_\w+)/ ) {
                      printf "%s: %s -> %s\n", $1, $3, $4;
                        if ( $4 eq "S_IDLE" ) { printf "\n"; }
                  }
               }'
# TODO: LARS, FABIAN: Implement other patterns
# TODO: FABIAN: What about readable patterns
} 


function awk_crmd-error() {
	echo "logs: $LOG" >/dev/stderr
	for f in ${LOG}; do
		test -r $f && cat $f
	done | sort -M -k1,3 |\
	grep -e "crmd:.*ERROR"
}


# main()

# TODO: reading from pipe 

case $1 in
	-h|--help)
		help
		exit
	;;
	-v|--version)
		echo -n "$(basename $EXE) "
        	head -11 $EXE | grep "^# Version: "
		exit
	;;
	-z|--zip)
		LOG=""
		for z in ${ZIPPED_LOG}; do
			test -s $z && LOG="${LOG} ${z}"
		done
		# unzipped log has to be last in loop :-/
		test -s ${CLUSTER_LOG} && LOG="${LOG} ${CLUSTER_LOG}"
		#awk_transition
		echo noop	
		exit
	;;
	-e|--error)
		test -s ${CLUSTER_LOG} && LOG="${CLUSTER_LOG}"
		awk_crmd-error
		exit
	;;
	*)
		test -s ${CLUSTER_LOG} && LOG="${CLUSTER_LOG}"
		filter_transition
		exit
	;;
esac
#
