#-------------------------------------------------------------------------------
# Function to query TSM 'database' for all backups on this TSM node in the past day.
# Future enhancements: Expand query scope to identify which tapes to recall for a restore, 
# and which tapes can be exported off site for safe-keeping .
#
# Note use of print and eval to force variable substitution before executing the command - 
# the dsmadmc command does not treat variables in the same way as the db2 clp.
#
# Arguments: $1 instance name, $2 database alias, $3 ksh debug switch ( +x=off, -x=on) 
#-------------------------------------------------------------------------------
 
TSM_QUERY()
{
 INSTNAME=$1
 DBNAME=$2
 DEBUG_SWITCH=$3
 set "$DEBUG_SWITCH"
 print "\n${BORDER}"
 print "Querying TSM to show recent backups"
 print "${BORDER}\n"
 EXISTENCE_TEST "/usr/tivoli/tsm/client/ba/bin/dsmadmc" "$DEBUG_SWITCH"
 export TSMNODE=$(db2 get db cfg for $DBNAME | grep TSM_NODENAME | awk -F"= " '{print $2}') 
 PW=$( grep ^password /usr/tivoli/tsm/client/api/bin/dsm.sys | awk '{print $2}' )

 print /usr/tivoli/tsm/client/ba/bin/dsmadmc -id=$INSTNAME -pass=$PW \"select \* from summary where entity=\'${TSMNODE}\' and activity=\'BACKUP\'   and start_time\>current timestamp - 1 day \" > /tmp/$INSTNAME.dsmadmc.cmd
 chmod +x /tmp/$INSTNAME.dsmadmc.cmd 
 /tmp/$INSTNAME.dsmadmc.cmd 
 rm -f /tmp/$INSTNAME.dsmadmc.cmd 

 eval /usr/tivoli/tsm/client/ba/bin/dsmadmc -id=$INSTNAME -pass=$PW "select distinct node_name, volume_name, stgpool_name from volumeusage where node_name = \'${TSMNODE}\' order by 2"

 /usr/tivoli/tsm/client/ba/bin/dsmadmc -id=$INSTNAME -pass=$PW "select distinct volume_name, stgpool_name, status, access, last_write_date from volumes where last_write_date>current timestamp - 1 day "
 print "${BORDER}\n"

# THIS UNINDEXED TABLE JOIN TAKES AGES: eval /usr/tivoli/tsm/client/ba/bin/dsmadmc -id=$INSTNAME -pass=$PW " select su.*, se.output_vol_access from summary su, sessions se where su.number=se.session_id and entity = '\${TSMNODE}\' and activity='BACKUP'   and su.start_time>current timestamp - 1 day" 
# AND SO DOES THIS: eval /usr/tivoli/tsm/client/ba/bin/dsmadmc -id=$INSTNAME -pass=$PW " select * from volumes v, volumeusage vu where node_name= \'${TSMNODE}\' and v.volume_name = vu.volume_name and v.last_write_date>current timestamp - 1 day "
}
