#!/usr/bin/ksh
#whatusedspace ()
#-----------------------------------------------------------------------#
#    Module Name:  whatusedspace  (c) Copywrite 2012                    #
#                                                                       #
#    Purpose:      Report on the Database TEMP Used space.              #
#                                                                       #
#    Maintenance                                                        #
#    Date          Author        Description                            #
#    -----------   ------------  -------------------------------------  #
#    12-May-2012   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  " # whatusedspace:         Oracle Database USED Space #"
            echo  " #---------------------------------------------------#"
            echo                                                               

            #------------------------------------------------------------#
            #  Trap:  Set CNTL-C CNTL-D CNTL-\ Before Passwd Protection  #
            #------------------------------------------------------------#
            trap        "stty echo; return"     2                              
            trap        "stty echo; return"     3
            stty -echo

            read PASSWD?" Please Enter the Oracle APPS Passwd:  "

            stty echo
            echo 
            #------------------------------------------------------------#
            #  Trap:  Reset CNTL-C CNTL-D CNTL-\ After Passwd Protection #
            #------------------------------------------------------------#
            trap        2
            trap        3

            clear
        else
            PASSWD=$1
            clear
        fi

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

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

        CONNECT  APPS/$PASSWD

        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 #- Oracle Database used Space           (whatusedspace) -#
        prompt #--------------------------------------------------------#

        COLUMN  datevalue       NOPRINT NEW_VALUE       DATEVAR
        COLUMN  timevalue       NOPRINT NEW_VALUE       TIMEVAR


        COLUMN tbsname  FORMAT A17              HEADING  "TableSpace"
        COLUMN usedsze  FORMAT 999,999,999,990  HEADING  "Used|Space(Kb)"
        COLUMN totsize  FORMAT 999,999,999,990  HEADING  "Total|Space(Kb)"
        COLUMN pctused  FORMAT 990              HEADING  "Pct|Used"

        BREAK  on REPORT

        TTITLE  CENTER  '${ORASID} (${HNAME}) E-Business Database Rpt '  -
                LEFT    DATEVAR                                  skip 1  -
                CENTER  'E-Business Database Total-Space and Used-Space' -
                LEFT    TIMEVAR                                  skip 2

        alter session set NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';


        SELECT  TO_CHAR(sysdate, 'DD-MON-YY')           datevalue,
                TO_CHAR(sysdate, 'HH24:MI:SS')          timevalue,
                tablespace_name                         tbsname,
                used_space                              usedsze,
                tablespace_size                         totsize,
                used_percent                            pctused
        FROM    dba_tablespace_usage_metrics
        ORDER BY used_percent;

END_FILE

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