您的位置:首页 > 运维架构 > Shell

自动生成awr报告shell脚本--每天早8点到晚12点每四个小时生成一次

2017-11-15 18:06 525 查看
一共两个脚本

生成脚本:dba_oracle_awr.sh

调用脚本:awr_everyday.sh

#!/usr/bin/bash
# ********************************
# * dba_oracle_awr.sh
# ********************************
# Usage: dba_oracle_awr.sh -s [instance_name]
#          -f [from time]
#          -t [to time]
#          -p [report type, html or text]
#          -h [oracle home]
#          -n [tns admin]
#
#         time format: 'yyyymmddhh24miss'.
#         E.g 20110304170000 means 05:00:00pm, Mar 04, 2011
#
#
# **********************
# get parameters
# **********************
while getopts ":s:f:t:p:h:n" opt
do
case $opt in
s) instance=$OPTARG
;;
f) from=$OPTARG
;;
t) to=$OPTARG
;;
p) type=$OPTARG
type=$(echo $type|tr "[:upper:]" "[:lower:]")
;;
h) oracle_home=$OPTARG
;;
n) tns_admin=$OPTARG
;;
'?') echo "$0: invalid option -$OPTARG">&2
exit 1
;;
esac
done
if [ "$instance" = "" ]
then
echo "instance name(-s) needed"
echo "program exiting..."
exit 1
fi
if [ "$from" = "" ]
then
echo "from time (-f} needed"
echo "program exiting..."
exit 1
fi
if [ "$to" = "" ]
then
echo "to time (-t) needed"
echo "program exiting..."
exit 1
fi
if [[ "$oracle_home" = "" ]]
then
echo "oracle home (-h) needed"
echo "program exiting..."
exit 1
fi
sqlplus="${oracle_home}/bin/sqlplus"
echo $sqlplus
if [ "$type" = "" ]
then
type="html"
fi
# ********************
# trim function
# ********************
function trim()
{
local result
result=`echo $1|sed 's/^ *//g' | sed 's/ *$//g'`
echo $result
}
# *******************************
# get begin and end snapshot ID
# *******************************
define_dur()
{
begin_id=$from
ret_code=$?
if [ "$ret_code" != "0" ]
then
echo "sqlplus failed with code $ret_code"
echo "program exiting..."
exit 10
fi
end_id=$to
ret_code=$?
if [ "$ret_code" != "0" ]
then
echo "sqlplus failed with code $ret_code"
echo "program exiting..."
exit 10
fi
begin_id=$(trim ${begin_id})
end_id=$(trim ${end_id})
# echo "begin_id: $begin_id  end_id: $end_id"
}
# *******************************
# generate AWR report
# *******************************
generate_awr()
{
awrsql="${oracle_home}/rdbms/admin/awrrpt.sql"
if [ ! -e $awrsql ]
then
echo "awrrpt.sql does not exist, exiting..."
exit 20
fi
tmp1_id=${begin_id}
#echo "begin_id is: $begin_id"
#echo "tmp1_id is: $tmp1_id"
while [ ${tmp1_id} -lt ${end_id} ]
do
let tmp2_id=${tmp1_id}+4
if [ $type = "text" ]
then
report_name="awrrpt_${instance}_${tmp1_id}_${tmp2_id}.txt"
else
report_name="awrrpt_${instance}_${tmp1_id}_${tmp2_id}.html"
fi
#echo $report_name
$sqlplus -s / as sysdba<<EOF
set term off
define report_type=$type
define num_days=1
define begin_snap=${tmp1_id}
define end_snap=${tmp2_id}
define report_name=${report_name}
@${oracle_home}/rdbms/admin/awrrpt.sql
exit;
EOF
tmp1_id=${tmp2_id}
done
}
# *******************************
# main routing
# *******************************
define_dur
generate_awr


#!/usr/bin/bash
# ********************************
# * awr_everyday.sh
# ********************************
export ORACLE_SID=csspm
export ORACLE_BASE=/u01/app/oracle
export ORACLE_HOME=/u01/app/oracle/product/11.2.0/db_1
#最大snap_id
m=(`sqlplus -S "/ as sysdba" << EOF
set pagesize 0 feedback off verify off heading off echo off
select max(snap_id) from dba_hist_snapshot where begin_interval_time>to_date(to_char(sysdate,'yyyymmdd')||'08'||to_char(sysdate,'miss'),'yyyymmddhh24miss') and end_interval_time<=to_date(to_char(sysdate,'yyyymmdd')||'23'||to_char(sysdate,'miss'),'yyyymmddhh24miss');
EOF`)
#最小snap_id
n=(`sqlplus -S "/ as sysdba" << EOF
set pagesize 0 feedback off verify off heading off echo off
select min(snap_id) from dba_hist_snapshot where begin_interval_time>to_date(to_char(sysdate,'yyyymmdd')||'08'||to_char(sysdate,'miss'),'yyyymmddhh24miss') and end_interval_time<=to_date(to_char(sysdate,'yyyymmdd')||'23'||to_char(sysdate,'miss'),'yyyymmddhh24miss');
EOF`)

#调用自动生成awr报告脚本,参数在dba_oracle_awr.sh脚本的上面有说明
bash /home/oracle/dba_oracle_awr.sh -s csspm -f $n -t $m -p html -h /u01/app/oracle/product/11.2.0/db_1


参考文章:

https://www.cnblogs.com/Clark-cloud-database/p/7813873.html

http://blog.csdn.net/zhyuh_perl/article/details/6250231

由于直接引用无法实现,上面代码是经本人修改测试过的:)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  awr