#!/bin/ksh93
#----------------------------------------------------------#
# 函数名: add_nhours 假设当前日期20120723
# 用法 : add_nhours 24; 返回 20120722
# add_nhours -48; 返回 20120725
# 功能 : 计算当前日期相对n小时的日期
# 说明 : 会改变环境变量,建议不用,推荐add_ndays函数
#----------------------------------------------------------#
function add_nhours {
typeset -i nhours;
((nhours=$1));
oldtz=`echo $TZ`; #系统原时区 -8
tz_hour=`echo $TZ|sed 's/.*\(..\)/\1/'`; #取最后两位 -8
new_tz=`echo $TZ|sed 's/..$//'`;
if [ $nhours -ge 0 ]; then
tz_hour=`expr $tz_hour + $nhours`;
else
nhours=`echo $nhours | cut -c 2-`; #从第2个字节开始取$nhours串,即去掉负号
tz_hour=`expr $tz_hour - $nhours`;
fi
TZ=$new_tz$tz_hour;
export TZ;
nhours_date=`date "+%Y%m%d"`;
TZ=$oldtz;
export TZ;
echo $nhours_date;
}
function get_begin_id {
snap_date=$1;
snap_id_file=$2;
sqlplus -s / as sysdba << !
set pages 0
set head off
set lines 100
set feedback off
set timing off
spool $snap_id_file
select 'snapid='||snap_id as id
from dba_hist_snapshot
where to_char(BEGIN_INTERVAL_TIME,'yyyymmdd hh24')='${snap_date}';
spool off
!
}
#-----------------------------------------------#
s_dir=/orabak/cv_scripts; # 脚本所在目录
log_dir=/orabak/cv_logs;
cd $log_dir;
last_day=`add_nhours 24`;
log=$log_dir/awr_make_${last_day}.log; # 日志
awr_dir=awr_${last_day}; # 保存 awr 的目录
snap_date="$last_day 00";
snap_id_file=$log_dir/spool_snap_id_${last_day}.txt; # 保存快照起始ID的文件
get_begin_id "$snap_date" $snap_id_file; # 快照起始ID保存到文件 $snap_id_file 中
BEGIN_ID=`sed -n "/snapid=/p" $snap_id_file | awk -F"[=]" '{print $2}'`; # 快照起始ID
END_ID=`expr $BEGIN_ID + 24 `; # 快照结束ID
echo "--> snap begin: $last_day" >> $log;
echo "--> snap begin hour: $snap_date " >> $log;
echo "--> snap begin id: $BEGIN_ID " >> $log;
echo "--> snap end id: $END_ID " >> $log;
if [ -d $awr_dir ] ; then
echo "--> awr dir $awr_dir exist!" >> $log;
else
echo "--> awr dir $awr_dir not exist!" >> $log;
mkdir $awr_dir;
if [ -d $awr_dir ] ; then
echo "--> create dir $awr_dir success!" >> $log;
else
echo "--> create dir $awr_dir fail !" >> $log;
exit 1;
fi
fi
cd $awr_dir; # 进入保存 awr 的目录
#dbid=344767908; # zscoredb dbid
dbid=344767908; # coredb dbid
inst_num=1; # 实例序号
rpt_options=0;
SNAP_HOUR=1; # 一个小时生成一个awr
i_id=$BEGIN_ID;
#i_id=`expr $i_id + 1`;
#-- 循环生成awr --#
while (( i_id <= END_ID ))
do
i_id2=`expr $i_id + $SNAP_HOUR `;
awr_name=awr_1_${i_id}_${i_id2}.html;
echo " --> snap_id $i_id" >> $log;
echo " --> snap_id $i_id2" >> $log;
sqlplus -s /nolog << !
conn / as sysdba
set linesize 1500
set termout off
set echo off
set feedback off
set SERVEROUT off
set head off
spool $awr_name
select output from table(dbms_workload_repository.awr_report_html($dbid,$inst_num,$i_id,$i_id2,$rpt_options));
spool off
!
i_id=`expr $i_id + $SNAP_HOUR `;
done
cd $log_dir;
mv $snap_id_file $log $awr_dir ;
tar -cf $awr_dir.tar $awr_dir;
gzip $awr_dir.tar;
rm -rf $awr_dir ;
awrgzip=${awr_dir}.tar.gz
ftp -i -n xx.xx.xx.xx
bin
cd awr
put $awrgzip
bye
!
find $log_dir -name "awr*.tar.gz" -mtime +60 | xargs -i /bin/rm {};
exit 0;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22661144/viewspace-1674530/,如需转载,请注明出处,否则将追究法律责任。