#!/bin/bash
#
# cs_sum_hana_config
#
# (c) 2015-2016 SUSE Linux GmbH, Germany. Author: L.Pinne.
# GNU General Public License. No warranty.
# http://www.gnu.org/licenses/gpl.html
#
# Version: 2016-03-07 12:00 SLES11
#

EXE="$0"

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

CFGVAR="
TEMP
CONF_FILES
CONF_CALLS
APPS_FILES
APPS_CALLS
"

test -z "${TEMP}" &&\
	TEMP="/dev/shm/cltl.$RANDOM"

test -z "${CONF_FILES}" &&\
	 CONF_FILES="
/etc/SuSE-release
/etc/products.d/*.prod
/proc/sys/kernel/tainted
/boot/grub/menu.lst
/etc/hosts
/etc/CLUSTER
/etc/ntp.conf
/etc/multipath.conf
/etc/multipath.bind*
/etc/iscsi/iscsid.conf
/etc/iscsi/initiatorname.iscsi
/etc/sysconfig/kernel
/etc/sysconfig/bootloader
/etc/sysconfig/kdump
/etc/sysconfig/sbd
/etc/sysconfig/ulimit
/etc/security/limits.conf
/etc/sysctl.conf
/etc/sudoers
/etc/modprobe.conf.local
/etc/modprobe.d/*
/etc/corosync/corosync.conf
/etc/corosync/authkey
/etc/ClusterTools2/*
/root/.ssh/known_hosts
/root/.ssh/authorized_keys
"
# TODO: use common library with cs_sum_base_config and cs_show_supportconfig
# TODO: exact grep patterns
test -z "${CONF_CALLS}" &&\
	 CONF_CALLS="
'/usr/bin/grep "MHz:" /proc/cpuinfo'
'/usr/bin/grep "model.name" /proc/cpuinfo'
'/usr/bin/grep -e "MemTotal:" -e "SwapTotal:" -e "VmallocTotal:" /proc/meminfo'
'/bin/dmesg | /usr/bin/grep -e governor -e cpupower -e cpuidle | colrm 1 14'
'/bin/cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor'
'/bin/cat /sys/kernel/mm/transparent_hugepage/enabled'
'/usr/bin/grep -v "^#" /etc/sysctl.conf'
'/usr/bin/grep -v "^#" /etc/security/limits.conf'
'/usr/bin/grep -v "^#" /etc/services'
'/usr/bin/lsscsi --kname'
'/bin/uname -s -r -v -m -p -i -o'
'/bin/ls -l /boot/vmlinuz | colrm 27 39'
'/bin/ls -l /boot/initrd | colrm 27 39'
'/bin/ls -l /boot/vmlinuz-xen | colrm 27 39'
'/bin/ls -l /boot/initrd-xen | colrm 27 39'
'/usr/bin/zcat /boot/initrd | cpio -itv 2>/dev/null | colrm 43 54'
'/bin/fuser -u /dev/watchdog'
'/usr/sbin/cs_make_sbd_devices -d'
'/bin/rpm -qa'
'/usr/bin/find /sys/devices/ -name "scheduler" -exec grep -e deadline -e noop {} \;'
'/sbin/multipath -ll'
'/sbin/dmsetup table | sort'
'/sbin/chkconfig -A'
'/bin/ip a s | /usr/bin/grep "^[123456789]\:"'
'/usr/sbin/cs_precheck_for_hana -b'
"
#
# TODO SAP HANA files
test -z "${APPS_FILES}" &&\
	 APPS_FILES="
/etc/sysconfig/sapconf
/etc/pam.d/sapstartsrv
/etc/rc.d/sapinit
/usr/sap/sapservices
/etc/sysconfig/saphaws
/usr/shared/SAPHanaSR-ScaleOut/SAPHanaSR.pyc
/hana/shared/srhook/SAPHanaSR.pyc
/hana/shared/???/global/hdb/custom/config/global.ini
"
#
# TODO SAP HANA calls
test -z "${APPS_CALLS}" &&\
	 APPS_CALLS="
'/bin/ls -al /hana/data/ | awk °{print %1,%2,%3,%4,%5,%9}°'
'/bin/ls -al /hana/log/ | awk °{print %1,%2,%3,%4,%5,%9}°'
'/bin/ls -al /hana/shared/ | awk °{print %1,%2,%3,%4,%5,%9}°'
'/bin/ls -al /sapmnt/[A-Z]??/profile/ | awk °{print %1,%2,%3,%4,%5,%9}°'
'/bin/ls -al /usr/sap/[A-Z]??/SYS/profile/ | awk °{print %1,%2,%3,%4,%5,%9}°'
'/bin/ls -al /usr/sap/*/[0-9][0-9]/work/ | awk °{print %1,%2,%3,%4,%5,%9}°'
'/usr/sbin/cs_precheck_for_hana -a'
"

# TODO: function to collect relevant files and output
# TODO: find reason for error on repos.d/ (line 136 ?)
# TODO: generic function for sum_calls and sum_files

function help(){
	echo "usage:	$(basename $0)"
	echo "	$(basename $0) [OPTION]"
	echo
	echo " --help		show help."
	echo " --version	show version."
	echo " --writecfg	show some internal variables."
	echo " --base 	checksum base files and calls only."
	echo " --apps 	checksum HANA apps files and calls only."
	echo
	echo "Default is to show files and calls for base and HANA apps."
	echo
	test $UID -gt 0 && echo "please call as root."
}


function writecfg(){
	echo -e "# $CFG \n# For SLES11.\n#"
	for c in $CFGVAR; do
		case $c in
		*_CALLS)
			echo "${c}=\""
			echo ${!c} | sed s/\'\ \'/\'\\n\'/g 
			echo "\""
			echo "#"	
		;;
		*_FILES)
			echo "${c}=\""
			echo ${!c} | tr " " "\n"
			echo "\""
			echo "#"	
		;;
		esac
	done
}


function sum_config(){
	S="NA"
	test -r $f && S=$(md5sum $f | awk '{print $1}')
	echo "$f = $S"
}


function sum_files(){
	nf=1
	f=$CFG
	sum_config
	# TODO: better loop
	for f in ${CONF_FILES}; do
		(( nf++ ))
		S="NA"
		test -r $f && S=$(md5sum $f | awk '{print $1}')
        	echo "$f = $S"
	done
	echo "INFO: $nf files" >>/dev/stderr
}


function sum_calls(){
	# TODO: replace errors by "$REPLY = NA" as above
	# TODO: array instead of list, to avoid ugly "'" handling
	mkdir $TEMP
	nc=0
	f=$CFG
	sum_config
	echo ${CONF_CALLS} |\
		 tr "'" "\n" | tr -s "\n" | tr -s "°" "'" | tr "%" "$" |\
	while read; do
		REPLY=$(echo "${REPLY}" | tr -s "'")
		echo -n "'${REPLY}' = "
		eval ${REPLY} | sort | md5sum - | awk '{print $1}'
		(( nc++ ))
		echo $nc >${TEMP}/nc
	done | grep -v "'.*d41d8cd98f00b204e9800998ecf8427e"
	nc=$(cat ${TEMP}/nc); nc=$[ $nc / 2 ]
	echo "INF0: $nc calls" >>/dev/stderr
	rm -rf $TEMP
}


function sum_apps(){
	nf=1
	f=$CFG
	sum_config
	# TODO: better loop
	for f in ${APPS_FILES}; do
		(( nf++ ))
		S="NA"
		test -r $f && S=$(md5sum $f | awk '{print $1}')
        	echo "$f = $S"
	done
	echo "INFO: $nf files" >>/dev/stderr

	mkdir $TEMP
	nc=0
	f=$CFG
	sum_config
	echo "${APPS_CALLS}" |\
		 tr "'" "\n" | tr -s "\n" | tr "°" "'" | tr "%" "$" |\
	while read; do
		REPLY=$(echo "${REPLY}" | tr -s "'")
		echo -n "'${REPLY}' = "
		eval "${REPLY}" | sort | md5sum - | awk '{print $1}'
		(( nc++ ))
		echo $nc >${TEMP}/nc
	done | grep -v "'.*d41d8cd98f00b204e9800998ecf8427e"
	nc=$(cat ${TEMP}/nc); nc=$[ $nc / 2 ]
	rm -rf $TEMP
}


# main()
case $1 in
	-v|--version)
		echo -n "$(basename $EXE) "
        	head -11 $EXE | grep "^# Version: "
		exit
	;;
	-h|--help)
		help
		exit
	;;
	-w|--writecfg)
		writecfg
		exit
	;;
	-b|--base)
		test $UID -gt 0 && echo "please call as root." && exit
		sum_files
		sum_calls
		exit
	;;
	-a|--apps)
		test $UID -gt 0 && echo "please call as root." && exit
		sum_apps
		exit
	;;
	*)
		test $UID -gt 0 && echo "please call as root." && exit
		sum_files
		sum_calls
		sum_apps
		exit		
	;;
esac
#
