#!/usr/bin/ksh
#whattempusage ()
#------------------------------------------------------------------------------#
#    Module Name:  whattempusage  (c)  Michael Barone                          #
#                                                                              #
#    Purpose:      Report on the Database TEMP Tablespace Usage.               #
#                                                                              #
#    Maintenance                                                               #
#    Date          Author        Description                                   #
#    -----------   ------------  --------------------------------------------  #
#    10-Oct-2009   M.Barone      Module design/creation                        #
#------------------------------------------------------------------------------#
{
        DIRNAM=$(dirname $0)
        ORASID=$(echo $CONTEXT_NAME | cut -f1 -d'_')
        HNAME=$(uname -n)

        #----------------------------------------------------------------------#
        #  TEST:  Number ($#) of arguments passed to this function?            #
        #----------------------------------------------------------------------#
        if      [ $# = 0 ]
        then
            clear
            echo 
            echo  " #--------------------------------------------------------#"
            echo  " # whattempusage:            Oracle TEMP Tablespace Usage #"
            echo  " #--------------------------------------------------------#"
            echo                                                               
            echo  " Connecting as SYSDBA"
            sleep 2

            clear
        else
            PASSWD=$1
            clear
        fi

#------------------------------------------------------------------------------#
#  Initialize                                                                  #
#------------------------------------------------------------------------------#
        SPOOLNM=/tmp/whattempusage_$$.txt

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

        CONNECT /  AS SYSDBA

        SPOOL ${SPOOLNM}

        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

        prompt #---------------------------------------------------------------#
        prompt #- What (whattempusage)                                        -#
        prompt #---------------------------------------------------------------#

        COLUMN  datevalue       NOPRINT NEW_VALUE       DATEVAR
        COLUMN  timevalue       NOPRINT NEW_VALUE       TIMEVAR

        COLUMN  tnam    FORMAT  A20             Heading 'Tablespace|Name'       WORD_WRAPPED
        COLUMN  tusd    FORMAT  9999999999      Heading 'Tablespace|Used'
        COLUMN  tsiz    FORMAT  9999999999      Heading 'Tablespace|Total'
        COLUMN  tpct    FORMAT  999.999999      Heading 'Tablespace|Used|Pct'

        TTITLE  LEFT    DATEVAR                                         -
                RIGHT   'Page:'         FORMAT 999 SQL.pno              -
                CENTER  'Tablespace TEMP DBA Report'            skip 1  -
                LEFT    TIMEVAR                                         -
                CENTER  'Oracle Tablespace/File Name'           skip 2

        SELECT  TO_CHAR(sysdate, 'DD-MON-YY')   datevalue,
                TO_CHAR(sysdate, 'HH24:MI:SS')  timevalue,
                tablespace_name                 tnam,
                used_space                      tusd,
                tablespace_size                 tsiz,
                used_percent                    tpct
          FROM  dba_tablespace_usage_metrics;

END_FILE

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