Oracle->自动发送AWR报告
2016年9月21日
09:31
需求描述:
每日或定期手动使用AWR报告来检查Oracle数据库状态不仅耗时也费力,需求使用脚本自动收集AWR报告。
 

分析方案:
  收集AWR报告的脚本:$ORACLE_HOME/rdbms/admin/awrrpt.sql ->主要运行脚本。
  首先来查看脚本都做了写什么,用了哪些视图。
cat $ORACLE_HOME/rdbms/admin/awrrpt.sql |grep -v 'Rem'|grep -v '^--' (排除‘Rem’和‘--’相关
得到主要信息->
select d.dbid            dbid
     , d.name            db_name
     , i.instance_number inst_num
     , i.instance_name   inst_name
  from v$database d,
       v$instance i;
和关联脚本@@awrrpti
再查看@@awrrpti脚本都执行了些什么
 cat $ORACLE_HOME/rdbms/admin/awrrpt.sql |grep -v 'Rem'|grep -v '^--' (排除‘Rem’和‘--’相关注释)
   

得到核心内容->(这就是生成AWR报告的程序)
spool &report_name;

select output from table(dbms_workload_repository.&fn_name( :dbid,
:inst_num,
:bid, :eid,
:rpt_options )); spool off; select output from table(dbms_workload_repository.&fn_name( :dbid,:inst_num,:bid, :eid,:rpt_options )); &fn_name->类型名称如awr_report_html和awr_report_text
:dbid->数据库id号
:inst_num->实例名
:bid->snap开始id
:eid->snap结束id
:rpt_options->控制显示ADDM
 
关联脚本
@@awrinput.sql  ->主要获取snapshots id号
@@awrinpnm.sql 'awrrpt_' &&ext ->主要生成AWR报告名称
 
AWR生成脚本的流程:
@awrrpt.sql(提供dbid和inst_num)
 @@awrrpti.sql(主程式脚本)
  @@awrinput.sql(提供snap id)
  @@awrinpnm.sql(提供awr生成名称)
  select output from table(dbms_workload_repository.&fn_name( :dbid,:inst_num,:bid, :eid,:rpt_options ));(生成awr报表)
制定脚本:<ora_auto_awrrpt.sql>
复制 @@awrrpti.sql内容并且加以更正就行
 

--conn / as sysdba;
set echo off;
set veri off;
set feedback off;
set termout on;
set heading off; variable rpt_options number; define NO_OPTIONS = 0;
define ENABLE_ADDM = 8; -- 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;
variable dbname varchar2(9);
variable btime number;
variable etime number;
begin
select dbid into :dbid from v$database;
select instance_number into :inst_num from v$instance;
select max(snap_id)-24 into :bid from dba_hist_snapshot where DBID=:dbid;--生成的awr 快照开始时间可以自定义。注意如果中间有重启数据库,会导致获取awr报告失败。
select max(snap_id) into :eid from dba_hist_snapshot where DBID=:dbid;
select lower(NAME) into :dbname from v$database;
select to_char(end_interval_time,'YYYYMMDDHH24') into :btime from dba_hist_snapshot where snap_id=:bid;
select to_char(end_interval_time,'DDHH24') into :etime from dba_hist_snapshot where snap_id=:eid; end;
/ 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 '' lnsz from dual where lower('&report_type') = 'html'; select :dbname||'_'||:btime||'to'||:etime||'.'||'&ext' report_name from dual;
set linesize &lnsz; -- print the AWR results into the report_name file using the spool command: column report_name new_value report_name noprint; select :dbname||'_'||:btime||'to'||:etime||'.'||'&ext' report_name from dual;
set termout off;
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;
undefine report_name
undefine report_type
undefine fn_name
undefine lnsz
undefine NO_OPTIONS
exit

设定shell脚本定时执行并发送邮件。

 
Script:
[oracle@ ~]$ cat /opt/app/oracle/ora_auto/oracle_auto_awr.sh
#!/bin/bash # auto gather awrrpt.sql
#
# parameters:
# $ -> SID
#
# The whole package includes files;
# oracle_auto_awr.sh(this file)
# ora_auto_awrrpt.sql
# #引用oracle数据库环境变量
. /home/oracle/.bash_profile ->(注意加上oracle环境变量,不然crontab排程运行时不能正常执行sql脚本). /home/oracle/.bash_profile
PATH=$PATH:$HOME/bin:/sbin:/usr/bin:/usr/sbin
ORACLE_BASE=/opt/app/oracle;export ORACLE_BASE
ORACLE_HOME=$ORACLE_BASE/product/11.2./dbhome_1;export ORACLE_HOME
ORA_AUTO_PATH='/opt/app/oracle/ora_auto'
#自定义awr脚本的SQL语句
ORACLE_SQL=$ORA_AUTO_PATH/ora_auto_awrrpt.sql
DAY_TIME=`date +"%Y%m%d"`
AWR_ZIP=AWR_$DAY_TIME.zip
cd $ORA_AUTO_PATH
echo "Dear Sir:">mail.log
echo " This is oracle database Awr.">>mail.log
mkdir -p $ORA_AUTO_PATH/$DAY_TIME

#脚本后跟上多个数据库名,用于集合
array_1=$@
for sid in ${array_1[@]}
do sleep tnschk=`tnsping $sid`
tnschk2=` echo $tnschk | grep -c OK`
if [ ${tnschk2} -eq ] ; then
echo " $sid start gather." >>mail.log

###连接数据库字符串,注意连接数据库的账户是否有权限执行awr
ORACLE_CONN='xxx/xxxx@'$sid
$ORACLE_HOME/bin/sqlplus -s $ORACLE_CONN @$ORACLE_SQL >sqlplus_$sid_output.log soput="`cat $ORA_AUTO_PATH/sqlplus_$sid_output.log|grep html`"
if [ -f $soput ]; thenecho " mv $soput $ORA_AUTO_PATH/$DAY_TIME">>mail.log
mv $soput $ORA_AUTO_PATH/$DAY_TIME
fi
else
echo " No TNS Listener on $sid" >>mail.log
fi
done
echo " zip -r $AWR_ZIP $DAY_TIME" >>mail.log
#`/bin/tar -zcf $AWR_ZIP $DAY_TIME` 将awr报表进行压缩,减少邮件传输的流量
`/usr/bin/zip -r $AWR_ZIP $DAY_TIME`
echo " Thanks!">>mail.log mutt -s "[xxxx][DATABASE][AWR]: time: `date '+%y%m%d-%H%M'`" "接受邮件地址" -a $ORA_AUTO_PATH/$AWR_ZIP < $ORA_AUTO_PATH/mail.log rm -rf $DAY_TIME 设定排程[xxx.xxx.xx.x]
* * * /xxx/app/oracle/ora_auto/oracle_auto_awr.sh oracle01 oracle02 oracle03

 
 
针对脚本进行加密
[root@xxx oracle]# tar xvfz shc-3.8.7.tgz
[root@xxxx shc-3.8.7]# ./shc -r -f oracle_auto_awr.sh ->对shell脚本进行加密
oracle_auto_awr.sh ->(原文件)
oracle_auto_awr.sh.x ->(二进制文件)
oracle_auto_awr.sh.x.c ->(C语言)
shc常用参数:
-e date
 Expiration date in dd/mm/yyyy format [none](指定过期日志)
-m message
 Message to display upon expiration ["Please contact your provider"](指定提示的信息)
-f script_name
 File name ofr the script to complie(指定要编译的shell路径及文件名)
-f Relax security.
 Make a redistributable binary which executes on different systems run(可以相同操作系统的不同系统中执行)
-V Verbose compilation(编译的详细情况)
 
 
 
 
参考:

(Oracle)自定义调用AWR的更多相关文章

  1. (Oracle)自定义调用AWR&ADDM

    Oracle->自定义调用AWR&ADDM 需求描述: 前面设定每天自动生成AWR用于提供前一天的数据库状态信息,但因数据库和信息过多不利于直观检查.此次新增ADDM诊断. ADDM诊断 ...

  2. Mybatis下配置调用Oracle自定义函数返回的游标结果集

    在ibatis和Mybatis对存储过程和函数函数的调用的配置Xml是不一样的,以下是针对Mybatis 3.2的环境进行操作的. 第一步配置Mapper的xml内容 <mapper names ...

  3. Oracle中的AWR,全称为Automatic Workload Repository

    Oracle中的AWR,全称为Automatic Workload Repository,自动负载信息库.它收集关于特定数据库的操作统计信息和其他统计信息,Oracle以固定的时间间隔(默认为1个小时 ...

  4. Oracle自定义数据类型 1

    原文 oracle 自定义类型 type / create type 一 Oracle中的类型 类型有很多种,主要可以分为以下几类: 1.字符串类型.如:char.nchar.varchar2.nva ...

  5. Oracle自定义函数和存储过程示例,自定义函数与存储过程区别

    参考资料:http://www.newbooks.com.cn/info/60861.html oracle自定义函数学习和连接运算符(||) 贴一段中文文档示例,应该就可以开始工作了: --过程(P ...

  6. Oracle自定义聚集函数

    今天工作中看见别人写的自定义聚集函数,所以一门心思的想搞懂,就在网上找资料了. 自定义聚集函数 自定义聚集函数接口简介 Oracle提供了很多预定义好的聚集函数,比如Max(), Sum(), AVG ...

  7. oracle 存储过程调用 执行

    oracle 存储过程调用 博客分类: 数据库相关   oracle存储过程 2011年02月11日 星期五 14:47 SQL中调用存储过程语句: call procedure_name(); 调用 ...

  8. ORACLE存储过程调用Web Service

    1. 概述 最近在ESB项目中,客户在各个系统之间的服务调用大多都是在oracle存储过程中进行的,本文就oracle存储过程调用web service来进行说明.其他主流数据库,比如mysql和sq ...

  9. .NET易忘备留 ORACLE存储过程调用

    1.Oracle存储过程调用[返回信息,单体或者列表] public IResult FundBuild(string partnerId,string userId, DateTime beginD ...

随机推荐

  1. Apache服务器运维笔记(5)----容器的处理顺序

    容器在配置文件中是可以多次使用的,同时也可以嵌套使用,但是 Apache 在处理容器时却是有一定顺序的,因此在编写容器配置时需要按照一定的顺序来进行,否则Apache处理的结果很可能不是管理员想要的. ...

  2. T-SQL语法学习一(持续更新)

    T-SQL语法学习(一) 第一节 不常用语句 不常用语句-指的是一些不常用的查询语句,不针对业务数据查询 SET STATISTICS IO ON(用于查询逻辑读取次数,物理读取次数) 图片 sele ...

  3. easyui datagrid行编辑中数据联动

    easyui的datagrid中行内编辑使用数据联动.即:当编辑产品编号时,该行的产品名称自动根据产品编号显示出来. 在编辑中获取当前行的索引 function getRowIndex(target) ...

  4. MVC的验证(模型注解和非侵入式脚本的结合使用)

    @HtmlHrlper方式创建的标签,会自动生成一些属性,其中一些属性就是关于验证 如图示例: 模型注解 通过模型注解后,MVC的验证,包括前台客户端,后台服务器的验证,MVC统统都做了包含,即使用户 ...

  5. ssh免密码登陆失败的原因

    今天因为需要在两台服务器上进行ssh免登陆,所以安装网上的教程,ssh-keygen -t rsa,然后把相互的密钥加入到对方的authorized_keys. 问题是我们虽然这样做了,却一直要密码, ...

  6. July 25th 2017 Week 30th Tuesday

    Everything is always more beautiful reflected in your eyes. 一切事物映在你的眼里都会变得更美. Looking in your eyes, ...

  7. ZT 俞敏洪:2014我要闭嘴 相信未来不是梦

    俞敏洪:2014我要闭嘴 相信未来不是梦 手机免费访问www.cnfol.com2013年12月11日 08:38 中国企业家网 查看评论 字体:小  中  大中金在线微博微信加关注 扫描二维码 关注 ...

  8. backtrack渗透测试中常用的命令总结

    ping 域名/ip 测试本机到远端主机是否联通. dig 域名/ip 查看域名解析的详细信息. host -l 域名 dns服务器 传输zone. 扫描 nmap: -sS 半开扫描TCP和SYN扫 ...

  9. Gradle入门实战(Windows版)

    Installation Gradle runs on all major operating systems and requires only a Java JDK or JRE version ...

  10. 阅读SessionFactory源码

    一.阅读类注释 ①.SessionFactory的主要任务是创建Session的实例.通常一个应用程序只有一个单一的SessionFactory对象,而且线程从这个SessionFactory中获取S ...