#!/usr/bin/ksh
# checkt die Dauer von
# expiration
# 
#set -x
rm -f /tmp/dauer_expiration_pro_node
if [ "$1" = "" ]
then
	echo "Usage: $0 actual - Displays todays Expiration | yesterday - Displays yesterday Expiration"
	exit 1
fi
cat /var/tsm/scripts/.pw | read id password

DSMADMC="/usr/bin/dsmadmc -id=$id -pa=$password -outfile=/tmp/tsm_out_checkexpiration -DISPL=list -DATAOnly=yes"

DATUM=`date +%d%m%y`
$DSMADMC 'select node_name from nodes'
cp /tmp/tsm_out_checkexpiration /tmp/liste_nodes_to_check
case $1 in
	actual|ACTUAL|Actual|a|today|TODAY|Today|t)
		$DSMADMC "q act begint=00:00 endt=23:59 msgno=4391"
		;;
	*)
		$DSMADMC "q act begind=-1 endt=00:00 msgno=4391"
		;;
esac
cat /tmp/liste_nodes_to_check|awk '{print $2}'|sed '/^$/d'| while read i
do
	# awk '{ if (NR==NUMBER) print $0 }' NUMBER=1824 /tmp/tsm_out_checkexpiration
	LINENUMBER_BEGIN=`grep -nw "${i}" /tmp/tsm_out_checkexpiration|head -1|awk '{print $1}'|tr -d ":"`
	LINENUMBER_END=`grep -nw "${i}" /tmp/tsm_out_checkexpiration|tail -1|awk '{print $1}'|tr -d ":"`
	let LINEBEGIN_TO_GREP=${LINENUMBER_BEGIN}-1
	let LINEEND_TO_GREP=${LINENUMBER_END}-1
	BEGIN_FOR_NODE=`awk '{ if (NR==NUMBER) print $0 }' NUMBER=$LINEBEGIN_TO_GREP /tmp/tsm_out_checkexpiration|awk '{print $2,$3}'`
	END_FOR_NODE=`awk '{ if (NR==NUMBER) print $0 }' NUMBER=$LINEEND_TO_GREP /tmp/tsm_out_checkexpiration |awk '{print $2,$3}'`
	# Beispiel: Node MINOS Expiration:
	# Started: 01/03/10 11:41:05 End: 01/03/10 11:41:43
#	echo "Node $i Expiration:"
#	echo "Started: $BEGIN_FOR_NODE End: $END_FOR_NODE"
	BEGIN=`/opt/freeware/bin/date -d "$BEGIN_FOR_NODE" +%s`
	END=`/opt/freeware/bin/date -d "$END_FOR_NODE" +%s`   
	if [ ${BEGIN} -lt ${END} ]                     
	then
		let DAUERS=${END}-${BEGIN}
		if [ ${DAUERS} > 0 ]     
		then
			if [ ${DAUERS} -lt 61 ]
			then
				DAUER=${DAUERS}
				EINHEIT="Sekunden"
			else
				let DAUER=${DAUERS}/60
				EINHEIT="Minuten"
			fi
			echo "$i Dauer Expiration: ${DAUER} ${EINHEIT} Starttime: $BEGIN_FOR_NODE Endtime: $END_FOR_NODE" | tee -a /tmp/dauer_expiration_pro_node
		fi
	fi
done
echo "##############################################"
echo "Top Nodes: (Expiration laenger als 30 Minuten)"
echo "##############################################"
cat /tmp/dauer_expiration_pro_node|grep Minuten |awk '{ if ($4 > 30) print $0 }'|sort -nr -k 4

# 0987 BEGIN Expire Inventory
# 0984 END Expire Inventory

case $1 in
	actual|ACTUAL|Actual|a|today|TODAY|Today|t)
		$DSMADMC "q act begint=00:00 endt=23:59 msgno=0987"
		;;
	*)
		$DSMADMC "q act begind=-1 endt=00:00 msgno=0987"
		;;
esac
LINENUMBER=`grep -n "EXPIRE INVENTORY" /tmp/tsm_out_checkexpiration|head -1|awk '{print $1}'|tr -d ":"`
let LINE_TO_GREP=${LINENUMBER}-1
EXPIRE_TO=`awk '{ if (NR==NUMBER) print $0 }' NUMBER=$LINE_TO_GREP /tmp/tsm_out_checkexpiration|awk '{print $2,$3}'`
case $1 in
	actual|ACTUAL|Actual|a|today|TODAY|Today|t)
		$DSMADMC "q act begint=00:00 endt=23:59 msgno=0984"
		;;
	*)
		$DSMADMC "q act begind=-1 endt=00:00 msgno=0984"
		;;
esac
LINENUMBER=`grep -n "EXPIRE INVENTORY" /tmp/tsm_out_checkexpiration|head -1|awk '{print $1}'|tr -d ":"`
let LINE_TO_GREP=${LINENUMBER}-1
EXPIRE_FROM=`awk '{ if (NR==NUMBER) print $0 }' NUMBER=$LINE_TO_GREP /tmp/tsm_out_checkexpiration|awk '{print $2,$3}'`
BEGIN=`/opt/freeware/bin/date -d "$EXPIRE_FROM" +%s`
END=`/opt/freeware/bin/date -d "$EXPIRE_TO" +%s`   
if [ "$EXPIRE_TO" = "" ]
then
	# if end not defined its running so
	END=`/opt/freeware/bin/date +%s`
	EXPIRE_TO=`/opt/freeware/bin/date +%d/%m/%Y" "%H:%M:%S`
fi
if [ ${BEGIN} -lt ${END} ]                     
then
	let DAUERS=${END}-${BEGIN}
	if [ ${DAUERS} > 0 ]     
	then
		let DAUER=${DAUERS}/60
	fi
fi
if [ "$EXPIRE_FROM" != "" ] 
then
	echo "Expiration runs: $DAUER Minuten Starttime: $EXPIRE_FROM Endtime: $EXPIRE_TO"
else
	echo "Expiration runs: TSM Log ist bereits gekuerzt"
fi
rm -f /tmp/tsm_out_checkexpiration
