获取AWR的脚本,可以在crontab里面部署

使用方法可以在脚本最后查看,或者使用-h参数获取帮助。


版本更新:

    1 修改不能在rac环境执行的问题。

            修改后获取本节点的awr,不能指定获取其他节点的awr



因为shell脚本不能直接上传,我使用云盘分享脚本。

脚本获取路径:

链接:https://pan.baidu.com/s/1BeBnJJ7DVweX_eRQkiX_NQ 

提取码:z5bb 



#!/bin/bash
#by raysuen
#v02
. ~/.bash_profile
AWR_FORMAT=html
NUM_DAYS=2
############################################
#获取指定时间的snapid的函数
############################################
getsnapID(){
	BEGIN_SNAP_ID=`sqlplus -S / as sysdba <<-RAY
		set heading off trimspool on feedback off
		SELECT trim(SNAP_ID) FROM DBA_HIST_SNAPSHOT a,v\\$instance b where to_char(END_INTERVAL_TIME,'yyyymmddhh24')='$1' and a.instance_number=b.instance_number;
		RAY`
	END_SNAP_ID=`sqlplus -S / as sysdba <<-RAY
		set heading off trimspool on feedback off
		SELECT trim(SNAP_ID) FROM DBA_HIST_SNAPSHOT a,v\\$instance b where to_char(END_INTERVAL_TIME,'yyyymmddhh24')='$2' and a.instance_number=b.instance_number;
		RAY`
	#判断获取的snapid是否为空
	if [ -z ${BEGIN_SNAP_ID} ];then
		echo "The script can not get a valid snap id,please enter a right time for -b."
		exit 96
	fi
	if [ -z ${BEGIN_SNAP_ID} ];then
		echo "The script can not get a valid snap id,please enter a right time for -e."
		exit 96
	fi
	BEGIN_SNAP_ID=`echo ${BEGIN_SNAP_ID} | sed 's/ //g'`
	END_SNAP_ID=`echo ${END_SNAP_ID} | sed 's/ //g'`
}
############################################
#获取帮助的函数
############################################
my_fun(){
	echo "SYNOPSIS:"
	echo "	./GET_AWR.sh -b begin_time -e end_time -n awr_name"
	echo "OPTIONS:"
	echo "	-b specify a time for begin time of awr,format yyyymmddhh24"
	echo "	-e specify a time for begin time of awr,format yyyymmddhh24"
	echo "	-n specify a name for name of awr"
	echo "EXAMPLE:"
	echo "	./GET_AWR.SH -b 2019051708 -e 2019051709 -n test"
	echo "	./GET_AWR.sh -b \`date +'%Y%m%d18' -d '+1 day ago'\` -e \`date +'%Y%m%d19' -d '+1 day ago'\`  -n test"
}
############################################
#脚本入口,获取参数
############################################
if [ $# -lt 1 ];then
	echo "You must specify parameters:"
	echo "	-b begin time of awr"
	echo "	-e end time of awr"
	exit 99
fi
while (($#>=1))
do
	if [ "$1" == "-b" ];then
		shift
		awrbegintime=$1
		shift
		continue
	fi
	if [ "$1" == "-e" ];then
		shift
		awrendtime=$1
		shift
		continue
	fi
	if [ "$1" == "-n" ];then
		shift
		awrname=$1
		shift
		continue
	fi
	if [ "$1" == "-h" ];then
		my_fun
		exit 0
	fi
	shift
done
############################################
#健壮性检查
############################################
#参数不可以为空
if [ -z ${awrbegintime} ];then
	echo "You must specify parameters:-b for begin time of awr"
	exit 98
fi
if [ -z ${awrendtime} ];then
	echo "You must specify parameters:-e for end time of awr"
	exit 98
fi
if [ -z ${awrname} ];then
	echo "You must specify parameters:-n for report name of awr"
	exit 98
fi
#判断参数为时间
date -d "${awrbegintime:0:8} ${awrbegintime:8:2}" > /dev/null 2>&1
if [ $? -ne 0 ];then
	echo "The valus of -b is invalid date."
	exit 97
fi
date -d "${awrendtime:0:8} ${awrendtime:8:2}" > /dev/null 2>&1
if [ $? -ne 0 ];then
	echo "The valus of -e is invalid date."
	exit 97
fi
############################################
#执行函数,获取snap id
############################################
getsnapID ${awrbegintime} ${awrendtime}
############################################
#定义awr报告的路径
############################################
AWR_LOG=/u02/logout/awr/AWR_${awrname}_${awrbegintime}_${awrendtime}.html
############################################
#获取awr报告
############################################
echo -e "$AWR_FORMAT\n$NUM_DAYS\n$BEGIN_SNAP_ID\n$END_SNAP_ID\n$AWR_LOG\n"|(sqlplus -S / as sysdba @?/rdbms/admin/awrrpt.sql) > /dev/null
############################################
#帮助:
#	参数区分大小写
#	-b awr的开始时间,格式:yyyymmddhh24
#   -e awr的结束时间,格式:yyyymmddhh24
#   -n awr报告中的名字
#例子:
#	./GET_AWR.SH -b "2019051708" -e "2019051709" -n test
#	./GET_AWR.sh -b `date +'%Y%m%d18' -d '+1 day ago'` -e `date +'%Y%m%d19' -d '+1 day ago'`  -n test
############################################


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28572479/viewspace-2644807/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28572479/viewspace-2644807/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值