Linux/Unix shell 自动发送AWR report(二)
观察Oracle数据库性能,Oracle自带的awr 功能为我们提供了一个近乎完美的解决方案,通过awr特性我们可以随时从数据库提取awr报告。不过awrrpt.sql脚本执行时需要我们提供一些交互信息,因此可以将其整合到shell脚本中来实现自动产生指定时段的awr报告并发送给相关人员。本文对Linux/Unix shell 自动发送AWR report的功能进行了完善和补充。
1、shell脚本
- robin@SZDB:~/dba_scripts/custom/awr> more autoawr_by_time.sh
- #!/bin/bash
- # --------------------------------------------------------------------------+
- # Generate AWR report and send mail automatically |
- # Filename: autoawr_by_time.sh |
- # Desc: |
- # The script use to generate awr report by time period. |
- # Three parameter for it. |
- # para1: <ORACLE_SID> mandatory parameter |
- # para2: [begin time] optional parameter |
- # para3: [end time ] optional parameter |
- # Deploy it by crontab as requirement |
- # Usage: |
- # ./autoawr_by_time.sh <instance_name> [begin time] [end time] |
- # Example: |
- # ./autoawr_by_time.sh TESTDB |
- # --default,time period is from last midnight to today midnight |
- # ./autoawr_by_time.sh TESTDB 2013031009 |
- # --time period is from 2013031009 to now |
- # ./autoawr_by_time.sh TESTDB 2013031009 2013031012 |
- # --time period by speicifed |
- # Author : Robinson |
- # Blog : http://blog.csdn.net/robinson_0612 |
- # --------------------------------------------------------------------------+
- #
- # -------------------------------
- # Set environment here
- # ------------------------------
- if [ -f ~/.bash_profile ]; then
- . ~/.bash_profile
- fi
- # ------------------------------------------------------------
- # Check the parameter, if no specify,then use default value
- # ------------------------------------------------------------
- if [ -z "${1}" ] ;then
- echo "Usage: "
- echo " `basename $0` <ORACLE_SID> [begin_date] [end_date]"
- fi
- if [ -z "${3}" ] && [ -z "${2}" ];then
- begin_date=`date -d yesterday +%Y%m%d`'00'
- end_date=`date +%Y%m%d`'00'
- elif [ -z "${3}" ]; then
- begin_date=${2}
- end_date=`date +%Y%m%d%H`
- else
- begin_date=${2}
- end_date=${3}
- fi
- ORACLE_SID=${1}
- export ORACLE_SID begin_date end_date
- export MACHINE=`hostname`
- export MAIL_DIR=/users/robin/dba_scripts/sendEmail-v1.56
- export MAIL_LIST='Robinson.chen@<span style="color:#000000;">12306</span>.com'
- export AWR_CMD=/users/robin/dba_scripts/custom/awr
- export AWR_DIR=/users/robin/dba_scripts/custom/awr/report/${ORACLE_SID}
- export MAIL_FM='oracle@szdb.com'
- RETENTION=31
- echo $ORACLE_SID
- echo $begin_date
- echo $end_date
- # --------------------------------------------------------------------
- # Check the directory for store awr report,if not exist, create it
- # --------------------------------------------------------------------
- if [ ! -d "${AWR_DIR}" ]; then
- mkdir -p ${AWR_DIR}
- fi
- # ----------------------------------------------
- # check if the database is running, if not exit
- # ----------------------------------------------
- db_stat=`ps -ef | grep pmon_$ORACLE_SID | grep -v grep| cut -f3 -d_`
- if [ -z "$db_stat" ]; then
- #date >/tmp/db_${ORACLE_SID}_stauts.log
- echo " $ORACLE_SID is not available on ${MACHINE} !!!" # >>/tmp/db_${ORACLE_SID}_stauts.log
- MAIL_SUB=" $ORACLE_SID is not available on ${MACHINE} !!!"
- MAIL_BODY=" $ORACLE_SID is not available on ${MACHINE} at `date` when try to generate AWR."
- $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_BODY
- exit 1
- fi;
- # ---------------------------------------------
- # Generate the awr report
- # ---------------------------------------------
- sqlplus -S "/ as sysdba" @${AWR_CMD}/autoawr_by_time.sql $begin_date $end_date
- status=$?
- if [ $status != 0 ];then
- echo " $ORACLE_SID is not available on ${MACHINE} !!!" # >>/tmp/db_${ORACLE_SID}_stauts.log
- MAIL_SUB=" Occurred error while generate AWR for ${ORACLE_SID} !!!"
- MAIL_BODY=" Some exceptions encountered during generate AWR report for $ORACLE_SID on `hostname`."
- $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_BODY
- exit
- fi
- # ------------------------------------------------
- # Send email with AWR report
- # ------------------------------------------------
- filename=`ls ${AWR_DIR}/${ORACLE_SID}_awrrpt_?_${begin_date}_${end_date}*`
- if [ -e "${filename}" ];then
- MAIL_SUB="AWR report from ${ORACLE_SID} on `hostname`."
- MAIL_BODY="This is an AWR report from ${ORACLE_SID} on `hostname`.Time period: $begin_date,$end_date. "
- $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_BODY -a ${filename}
- echo ${filename}
- fi
- # ------------------------------------------------
- # Removing files older than $RETENTION parameter
- # ------------------------------------------------
- find ${AWR_DIR} -name "*awrrpt*" -mtime +$RETENTION -exec rm {} \;
- exit
2、产生awr report 的sql脚本
- robin@SZDB:~/dba_scripts/custom/awr> more autoawr_by_time.sql
- SET ECHO OFF;
- SET VERI OFF;
- SET FEEDBACK OFF;
- SET TERMOUT ON;
- SET HEADING OFF;
- SET TRIMSPOOL ON;
- VARIABLE rpt_options NUMBER;
- DEFINE no_options = 0;
- define ENABLE_ADDM = 8;
- REM according to your needs, the value can be 'text' or 'html'
- DEFINE report_type='html';
- BEGIN
- :rpt_options := &no_options;
- END;
- /
- VARIABLE dbid NUMBER;
- VARIABLE inst_num NUMBER;
- VARIABLE bid NUMBER;
- VARIABLE eid NUMBER;
- BEGIN
- SELECT snap_id
- INTO :bid
- FROM dba_hist_snapshot
- WHERE TO_CHAR (end_interval_time, 'yyyymmddhh24') = '&1';
- SELECT snap_id
- INTO :eid
- FROM dba_hist_snapshot
- WHERE TO_CHAR (end_interval_time, 'yyyymmddhh24') = '&2';
- SELECT dbid INTO :dbid FROM v$database;
- SELECT instance_number INTO :inst_num FROM v$instance;
- END;
- /
- --print dbid;
- --print bid;
- --print eid;
- --print inst_num;
- COLUMN ext NEW_VALUE ext NOPRINT
- COLUMN fn_name NEW_VALUE fn_name NOPRINT;
- COLUMN lnsz NEW_VALUE lnsz NOPRINT;
- SELECT 'txt' ext
- FROM DUAL
- WHERE LOWER ('&report_type') = 'text';
- SELECT 'html' ext
- FROM DUAL
- WHERE LOWER ('&report_type') = 'html';
- SELECT 'awr_report_text' fn_name
- FROM DUAL
- WHERE LOWER ('&report_type') = 'text';
- SELECT 'awr_report_html' fn_name
- FROM DUAL
- WHERE LOWER ('&report_type') = 'html';
- SELECT '80' lnsz
- FROM DUAL
- WHERE LOWER ('&report_type') = 'text';
- SELECT '1500' lnsz
- FROM DUAL
- WHERE LOWER ('&report_type') = 'html';
- set linesize &lnsz;
- COLUMN report_name NEW_VALUE report_name NOPRINT;
- SELECT instance_name || '_awrrpt_' || instance_number || '_' ||'&&1'||'_'||'&&2'|| '.' || '&ext'
- report_name
- FROM v$instance a,
- (SELECT TO_CHAR (begin_interval_time, 'yyyymmdd') timestamp
- FROM dba_hist_snapshot
- WHERE snap_id = :bid) b;
- SET TERMOUT OFF;
- SPOOL ${AWR_DIR}/&report_name;
- --SPOOL &report_name
- SELECT output
- FROM TABLE (DBMS_WORKLOAD_REPOSITORY.&fn_name (:dbid,
- :inst_num,
- :bid,
- :eid,
- :rpt_options));
- SPOOL OFF;
- SET TERMOUT ON;
- CLEAR COLUMNS SQL;
- TTITLE OFF;
- BTITLE OFF;
- REPFOOTER OFF;
- SET TRIMSPOOL OFF;
- UNDEFINE report_name
- UNDEFINE report_type
- UNDEFINE fn_name
- UNDEFINE lnsz
- UNDEFINE no_options
- exit;
3、补充说明
a、该脚本实现了基于不同时段,不同instance自动生成awr report,具体如下
b、用法为./autoawr_by_time.sh <instance_name> [begin time] [end time],可以用于随时随地直接生成awr report
c、在省略[begin time] [end time]的情形下会自动生成昨天凌晨至今天凌晨的awr report
d、当仅仅省略[end time]时则从[begin time]开始至当前的最大snap_id来生成awr report
e、当[begin time] [end time]都被指定时则生成指定时段的awr report
f、通过调用sendEmail发送awr report,具体参考:不可或缺的 sendEmail
4、部署参考
- #如果仅仅需要一整天的awr report,直接将其部署到crontab即可。
- #如果需要一整天以及不同时段的awr report,则可以考虑采用如下方式来部署,将其合并到一个shell文件
- robin@SZDB:~/dba_scripts/custom/awr> more awr.sh
- #!/bin/bash
- dt=`date +%Y%m%d`
- start_date=$dt'05'
- end_date=$dt'09'
- /users/robin/dba_scripts/custom/awr/autoawr_by_time.sh CNMMBO #获取一整天的awr report
- /users/robin/dba_scripts/custom/awr/autoawr_by_time.sh CNMMBO $start_date #获取指定起始时间至今的awr report
- /users/robin/dba_scripts/custom/awr/autoawr_by_time.sh CNMMBO $start_date $end_date #获取指定时间段的awr report
- exit
- robin@SZDB:~/dba_scripts/custom/awr> crontab -l
- # DO NOT EDIT THIS FILE - edit the master and reinstall.
- 45 11 * * * /users/robin/dba_scripts/custom/awr/awr.sh
- 转:http://blog.csdn.net/leshami/article/details/8687690
Linux/Unix shell 自动发送AWR report(二)的更多相关文章
- Linux/Unix shell 自动发送AWR report
观察Oracle数据库性能,Oracle自带的awr 功能为我们提供了一个近乎完美的解决方案,通过awr特性我们可以随时从数据库提取awr报告.不过awrrpt.sql脚本执行时需要我们提供一些交互信 ...
- Linux/Unix shell 监控Oracle实例(monitor instance)
使用shell脚本实现对Oracle数据库的监控与管理将大大简化DBA的工作负担,如常见的对实例的监控,监听的监控,告警日志的监控,以及数据库的备份,AWR report的自动邮件等.本文给出Linu ...
- Linux/Unix shell 监控Oracle监听器(monitor listener)
使用shell脚本实现对Oracle数据库的监控与管理将大大简化DBA的工作负担,如常见的对实例的监控,监听的监控,告警日志的监控,以及数据库的备份,AWR report的自动邮件等.本文给出Linu ...
- Linux/Unix shell 监控Oracle告警日志(monitor alter log file)
使用shell脚本实现对Oracle数据库的监控与管理将大大简化DBA的工作负担,如常见的对实例的监控,监听的监控,告警日志的监控,以及数据库的备份,AWR report的自动邮件等.本文给出Linu ...
- Linux/Unix shell 脚本中调用SQL,RMAN脚本
Linux/Unix shell脚本中调用或执行SQL,RMAN 等为自动化作业以及多次反复执行提供了极大的便利,因此通过Linux/Unix shell来完成Oracle的相关工作,也是DBA必不可 ...
- Linux/Unix shell sql 之间传递变量
灵活结合Linux/Unix Shell 与SQL 之间的变量传输,极大程度的提高了DBA的工作效率,本文针对Linux/Unix shell sql 之间传递变量给出几个简单的示例以供参考. Lin ...
- UNIX SHELL基础知识总结(二)
1. vim,vi及ex的关系 vim不需要安装,vi为ex的“Visual Mode”,Vim是vi的高级版本: 2. Unix Shell 快捷键 Ctrl+a/e将光标定位到 命令的头/尾 Ct ...
- Linux Unix shell 编程指南学习笔记(第四部分)
第十六章 shell脚本介绍 此章节内容较为简单,跳过. 第十七章 条件測试 test命令 expr命令 test 格式 test condition 或者 [ conditio ...
- Linux Unix shell 编程指南学习笔记(第五部分)
第二十五章 深入讨论 << 当shell 看到 << 的时候,它知道下一个词是一个分界符.该分界符后面的内容都被当做输入,直到shell又看到该分界符(位于单独的一行).比方: ...
随机推荐
- border-radius几种写法的原理剖析
border-radius:40px; border-radius:40px/20px; border-radius:40px 20px; border-radius:40px 20px 10px 5 ...
- Gradle Goodness: Rename Ant Task Names When Importing Ant Build File
Migrating from Ant to Gradle is very easy with the importBuild method from AntBuilder. We only have ...
- 【转】Linux写时拷贝技术(copy-on-write)
http://www.cnblogs.com/biyeymyhjob/archive/2012/07/20/2601655.html 源于网上资料 COW技术初窥: 在Linux程序中,fork()会 ...
- ruby 学习 -- Array --2
定义: [1, 2, 3] # An array that holds three Fixnum objects [-10...0, 0..10,] # An array of two ranges; ...
- 物联网操作系统Hello China V1.76(PC串口版)版本发布
作为向ARM平台移植的基线版本,经过三个多月的努力,Hello China V1.76终于完成并发布.相对原来发布的V1.75版本,该版本主要做了如下修改: 彻底去掉了原来版本源代码中的C++特性,采 ...
- 转载网页博客:ie7bug:div容器下的img与div存在间隙
1.代码及在浏览器上的显示 ie7: ie8+: Firefox: Chrome: 可以看出ie7上在div容器下添加img,div与img中有间隙,而在ie8+和其他浏览器上均显示正常 网页源代码如 ...
- Android AlarmManager(全局定时器/闹钟)指定时长或以周期形式执行某项操作
AlarmManager的使用机制有的称呼为全局定时器,有的称呼为闹钟.通过对它的使用,个人觉得叫全局定时器比较合适,其实它的作用和Timer有点相似.都有两种相似的用法:(1)在指定时长后执行某项操 ...
- linux 大量的TIME_WAIT解决办法
发现存在大量TIME_WAIT状态的连接tcp 0 0 127.0.0.1:3306 127.0.0.1:41378 TIME ...
- spring springmvc mybatis 整合
环境 apache-tomcat-8.0.33.jdk1.8.0_05 maven Dynamic Web Module 2.5 1.各个xml配置文件的配置 (1)pom.xml 配置清单文件 连接 ...
- usb协议分析-设备描述符配置包-描述符
/* usb协议分析仅供大家参考---设备描述符配置包,设备描述符, 地址设置, 配置描述符, 字符串描述符 */ /* -1- usb设备描述符配置包 */ typedef struct _USB_ ...