awr report automatically generated script every day

The original reference to the script written by someone else, but there are some mistakes place, can not be executed directly, it had become modified

Followed by another according to their business needs, set up the next generation time awr 


#!/bin/bash                                              

# Load the environment variable file oracle, otherwise sqlplus command can not be executed in the crontab scheduled tasks in (or with a source /home/oracle/.bash_profile)

. /home/oracle/.bash_profile

# Define variables ---- generated awr report file types

AWR_FORMAT = html

# Define variables ---- pull snap list in a few days               

NUM_DAYS=3

End snap ID # define variables ---- generated awr report, namely: end_snap  

MAX_SNAP_ID=$(echo `sqlplus -S / as sysdba <<EOF

set heading off trimspool on feedback off pagesize 0

SELECT trim(max(SNAP_ID))-3 FROM DBA_HIST_SNAPSHOT;

EOF`)

# Define a variable ---- generated awr report begins snap ID, namely: begin_snap

MIN_SNAP_ID=`expr $MAX_SNAP_ID - 24` 

# Awr report generated file path and file name

AWR_LOG=/home/oracle/AWR_shahand_`date '+%Y%m%d_%H%M'`.html

# Awr report generation

echo -e "$AWR_FORMAT\n$NUM_DAYS\n$MIN_SNAP_ID\n$MAX_SNAP_ID\n$AWR_LOG\n"|(sqlplus -S / as sysdba @?/rdbms/admin/awrrpt.sql) 






Improved version

Under the oracle user

crontab -e

00 8 * * * /home/oracle/auto_awr.sh


mkdir /home/oracle/awr_log/

vi /home/oracle/auto_awr.sh

#!/bin/bash                                              


#每天早上8点执行,取昨天0点到今天0点的awr报告

#加载一下oracle的环境变量文件,不然 sqlplus 命令在crontab 计划任务里不能被执行(或者用 source /home/oracle/.bash_profile)

郑州同济医院:http://jbk.39.net/yiyuanzaixian/zztjyy/

. /home/oracle/.bash_profile

export ORACLE_SID=这里填写数据库的sid

#定义变量----生成awr报告的文件类型

AWR_FORMAT=html

#定义变量---- 拉取几天内的snap列表               

NUM_DAYS=3

#定义变量----生成的awr报告的结束snap ID  ,即:end_snap  

MAX_SNAP_ID=$(echo `sqlplus -S / as sysdba <<EOF

set heading off trimspool on feedback off pagesize 0

SELECT trim(max(SNAP_ID))-8 FROM DBA_HIST_SNAPSHOT;

EOF`)


#定义变量----生成的awr报告的开始snap ID  ,即:begin_snap

MIN_SNAP_ID=`expr $MAX_SNAP_ID - 24` 

#生成的awr报告的文件路径及文件名

AWR_LOG=/home/oracle/awr_log/AWR_shahand_`date '+%Y%m%d_%H%M'`_00_24.html

#生成awr报告

echo -e "$AWR_FORMAT\n$NUM_DAYS\n$MIN_SNAP_ID\n$MAX_SNAP_ID\n$AWR_LOG\n"|(sqlplus -S / as sysdba @?/rdbms/admin/awrrpt.sql) 


#每天早上8点执行,取昨天9点到昨天17点的awr报告

#最大值减去15就是昨天的17点

MAX_SNAP_ID_2=$(echo `sqlplus -S / as sysdba <<EOF

set heading off trimspool on feedback off pagesize 0

SELECT trim(max(SNAP_ID))-15 FROM DBA_HIST_SNAPSHOT;

EOF`)

#最小值=最大值减去8就是9点了,这里expr里面的计算两个值之间与减号一定要有空格,否则报错

MIN_SNAP_ID=`expr $MAX_SNAP_ID - 8`

#设置awr报告生成路径及文件名

AWR_LOG=/home/oracle/awr_log/AWR_shahand_`date '+%Y%m%d_%H%M'`_09_17.html

#生成awr报告的命令

echo -e "$AWR_FORMAT\n$NUM_DAYS\n$MIN_SNAP_ID\n$MAX_SNAP_ID\n$AWR_LOG\n"|(sqlplus -S / as sysdba @?/rd


Guess you like

Origin blog.51cto.com/14510269/2431345