Monday, June 13, 2016

Shell script to Automate AWR report (to mail Oracle awr performance reports daily for peak periods )

Cat daily_awr_gen.sh

#!/bin/ksh
ORACLE_HOME=/app/11.2.0/prdobi
export ORACLE_HOME
ORACLE_SID=prodobi
export ORACLE_SID

/app/11.2.0/prdobi/bin/sqlplus -s / as sysdba << EOF

create or replace function snap_values (p_snap_range  varchar2)
return number
as
l_min_snap_id number;
l_max_snap_id number;
begin
SELECT min(snap_id),max(snap_id)  into l_min_snap_id,l_max_snap_id
FROM dba_hist_snapshot
WHERE trunc(begin_interval_time )= trunc(SYSDATE )
and trunc(end_interval_time) = trunc(SYSDATE )
and (to_char(begin_interval_time,'hh24') >= 10 and to_char(begin_interval_time,'hh24') <=16)
order by begin_interval_time;
if p_snap_range='MIN'  then
return l_min_snap_id;
elsif p_snap_range='MAX' then
return l_max_snap_id;
else
return null;
end if;
exception when others then
return null;
end;


EOF
echo $dbid
echo $db_name
echo $inst_num
echo $inst_name
echo $num_days

#repnam='awrrpt_'$inst_num'_'$inst_name'.html'

rm -rf awr_report_*.*

/app/11.2.0/prdobi/bin/sqlplus -s / as sysdba << EOF
    define report_type=html
    define num_days=1
    @/home/obiee/snu/a.sql
    @?/rdbms/admin/awrrpt
EOF

if [[ $? -eq 0 ]]; then
#mailx -s "AWR report for PRODOBIEE for Today !!! " -a awr_report_today.*  giriraj_inja@cree.com</tmp
mutt -s "AWR Report on PRODOBIEE for today " xyz@gmail.com -a awr_report*.*</tmp
else
mailx -s "PRODOBIEE instance is not up !! " xyz@gmail.com</tmp
fi


cat a.sql
set feedback off
set heading off
    define report_type=html
    define num_days=1
    define report_name=awr_report_today.html

spool /home/obiee/snu/b.sql
select 'define begin_snap=' || snap_values('MIN') from dual;
select 'define end_snap=' || snap_values('MAX') from dual;
spool off
@b.sql

cat b.sql  (This gets generated from a.sql automatically)

define begin_snap=13507                                                         

define end_snap=13509             

No comments: