Linux/Unix shell 自动发送AWR report(二)

简介:

 观察Oracle数据库性能,Oracle自带的awr 功能为我们提供了一个近乎完美的解决方案,通过awr特性我们可以随时从数据库提取awr报告。不过awrrpt.sql脚本执行时需要我们提供一些交互信息,因此可以将其整合到shell脚本中来实现自动产生指定时段的awr报告并发送给相关人员。本文对Linux/Unix shell 自动发送AWR report的功能进行了完善和补充。   

 

1、shell脚本

[python]  view plain  copy
 
 print?
  1. robin@SZDB:~/dba_scripts/custom/awr> more autoawr_by_time.sh  
  2. #!/bin/bash  
  3. # --------------------------------------------------------------------------+  
  4. #                 Generate AWR report and send mail automatically           |  
  5. #   Filename: autoawr_by_time.sh                                            |  
  6. #   Desc:                                                                   |  
  7. #       The script use to generate awr report by time period.               |  
  8. #       Three parameter for it.                                             |  
  9. #           para1: <ORACLE_SID>   mandatory parameter                       |  
  10. #           para2: [begin time]   optional parameter                        |    
  11. #           para3: [end time  ]   optional parameter                        |  
  12. #       Deploy it by crontab as requirement                                 |    
  13. #   Usage:                                                                  |  
  14. #       ./autoawr_by_time.sh <instance_name> [begin time] [end time]        |    
  15. #   Example:                                                                |  
  16. #       ./autoawr_by_time.sh TESTDB                                         |     
  17. #            --default,time period is from last midnight to today midnight  |  
  18. #       ./autoawr_by_time.sh TESTDB 2013031009                              |  
  19. #            --time period is from 2013031009 to now                        |  
  20. #       ./autoawr_by_time.sh TESTDB 2013031009 2013031012                   |  
  21. #            --time period by speicifed                                     |   
  22. #   Author : Robinson                                                       |   
  23. #   Blog   : http://blog.csdn.net/robinson_0612                             |  
  24. # --------------------------------------------------------------------------+  
  25. #  
  26. # -------------------------------  
  27. #  Set environment here   
  28. # ------------------------------  
  29.   
  30. if [ -f ~/.bash_profile ]; then  
  31.     . ~/.bash_profile  
  32. fi  
  33.   
  34. # ------------------------------------------------------------  
  35. #  Check the parameter, if no specify,then use default value  
  36. # ------------------------------------------------------------  
  37.   
  38. if [ -z "${1}" ] ;then  
  39.     echo "Usage: "  
  40.     echo "      `basename $0` <ORACLE_SID> [begin_date] [end_date]"  
  41. fi  
  42.   
  43. if [ -z "${3}" ] && [ -z "${2}" ];then  
  44.     begin_date=`date -d yesterday +%Y%m%d`'00'  
  45.     end_date=`date +%Y%m%d`'00'  
  46. elif [ -z "${3}" ]; then  
  47.     begin_date=${2}  
  48.     end_date=`date +%Y%m%d%H`  
  49. else  
  50.     begin_date=${2}  
  51.     end_date=${3}  
  52. fi  
  53.   
  54. ORACLE_SID=${1}  
  55. export ORACLE_SID begin_date end_date   
  56. export MACHINE=`hostname`  
  57. export MAIL_DIR=/users/robin/dba_scripts/sendEmail-v1.56  
  58. export MAIL_LIST='Robinson.chen@<span style="color:#000000;">12306</span>.com'  
  59. export AWR_CMD=/users/robin/dba_scripts/custom/awr  
  60. export AWR_DIR=/users/robin/dba_scripts/custom/awr/report/${ORACLE_SID}  
  61. export MAIL_FM='oracle@szdb.com'  
  62. RETENTION=31  
  63.   
  64. echo $ORACLE_SID   
  65. echo $begin_date  
  66. echo $end_date  
  67. # --------------------------------------------------------------------  
  68. #  Check the directory for store awr report,if not exist, create it  
  69. # --------------------------------------------------------------------  
  70.   
  71. if [ ! -d "${AWR_DIR}" ]; then  
  72.     mkdir -p ${AWR_DIR}  
  73. fi  
  74.   
  75. # ----------------------------------------------  
  76. # check if the database is running, if not exit  
  77. # ----------------------------------------------  
  78.   
  79. db_stat=`ps -ef | grep pmon_$ORACLE_SID | grep -v grep| cut -f3 -d_`  
  80. if [ -z "$db_stat" ]; then  
  81.     #date >/tmp/db_${ORACLE_SID}_stauts.log  
  82.     echo " $ORACLE_SID is not available on ${MACHINE} !!!"   # >>/tmp/db_${ORACLE_SID}_stauts.log   
  83.     MAIL_SUB=" $ORACLE_SID is not available on ${MACHINE} !!!"  
  84.     MAIL_BODY=" $ORACLE_SID is not available on ${MACHINE} at `date` when try to generate AWR."  
  85.     $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_BODY   
  86.     exit 1  
  87. fi;  
  88.   
  89. # ---------------------------------------------  
  90. #  Generate the awr report  
  91. # ---------------------------------------------  
  92.   
  93. sqlplus -S "/ as sysdba" @${AWR_CMD}/autoawr_by_time.sql $begin_date $end_date   
  94.   
  95. status=$?  
  96. if [ $status != 0 ];then  
  97.     echo " $ORACLE_SID is not available on ${MACHINE} !!!"   # >>/tmp/db_${ORACLE_SID}_stauts.log  
  98.     MAIL_SUB=" Occurred error while generate AWR for ${ORACLE_SID}  !!!"  
  99.     MAIL_BODY=" Some exceptions encountered during generate AWR report for $ORACLE_SID on `hostname`."  
  100.     $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_BODY  
  101.     exit  
  102. fi  
  103.   
  104. # ------------------------------------------------  
  105. # Send email with AWR report  
  106. # ------------------------------------------------  
  107.   
  108. filename=`ls ${AWR_DIR}/${ORACLE_SID}_awrrpt_?_${begin_date}_${end_date}*`  
  109. if [ -e "${filename}" ];then  
  110.     MAIL_SUB="AWR report from ${ORACLE_SID} on `hostname`."  
  111.     MAIL_BODY="This is an AWR report from ${ORACLE_SID} on `hostname`.Time period: $begin_date,$end_date. "  
  112.     $MAIL_DIR/sendEmail -u $MAIL_SUB -f $MAIL_FM -t $MAIL_LIST -m $MAIL_BODY -a ${filename}  
  113.     echo ${filename}  
  114. fi  
  115.   
  116. # ------------------------------------------------  
  117. # Removing files older than $RETENTION parameter   
  118. # ------------------------------------------------  
  119.   
  120. find ${AWR_DIR} -name "*awrrpt*" -mtime +$RETENTION -exec rm {} \;  
  121.   
  122. exit  

2、产生awr report 的sql脚本

[sql]  view plain  copy
 
 print?
  1. robin@SZDB:~/dba_scripts/custom/awr> more autoawr_by_time.sql  
  2. SET ECHO OFF;  
  3. SET VERI OFF;  
  4. SET FEEDBACK OFF;  
  5. SET TERMOUT ON;  
  6. SET HEADING OFF;  
  7. SET TRIMSPOOL ON;  
  8.   
  9. VARIABLE rpt_options NUMBER;  
  10. DEFINE no_options = 0;  
  11.   
  12. define ENABLE_ADDM = 8;  
  13.   
  14. REM according to your needs, the value can be 'text' or 'html'  
  15.   
  16. DEFINE report_type='html';  
  17.   
  18. BEGIN  
  19.    :rpt_options := &no_options;  
  20. END;  
  21. /  
  22.   
  23. VARIABLE dbid NUMBER;  
  24. VARIABLE inst_num NUMBER;  
  25. VARIABLE bid NUMBER;  
  26. VARIABLE eid NUMBER;  
  27.   
  28. BEGIN  
  29.       SELECT snap_id  
  30.         INTO :bid  
  31.         FROM dba_hist_snapshot  
  32.        WHERE TO_CHAR (end_interval_time, 'yyyymmddhh24') = '&1';  
  33.   
  34.       SELECT snap_id  
  35.         INTO :eid  
  36.         FROM dba_hist_snapshot  
  37.        WHERE TO_CHAR (end_interval_time, 'yyyymmddhh24') = '&2';  
  38.   
  39.    SELECT dbid INTO :dbid FROM v$database;  
  40.   
  41.    SELECT instance_number INTO :inst_num FROM v$instance;  
  42. END;  
  43. /  
  44.   
  45. --print dbid;  
  46. --print bid;  
  47. --print eid;  
  48. --print inst_num;  
  49.   
  50. COLUMN ext NEW_VALUE ext NOPRINT  
  51. COLUMN fn_name NEW_VALUE fn_name NOPRINT;  
  52. COLUMN lnsz NEW_VALUE lnsz NOPRINT;  
  53. SELECT 'txt' ext  
  54.   FROM DUAL  
  55.  WHERE LOWER ('&report_type') = 'text';  
  56.   
  57. SELECT 'html' ext  
  58.   FROM DUAL  
  59.  WHERE LOWER ('&report_type') = 'html';  
  60.   
  61. SELECT 'awr_report_text' fn_name  
  62.   FROM DUAL  
  63.  WHERE LOWER ('&report_type') = 'text';  
  64.   
  65. SELECT 'awr_report_html' fn_name  
  66.   FROM DUAL  
  67.  WHERE LOWER ('&report_type') = 'html';  
  68.   
  69. SELECT '80' lnsz  
  70.   FROM DUAL  
  71.  WHERE LOWER ('&report_type') = 'text';  
  72.   
  73. SELECT '1500' lnsz  
  74.   FROM DUAL  
  75.  WHERE LOWER ('&report_type') = 'html';  
  76.   
  77. set linesize &lnsz;  
  78. COLUMN report_name NEW_VALUE report_name NOPRINT;  
  79.   
  80. SELECT instance_name || '_awrrpt_' || instance_number || '_' ||'&&1'||'_'||'&&2'|| '.' || '&ext'  
  81.           report_name  
  82.   FROM v$instance a,  
  83.        (SELECT TO_CHAR (begin_interval_time, 'yyyymmdd') timestamp  
  84.           FROM dba_hist_snapshot  
  85.          WHERE snap_id = :bid) b;  
  86.   
  87. SET TERMOUT OFF;  
  88. SPOOL ${AWR_DIR}/&report_name;  
  89. --SPOOL &report_name  
  90.   
  91. SELECT output  
  92.   FROM TABLE (DBMS_WORKLOAD_REPOSITORY.&fn_name (:dbid,  
  93.                                                  :inst_num,  
  94.                                                  :bid,  
  95.                                                  :eid,  
  96.                                                  :rpt_options));  
  97. SPOOL OFF;  
  98. SET TERMOUT ON;  
  99. CLEAR COLUMNS SQL;  
  100. TTITLE OFF;  
  101. BTITLE OFF;  
  102. REPFOOTER OFF;  
  103. SET TRIMSPOOL OFF;  
  104.   
  105. UNDEFINE report_name  
  106. UNDEFINE report_type  
  107. UNDEFINE fn_name  
  108. UNDEFINE lnsz  
  109. UNDEFINE no_options  
  110. 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、部署参考

[python]  view plain  copy
 
 print?
    1. #如果仅仅需要一整天的awr report,直接将其部署到crontab即可。  
    2. #如果需要一整天以及不同时段的awr report,则可以考虑采用如下方式来部署,将其合并到一个shell文件  
    3. robin@SZDB:~/dba_scripts/custom/awr> more awr.sh  
    4. #!/bin/bash  
    5. dt=`date +%Y%m%d`  
    6. start_date=$dt'05'  
    7. end_date=$dt'09'  
    8. /users/robin/dba_scripts/custom/awr/autoawr_by_time.sh CNMMBO               #获取一整天的awr report  
    9. /users/robin/dba_scripts/custom/awr/autoawr_by_time.sh CNMMBO $start_date   #获取指定起始时间至今的awr report  
    10. /users/robin/dba_scripts/custom/awr/autoawr_by_time.sh CNMMBO $start_date $end_date #获取指定时间段的awr report  
    11. exit   
    12. robin@SZDB:~/dba_scripts/custom/awr> crontab -l  
    13. # DO NOT EDIT THIS FILE - edit the master and reinstall.  
    14. 45  11 * * * /users/robin/dba_scripts/custom/awr/awr.sh 
    15. 转:http://blog.csdn.net/leshami/article/details/8687690
文章可以转载,必须以链接形式标明出处。

本文转自 张冲andy 博客园博客,原文链接: http://www.cnblogs.com/andy6/p/5877500.html   ,如需转载请自行联系原作者
相关文章
|
18天前
|
Web App开发 Java Linux
Linux之Shell基本命令篇
Linux之Shell基本命令篇
Linux之Shell基本命令篇
|
16天前
|
存储 Shell Linux
【攻防世界】unseping (反序列化与Linux bash shell)
【攻防世界】unseping (反序列化与Linux bash shell)
|
19天前
|
Shell Linux
【Linux】12. 模拟实现shell
【Linux】12. 模拟实现shell
27 2
|
25天前
|
Shell Linux
Linux的shell入门教程shell脚本入门教程
Linux的shell入门教程shell脚本入门教程
16 0
|
1月前
|
存储 算法 Shell
【Linux 环境变量相关】深入理解Linux下 CMake、Shell 与环境变量的交互(二)
【Linux 环境变量相关】深入理解Linux下 CMake、Shell 与环境变量的交互
50 0
|
1月前
|
Shell Linux 开发工具
shell的介绍以及Linux权限的讲解
shell的介绍以及Linux权限的讲解
31 2
|
1月前
|
网络协议 Shell Linux
【Shell 命令集合 系统管理 】Linux 查询域名的注册信息 whois命令 使用指南
【Shell 命令集合 系统管理 】Linux 查询域名的注册信息 whois命令 使用指南
48 1
|
5天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
31 3
|
2天前
|
监控 Shell 应用服务中间件
第十二章 Shell脚本编写及常见面试题(二)
第十二章 Shell脚本编写及常见面试题(二)
|
2天前
|
监控 关系型数据库 Shell
第十二章 Shell脚本编写及常见面试题(一)
第十二章 Shell脚本编写及常见面试题(一)