
@@header

/*
*
*  Author    : Vishal Gupta
*  Purpose   : Display hourly OS Statistics from AWR 
*              
*  Parameter : 1 - InstanceNumber (Use % for all instances)
*              2 - From Timestamp (In YYYY-MM-DD HH24:MI:SS format)
*              3 - To Timestamp   (In YYYY-MM-DD HH24:MI:SS format)
*              4 - Statistics Name (Exact Name, use @osstatname script to find exact name)           
*            
*
*
*  Revision History:
*  ===================
*  Date       Author        Description
*  ---------  ------------  -----------------------------------------
*  20-Feb-15  Vishal Gupta  Change input parameter order
*  25-May-12  Vishal Gupta  Created
*  
*/


/************************************
*  INPUT PARAMETERS
************************************/
DEFINE STAT_NAME="&1"
DEFINE FROM_TIMESTAMP="&2"
DEFINE TO_TIMESTAMP="&3"
DEFINE INST_ID="&&4"


DEFINE IF_NON_CUMULATIVE_VALUE="--"
DEFINE IF_CUMULATIVE_VALUE="--"

set term off

COLUMN "_STAT_NAME"                new_value STAT_NAME                  NOPRINT
COLUMN "_FROM_TIMESTAMP"           new_value FROM_TIMESTAMP             NOPRINT
COLUMN "_TO_TIMESTAMP"             new_value TO_TIMESTAMP               NOPRINT
COLUMN "_INST_ID"                  new_value INST_ID                    NOPRINT

SELECT DECODE('&&STAT_NAME','','BUSY_TIME','&&STAT_NAME')         "_STAT_NAME"
     , DECODE('&&FROM_TIMESTAMP','',TO_CHAR(sysdate - 1,'YYYY-MM-DD HH24:MI:SS'),'&&FROM_TIMESTAMP')   "_FROM_TIMESTAMP"
     , DECODE('&&TO_TIMESTAMP','',TO_CHAR(sysdate,'YYYY-MM-DD HH24:MI:SS'),'&&TO_TIMESTAMP')           "_TO_TIMESTAMP"
     , DECODE('&&INST_ID','','%','&&INST_ID')   "_INST_ID"
FROM DUAL
;

COLUMN "_IF_CUMULATIVE_VALUE"      new_value IF_CUMULATIVE_VALUE        NOPRINT
COLUMN "_IF_NON_CUMULATIVE_VALUE"  new_value IF_NON_CUMULATIVE_VALUE    NOPRINT

SELECT DECODE(cumulative,'YES','','--')  "_IF_CUMULATIVE_VALUE"
     , DECODE(cumulative,'NO','','--')  "_IF_NON_CUMULATIVE_VALUE"
  FROM v$osstat   
 WHERE stat_name = '&&STAT_NAME'
;

COLUMN  "_HEADING"             new_value    "HEADING"             NOPRINT
COLUMN  "_DIVIDER"             new_value    "DIVIDER"             NOPRINT
COLUMN  "_STAT_DESCRIPTION"    new_value    "STAT_DESCRIPTION"    NOPRINT

SELECT DECODE('&&STAT_NAME'
               ,'BUSY_TIME','(s)'
               ,'USER_TIME','(s)'
               ,'IOWAIT_TIME','(s)'
               ,'SYS_TIME','(s)'
               ,'IDLE_TIME','(s)'
               ,'NICE_TIME','(s)'
               ,'RSRC_MGR_CPU_WAIT_TIME','(s)'
               ,'VM_IN_BYTES','MB'
               ,'VM_OUT_BYTES','MB'
               ,''
              )  "_HEADING"
     , DECODE('&&STAT_NAME'
               ,'BUSY_TIME','100'
               ,'USER_TIME','100'
               ,'IOWAIT_TIME','100'
               ,'SYS_TIME','100'
               ,'IDLE_TIME','100'
               ,'NICE_TIME','100'
               ,'RSRC_MGR_CPU_WAIT_TIME','100'
               ,'VM_IN_BYTES','power(1024,2)'
               ,'VM_OUT_BYTES','power(1024,2)'
               ,1
              )  "_DIVIDER"
     , DECODE('&&STAT_NAME'
               ,'BUSY_TIME','Time stat CPUs have been in busy state'
               ,'USER_TIME','Time spent in user code'
               ,'IOWAIT_TIME','Time spent waiting for IO'
               ,'SYS_TIME','Time spent in the kernel'
               ,'IDLE_TIME','Time that CPUs have been in idle state'
               ,'NICE_TIME','100'
               ,'RSRC_MGR_CPU_WAIT_TIME','100'
               ,'VM_IN_BYTES','power(1024,2)'
               ,'VM_OUT_BYTES','power(1024,2)'
               ,''
              )  "_STAT_DESCRIPTION"
  FROM v$osstat   
 WHERE stat_name = '&&STAT_NAME'
;


set term on


/************************************
*  CONFIGURATION PARAMETERS
************************************/

set pages 50000

DEFINE FORMAT="999,999,999,999"
DEFINE TOTAL_FORMAT="999,999,999,999"

DEFINE HEADING="#"
DEFINE DIVIDER="1"
--DEFINE HEADING="'1000s"
--DEFINE DIVIDER="1000"

--DEFINE HEADING="min"
--DEFINE DIVIDER="60/100"
--DEFINE HEADING="hr"
--DEFINE DIVIDER="60/60/100"

--DEFINE HEADING="'KB"
--DEFINE DIVIDER="1024"
--DEFINE HEADING="'MB"
--DEFINE DIVIDER="1024/1024"
--DEFINE HEADING="'GB"
--DEFINE DIVIDER="1024/1024/1024"

PROMPT 
PROMPT #############################################################################
PROMPT #   AWR Operation System Statistics
PROMPT #  
PROMPT #   From AWR Repository 
PROMPT #   - Statistic Name  - "&&STAT_NAME" - &&STAT_DESCRIPTION
PROMPT #   - From Timestamp  - "&&FROM_TIMESTAMP" (YYYY-MM-DD HH24:MI:SS)
PROMPT #   - To Timestamp    - "&&TO_TIMESTAMP" (YYYY-MM-DD HH24:MI:SS)
PROMPT #   - For Instance ID - "&&INST_ID"
PROMPT #############################################################################

COLUMN end_interval_time   HEADING "SnapTime"             FORMAT A15
COLUMN all_inst_value      HEADING "All Inst|Value"       FORMAT &&TOTAL_FORMAT
COLUMN inst1_value         HEADING "Inst 1|Value"         FORMAT &&FORMAT
COLUMN inst2_value         HEADING "Inst 2|Value"         FORMAT &&FORMAT
COLUMN inst3_value         HEADING "Inst 3|Value"         FORMAT &&FORMAT
COLUMN inst4_value         HEADING "Inst 4|Value"         FORMAT &&FORMAT
COLUMN inst5_value         HEADING "Inst 5|Value"         FORMAT &&FORMAT
COLUMN inst6_value         HEADING "Inst 6|Value"         FORMAT &&FORMAT
COLUMN inst7_value         HEADING "Inst 7|Value"         FORMAT &&FORMAT
COLUMN inst8_value         HEADING "Inst 8|Value"         FORMAT &&FORMAT

WITH snap
AS
 (SELECT s.dbid
       , s.instance_number
       , s.snap_id
       , LEAD (s.snap_id) OVER (ORDER BY s.dbid, s.instance_number, s.snap_id) next_snap_id
       , s.end_interval_time
   FROM  dba_hist_snapshot s
       , v$database d
  WHERE s.dbid = d.dbid
    AND s.instance_number LIKE '&&INST_ID'
    AND s.end_interval_time BETWEEN TO_TIMESTAMP('&&FROM_TIMESTAMP','YYYY-MM-DD HH24:MI:SS') 
       AND TO_TIMESTAMP('&&TO_TIMESTAMP'  ,'YYYY-MM-DD HH24:MI:SS')
 )
SELECT /*+ USE_NL(s1 ss1 ss_prev) */ 
      TO_CHAR(s.end_interval_time,'DD-MON-YY HH24:MI')  end_interval_time
&&IF_CUMULATIVE_VALUE     , ROUND(SUM(ss.value - LEAST(ss_prev.value,ss.value)) / &&DIVIDER ) all_inst_value
&&IF_CUMULATIVE_VALUE     , ROUND(SUM(DECODE(ss.instance_number,1,(ss.value - LEAST(ss_prev.value,ss.value)),0)) / &&DIVIDER ) inst1_value
&&IF_CUMULATIVE_VALUE     , ROUND(SUM(DECODE(ss.instance_number,2,(ss.value - LEAST(ss_prev.value,ss.value)),0)) / &&DIVIDER ) inst2_value
&&IF_CUMULATIVE_VALUE     , ROUND(SUM(DECODE(ss.instance_number,3,(ss.value - LEAST(ss_prev.value,ss.value)),0)) / &&DIVIDER ) inst3_value
&&IF_CUMULATIVE_VALUE     , ROUND(SUM(DECODE(ss.instance_number,4,(ss.value - LEAST(ss_prev.value,ss.value)),0)) / &&DIVIDER ) inst4_value
&&IF_CUMULATIVE_VALUE     , ROUND(SUM(DECODE(ss.instance_number,5,(ss.value - LEAST(ss_prev.value,ss.value)),0)) / &&DIVIDER ) inst5_value
&&IF_CUMULATIVE_VALUE     , ROUND(SUM(DECODE(ss.instance_number,6,(ss.value - LEAST(ss_prev.value,ss.value)),0)) / &&DIVIDER ) inst6_value
&&IF_CUMULATIVE_VALUE     , ROUND(SUM(DECODE(ss.instance_number,7,(ss.value - LEAST(ss_prev.value,ss.value)),0)) / &&DIVIDER ) inst7_value
&&IF_CUMULATIVE_VALUE     , ROUND(SUM(DECODE(ss.instance_number,8,(ss.value - LEAST(ss_prev.value,ss.value)),0)) / &&DIVIDER ) inst8_value
&&IF_NON_CUMULATIVE_VALUE     , ROUND(MAX(ss.value) / &&DIVIDER ) all_inst_value
&&IF_NON_CUMULATIVE_VALUE     , ROUND(MAX(DECODE(ss.instance_number,1,ss.value,0)) / &&DIVIDER ) inst1_value
&&IF_NON_CUMULATIVE_VALUE     , ROUND(MAX(DECODE(ss.instance_number,2,ss.value,0)) / &&DIVIDER ) inst2_value
&&IF_NON_CUMULATIVE_VALUE     , ROUND(MAX(DECODE(ss.instance_number,3,ss.value,0)) / &&DIVIDER ) inst3_value
&&IF_NON_CUMULATIVE_VALUE     , ROUND(MAX(DECODE(ss.instance_number,4,ss.value,0)) / &&DIVIDER ) inst4_value
&&IF_NON_CUMULATIVE_VALUE     , ROUND(MAX(DECODE(ss.instance_number,5,ss.value,0)) / &&DIVIDER ) inst5_value
&&IF_NON_CUMULATIVE_VALUE     , ROUND(MAX(DECODE(ss.instance_number,6,ss.value,0)) / &&DIVIDER ) inst6_value
&&IF_NON_CUMULATIVE_VALUE     , ROUND(MAX(DECODE(ss.instance_number,7,ss.value,0)) / &&DIVIDER ) inst7_value
&&IF_NON_CUMULATIVE_VALUE     , ROUND(MAX(DECODE(ss.instance_number,8,ss.value,0)) / &&DIVIDER ) inst8_value
FROM snap s
   , dba_hist_osstat ss
   , dba_hist_osstat ss_prev
where s.next_snap_id IS NOT NULL
and s.dbid = ss_prev.dbid
and s.instance_number = ss_prev.instance_number
and s.snap_id = ss_prev.snap_id
and s.dbid = ss.dbid
and s.instance_number = ss.instance_number
and s.next_snap_id = ss.snap_id
and ss_prev.stat_name = ss.stat_name
and ss.stat_name = '&&STAT_NAME'
GROUP BY TO_CHAR(s.end_interval_time,'DD-MON-YY HH24:MI')
ORDER BY TO_DATE(TO_CHAR(s.end_interval_time,'DD-MON-YY HH24:MI'),'DD-MON-YY HH24:MI') asc
;


UNDEFINE IF_NON_CUMULATIVE_VALUE
UNDEFINE IF_CUMULATIVE_VALUE


@@footer
