#!/bin/bash
#
# cs_sum_base_config
#
# (c) 2011-2014 SUSE Linux GmbH, Germany. Author: L.Pinne.
# GNU Public License. No warranty.
#
# Version: 2014-02-07 16:25
#

EXE="$0"

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

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

test -z "${CONF_FILES}" &&\
	 CONF_FILES="
/etc/SuSE-release
/etc/zypp/zypp.conf
/etc/zypp/zypper.conf
/etc/zypp/repos.d/*.repo
/etc/products.d/*.prod
/boot/vmlinuz-2.6*default
/boot/initrd-2.6*default
/boot/vmlinuz-2.6*xen
/boot/initrd-2.6*xen
/boot/vmlinuz-3.*default
/boot/initrd-3.*default
/boot/vmlinuz-3.*xen
/boot/initrd-3.*xen
/proc/cmdline
/proc/sys/kernel/tainted
/etc/auto.*
/etc/ld.so.conf
/etc/ld.so.conf.d/*
/etc/inittab
/etc/hosts
/etc/host.conf
/etc/resolv.conf
/etc/ntp.conf
/etc/adjtime
/etc/passwd
/etc/group
/etc/sudoers
/etc/services
/etc/ldap.conf
/etc/openldap/ldap.conf
/etc/logevent.conf
/etc/krb5.conf
/etc/drbd.conf
/etc/drbd.d/*
/etc/lvm/lvm.conf
/etc/multipath.conf
/etc/multipath.bind*
/etc/mdadm.conf*
/etc/*mdadm.*.conf
/clusterconf/*/mdadm.conf
/clusterconf/mdadm.*.conf
/etc/iscsi/iscsid.conf
/etc/iscsi/initiatorname.iscsi
/etc/sysstat/*
/etc/sysconfig/kernel
/etc/sysconfig/bootloader
/etc/sysconfig/sbd
/etc/sysconfig/ulimit
/etc/sysconfig/uuidd
/etc/sysconfig/security
/etc/sysconfig/language
/etc/sysconfig/clock
/etc/sysconfig/ntp
/etc/sysconfig/lvm
/etc/sysconfig/o2cb
/etc/sysconfig/maranello
/etc/sysconfig/bravo
/etc/sysconfig/customfw
/etc/ocfs2/cluster.conf
/etc/security/limits.conf
/etc/pam.d/login
/etc/opt/six/maranello/*.conf
/etc/opt/six/maranello/profiles/two-sets/*/*
/etc/opt/six/maranello/profiles/three-sets/*/*
/etc/opt/bravo/*.conf
/etc/opt/bravo/profiles/two-sets/*/*
/etc/opt/bravo/profiles/three-sets/*/*
/etc/sysctl.conf
/etc/modprobe.conf.local
/etc/modprobe.d/*
/etc/cron.d/*
/etc/logrotate.d/corosync
/etc/corosync/corosync.conf
/etc/corosync/logd.cf
/etc/corosync/authkey
/etc/xen/xend-config.sxp
/etc/xen/vm/*.xml
/etc/hp/*
/etc/hp*.conf
/etc/patrol.d/*.conf
/etc/vmware-tools/tools.conf
/etc/vmware-tools/plugins
/etc/pam.d/vmtoolsd
"
# TODO: exact grep patterns
test -z "${CONF_CALLS}" &&\
	 CONF_CALLS="
'/usr/sbin/dmidecode -t system | /usr/bin/grep -v "UUID:" | /usr/bin/grep -v "Serial.Number:"'
'/sbin/lspci'
'/usr/bin/grep "model.name" /proc/cpuinfo'
'/usr/bin/grep -e "MemTotal:" -e "SwapTotal:" -e "VmallocTotal:" /proc/meminfo'
'/usr/bin/grep -v "^#" /etc/sysctl.conf'
'/usr/bin/grep -v "^#" /etc/security/limits.conf'
'/usr/bin/lsscsi --kname'
'/bin/uname -s -r -v -m -p -i -o'
'/bin/lsmod'
'ls -l /boot/vmlinuz | colrm 27 39'
'ls -l /boot/initrd | colrm 27 39'
'ls -l /boot/vmlinuz-xen | colrm 27 39'
'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'
'/bin/rpm -Va'
'/usr/bin/zypper lr'
'/usr/bin/zypper -n lu'
'/usr/bin/zypper -n se -t pattern | grep "^i"'
'/bin/cat /proc/mounts'
'find /sys/devices/ -name "scheduler" -exec grep deadline {} \;'
'find /sys/devices/ -name "scheduler" -exec grep noop {} \;'
'/sbin/multipath -ll'
'/sbin/dmsetup table | sort'
'/usr/bin/grep -v "^#" /etc/lvm/lvm.conf'
'/usr/bin/grep -v "^#" /etc/multipath.conf'
'/sbin/chkconfig -A'
'/sbin/route -n'
'/usr/sbin/iptables -L'
'/bin/ip a s | /usr/bin/grep "^[123456789]\:"'
'/usr/bin/grep -v "IPADDR=" /etc/sysconfig/network/ifcfg-* | /usr/bin/grep -v "UNIQUE="'
'/usr/bin/cset set -l'
'/usr/bin/numactl -H | grep -v free; /usr/bin/numactl -s'
'find /opt/'
'find /usr/'
'find /etc/'
'/bin/ls -al /etc/ | awk °{print %1,%2,%3,%4,%5,%9}°'
'/bin/ls -al /etc/*/ | awk °{print %1,%2,%3,%4,%5,%9}°'
'/bin/ls -al /home/ | awk °{print %1,%2,%3,%4,%5,%9}°'
"
#
test -z "${APPS_FILES}" &&\
	 APPS_FILES="
/etc/oratab
/etc/oraInst.loc
/etc/sysconfig/oracle
/etc/tnsnames.ora
/opt/oracle/product/*/*/dbs/*.ora
/opt/oracle/product/*/*/network/admin/*.ora
/etc/sysconfig/postgresql
/var/lib/pgsql/data/postgresql.conf
/etc/sysconfig/rhn/*
/etc/rhn/rhn.conf
/etc/nocpulse/*
/etc/opt/novell/eDirectory/conf/nds.conf
/etc/logevent.conf
/etc/opt/novell/nici*
/etc/sysconfig/sapconf
/etc/pam.d/sapstartsrv
/etc/rc.d/sapinit
/usr/sap/sapservices
/etc/opt/sdb
/etc/db2nodes.conf
/etc/sysconfig/saphaws
/etc/sysconfig/spock
/etc/sysconfig/ctdb
/etc/samba/*
"
#
test -z "${APPS_CALLS}" &&\
	 APPS_CALLS="
'/bin/ls -al /home/ | awk °{print %1,%2,%3,%4,%5,%9}°'
'/bin/ls -al /usr/spool/sql/ | awk °{print %1,%2,%3,%4,%5,%9}°'
'/bin/ls -al /sapdb/programs/lib/ | awk °{print %1,%2,%3,%4,%5,%9}°'
'/bin/ls -al /sapdb/programs/runtime/ | awk °{print %1,%2,%3,%4,%5,%9}°'
'/bin/ls -al /sapdb/clients/ | awk °{print %1,%2,%3,%4,%5,%9}°'
'/bin/ls -al /var/lib/sdb/dbm/ | 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}°'
"
# TODO: samba
# TODO: ndstat and other edir checking
# TODO: generic script to strip "#" from conf files 
# TODO: generic script for directory listing 
# TODO: better network listing 
# TODO: calc diff between hwclock and Linux time, fe. based on hwclock --debug --test


function help() {
	echo "usage:	$(basename $0)"
	echo "	$(basename $0) [OPTION]"
	echo
	echo " --help		show help."
	echo " --version	show version."
	echo " --files	checksum base files only."
	echo " --calls	checksum base calls only."
	echo " --apps 	checksum apps config files and calls only."
	echo
	test $UID -gt 0 && echo "please call as root."
}


# 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 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
	;;
	-f|--files)
		test $UID -gt 0 && echo "please call as root." && exit
		sum_files
		exit
	;;
	-c|--calls)
		test $UID -gt 0 && echo "please call as root." && exit
		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
		exit		
	;;
esac
#
