#!/usr/bin/ksh
# whatsortusage ()
#------------------------------------------------------------------------------#
#    Module Name:  whatsortusage                                               #
#                                                                              #
#    Purpose:      Report on the SORT Usage (TEMP Tablespace Segments)         #
#                                                                              #
#    Maintenance                                                               #
#    Date          Author        Description                                   #
#    -----------   ------------  --------------------------------------------  #
#    10-Oct-2009   T.Robinette   Module design/creation                        #
#                  F.Bender      Module design/creation                        #
#                  A.Estrada     Module design/creation                        #
#                  M.Barone      Module design/creation                        #
#------------------------------------------------------------------------------#
{
        DIRNAM=$(dirname $0)
        ORASID=$(echo $CONTEXT_NAME | cut -f1 -d'_')
        HNAME=$(uname -n)
                
        clear
        #----------------------------------------------------------------------#
        #  TEST:  Number ($#) of arguments passed to this function?            #
        #----------------------------------------------------------------------#
        if      [ $# = 0 ]
        then
            clear
            echo
            echo  " #--------------------------------------------------------#"
            echo  " # whatsortusage:                       Oracle SORT Usage #"
            echo  " #--------------------------------------------------------#"
            echo

            echo  " Connecting as SYSDBA"
            sleep 2

            clear
        else
            SEARCH=$1
            clear
        fi

        ORANAM=${ORASID}

#------------------------------------------------------------------------------#
#  SQL*Plus ("Here-Document")                                                  #
#------------------------------------------------------------------------------#
sqlplus  -s  /nolog  <<-END_FILE

	CONNECT  /  as  SYSDBA

        clear   BREAKS
        clear   BUFFER
        clear   COLUMNS
        clear   COMPUTES
        clear   SCREEN
        clear   SQL
        clear   TIMING

        set     feedback        off
        set     flush           on
        set     heading         on
        set     pause           off
        set     space           1
        set     termout         on
        set     verify          on
        set     linesize        80
        set     pagesize        999

        SPOOL /tmp/whatsortusage_$$.txt

	COLUMN datevalue NOPRINT NEW_VALUE DATEVAR
	COLUMN timevalue NOPRINT NEW_VALUE TIMEVAR
	COLUMN sid		FORMAT 99999		HEADING 'SID' just c
	COLUMN serial#		FORMAT 99999		HEADING 'Serial|Number'
	COLUMN machine		FORMAT A10		HEADING 'Server'
	COLUMN module		FORMAT A10		HEADING 'Module'
	COLUMN tot_extnts	FORMAT 999,999		HEADING 'Total|Extents'
	COLUMN tot_blocks	FORMAT 999,999,999      heading 'Total|Blocks'

	TTITLE LEFT DATEVAR -
	RIGHT 'Page:' FORMAT 999 SQL.pno -
	CENTER '$(uname -n)  ${ORACLE_SID} DBA Report'			skip 1 -
	LEFT TIMEVAR -
	CENTER	'Oracle (${ORACLE_SID}) Sort-Usage (TEMP Tablespace)'	skip 1 -
	CENTER	'whatsortusage'					 	skip 2 

	SELECT	TO_CHAR(sysdate, 'DD-MON-YY') datevalue,
		TO_CHAR(sysdate, 'HH24:MI-SS') timevalue,
  		sid,
		serial#,
		machine,
		module,
		/*,
		segtype,
		*/
		sum(extents) tot_extnts,
		sum(blocks)  tot_blocks
	  FROM	v\$sort_usage vsu,
		v\$session vs
	WHERE	vsu.session_addr = vs.saddr
	GROUP BY  sid, serial#, machine, module/*, segtype*/
	ORDER BY  tot_blocks desc;

EXIT;

END_FILE

print
print "Please see /tmp/whatsortusage_$$.txt"
print
}
