暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

非交互式生成awr,ash报告

原创 陈坤 云和恩墨 2022-05-24
1743

最近在写一键生成awr报告的脚本,需要用python代码生成awr报告。

我们一般生成awr使用的一般方式有两种:


方式一

一种是手动交互式的生成报告,以Oracle 的19c 的PDB生成awr报告为例:

直接输入

@?/rdbms/admin/awrrpt.sql

然后弹出交互界面,以此让你选择

生成html格式,还是txt格式

Specify the Report Type
~~~~~~~~~~~~~~~~~~~~~~~
AWR reports can be generated in the following formats. Please enter the
name of the format at the prompt. Default value is 'html'.

'html' HTML format (default)
'text' Text format
'active-html' Includes Performance Hub active report

Enter value for report_type: html
old 1: select 'Type Specified: ',lower(nvl('&&report_type','html')) report_type from dual
new 1: select 'Type Specified: ',lower(nvl('html','html')) report_type from dual

选择PDB还是ROOT

Specify the location of AWR Data
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
AWR_ROOT - Use AWR data from root (default)
AWR_PDB - Use AWR data from PDB

选择天数:

Entering the number of days (n) will result in the most recent
(n) days of snapshots being listed. Pressing <return> without
specifying a number lists all completed snapshots.

选择开始的snap id和截止的snap id

(略)

然选择导出的文件名或使用默认

之后就会生成报告

缺点:

这样手工生成awr报告的方式,需要每一步去指定选项,在脚本自动化中不是特别好实现。

当然,也可以在脚本中使用expect去一步步规划好输入的结果,当然这样做显然不够优雅


方式二

使用DBMS_WORKLOAD_REPOSITORY.AWR_REPORT_HTML包生成

先是根据时间取得snap id

然后使用sql命令

SELECT *
FROM TABLE(DBMS_WORKLOAD_REPOSITORY.AWR_REPORT_HTML(dbid,
instance_num,
begin_snap_id,
end_snap_id,
0));
然后输出的结果就是html格式的awr报告,获取结果,保存为.html文件就可以了

缺点

适合脚本编写,但又需要将结果转存一次,步骤多余,也耗时间,生成报告的时间比第一种慢。


非交互式生成报告

那么怎样才能效率又好,又适合脚本编写呢?

其实阅读awrrpt.sql源码,在开头的注释中就提到了:

If you want to use this script in an non-interactive fashion,
see the 'customer-customizable report settings' section in
awrrpti.sql


那我们再去看awrrpti.sql脚本源码:

If you want to use this script in an non-interactive fashion,
without executing the script through awrrpt, then
do something similar to the following:

define inst_num = 1;
define num_days = 3;
define inst_name = 'Instance';
define db_name = 'Database';
define dbid = 4;
define begin_snap = 10;
define end_snap = 11;
define report_type = 'text';
define report_name = /tmp/swrf_report_10_11.txt
@@?/rdbms/admin/awrrpti

原来只需要预先定义好生成报告需要的变量,然后再执行.sql文件,就可以直接跳过交互过程

那我们尝试:

define report_type = 'html';
define awr_location = 'AWR_PDB';
define num_days = 3;
define begin_snap = 4422;
define end_snap = 4423;
define report_name = 'tmp/awrrpt_20220524_4422.html';
@?/rdbms/admin/awrrpt.sql

果然,在指定目录生成了awr报告文件

也并不需要设置那么多变量,就按交互式的需要设置的将对应变量设置好,就可以生成了

这种方式也需要先取到snap_id


最后,发散想一想,ash是不是也可以这样来生成?

查看ashrpt.sql,果然,在开头的注释里也写明了对应的方法,和生成awr报告类似。

最后修改时间:2022-05-24 22:38:51
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论