#!/usr/bin/ksh
# whatblockwait ()
#------------------------------------------------------------------------------#
#    Module Name:  whatblockwait                                               #
#                                                                              #
#    Purpose:      Report on the v$WAITSTAT.                                   #
#                                                                              #
#    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  " # whatblockwait:  Oracle Apps DataBlock WaitInformation  #"
            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/whatblockwait_$$.txt

        COLUMN  datevalue       NOPRINT NEW_VALUE       DATEVAR
        COLUMN  timevalue       NOPRINT NEW_VALUE       TIMEVAR

        COLUMN  wclas   FORMAT  A22             HEADING 'BlockClass|Description'
        COLUMN  wtotl   FORMAT  9999999990      HEADING 'Total|Waits'
        COLUMN  wtime   FORMAT  9999999990      HEADING 'Total|Wait|Time'

        TTITLE  CENTER  '${ORANAM} (${HNAME}) Block Contention'     -
                LEFT    DATEVAR                                     skip 1    -
                LEFT    TIMEVAR                                     skip 2

        SELECT  TO_CHAR(sysdate, 'DD-MON-YY')                   datevalue,
                TO_CHAR(sysdate, 'HH24:MI:SS')                  timevalue,
                CLASS                                           wclas,
                count                                           wtotl,
                time                                            wtime
        FROM    V\$WAITSTAT
        ORDER BY 2 desc;

EXIT;

END_FILE
print
print "Please see /tmp/whatblockwait_$$.txt"
print
}
