Oracle AWR 报告 每天自动生成并发送邮箱 Python脚本

2024-04-04 03:08

本文主要是介绍Oracle AWR 报告 每天自动生成并发送邮箱 Python脚本,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 

       Oracle AWR 报告能很好的提供有关DB性能的信息。 所以DBA 需要定期的查看AWR的报告。 有关AWR报告的说明参考:

       Oracle AWR 介绍

       http://blog.csdn.net/tianlesoftware/archive/2009/10/17/4682300.aspx

 

       如果说每天都去手工的出创建AWR报告,是个费时的活。 所以,最好就是弄成脚本来执行。 这篇文章就是说明这个问题的。

 

       注意的一个问题,AWR 报告的两个snap 之间不能有重启DB的操作。

 

Python 执行 系统命令

http://blog.csdn.net/tianlesoftware/archive/2011/02/17/6192202.aspx

 

Python 发送带 附件 邮件 脚本

http://www.cndba.cn/Dave/article/417

 

 

. 准备工作

 

       一般我们都是条用awrrpt.sql 来创建我们的AWR报告。 我们先看下这个脚本的具体内容:

[oracle@rac1 admin]$ cat awrrpt.sql | grep -v 'Rem'|grep -v '^--'

set echo off heading on underline on;

column inst_num  heading "Inst Num"  new_value inst_num  format 99999;

column inst_name heading "Instance"  new_value inst_name format a12;

column db_name   heading "DB Name"   new_value db_name   format a12;

column dbid      heading "DB Id"     new_value dbid      format 9999999999 just c;

 

prompt

prompt Current Instance

prompt ~~~~~~~~~~~~~~~~

 

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

 

undefine num_days;

undefine report_type;

undefine report_name;

undefine begin_snap;

undefine end_snap;

 

       在以上的脚本里,我们发现它只是生成了一些变量,然后把这些变量传给了另一个脚本:awrrpti.sql 我们看下awrrpti.sql 脚本的具体内容:

 

[oracle@rac1 admin]$ cat awrrpti.sql | grep -v 'Rem'|grep -v '^--' 

 

set echo off;

set veri off;

set feedback off;

 

variable rpt_options number;

 

define NO_OPTIONS   = 0;

define ENABLE_ADDM  = 8;

 

 

begin

  :rpt_options := &NO_OPTIONS;

end;

/

 

prompt

prompt Specify the Report Type

prompt ~~~~~~~~~~~~~~~~~~~~~~~

prompt Would you like an HTML report, or a plain text report?

prompt Enter 'html' for an HTML report, or 'text' for plain text

prompt  Defaults to 'html'

 

column report_type new_value report_type;

set heading off;

select 'Type Specified: ',lower(nvl('&&report_type','html')) report_type from dual;

set heading on;

 

set termout off;

column ext new_value ext;

select '.html' ext from dual where lower('&&report_type') <> 'text';

select '.txt' ext from dual where lower('&&report_type') = 'text';

set termout on;

 

@@awrinput.sql

-- 这个脚本主要是确定SNAP的。

@@awrinpnm.sql 'awrrpt_' &&ext

-- 这个脚本主要是确定AWR 文件名称的

 

set termout off;

column fn_name new_value fn_name noprint;

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') <> 'text';

 

column lnsz new_value lnsz noprint;

select '80' lnsz from dual where lower('&report_type') = 'text';

select '1500' lnsz from dual where lower('&report_type') <> 'text';

 

set linesize &lnsz;

set termout on;

spool &report_name;

 

select output from table(dbms_workload_repository.&fn_name( :dbid,

                                                    :inst_num,

                                                    :bid, :eid,

                                                    :rpt_options ));

 

 

spool off;

 

prompt Report written to &report_name.

 

set termout off;

clear columns sql;

ttitle off;

btitle off;

repfooter off;

set linesize 78 termout on feedback 6 heading on;

undefine report_name

 

undefine report_type

undefine ext

undefine fn_name

undefine lnsz

 

undefine NO_OPTIONS

undefine ENABLE_ADDM

 

undefine top_n_events

undefine num_days

undefine top_n_sql

undefine top_pct_sql

undefine sh_mem_threshold

undefine top_n_segstat

 

whenever sqlerror continue;

[oracle@rac1 admin]$

 

 

       这个脚本才是我们真正生成AWR的脚本。 在这个脚本里面,提示我们选择AWR报告的类型。

 

 

通过上面的2个脚本,我们将AWR报告简化一下:

       select output from

table(dbms_workload_repository.&fn_name(:dbid, :inst_num,:bid, :eid,:rpt_options ));

 

这条语句就是整个AWR报告的核心:

1&fn_name 决定AWR报告的类型,有2个值:awr_report_htmlawr_report_text

2dbidinst_num,bid,eid 可以通过dba_hist_snapshot查询. bid 指的是begin snap_id, eid 指的是end snap_id.

 

 

SQL> select * from (select snap_id,dbid,instance_number from dba_hist_snapshot  order by snap_id) where rownum<10;

 

   SNAP_ID       DBID INSTANCE_NUMBER

---------- ---------- ---------------

       184  809910293               2

       184  809910293               1

       185  809910293               2

       185  809910293               1

       186  809910293               2

       186  809910293               1

       187  809910293               2

       187  809910293               1

       188  809910293               2

 

9 rows selected.

 

       我这里是个RAC 环境, 通过这个可以看出在每个节点上都保存着AWR的信息。

 

3rpt_options:该参数控制是否显示ADDM的。

--  NO_OPTIONS -

--    No options. Setting this will not show the ADDM

--    specific portions of the report.

--    This is the default setting.

--

--  ENABLE_ADDM -

--    Show the ADDM specific portions of the report.

--    These sections include the Buffer Pool Advice,

--    Shared Pool Advice, PGA Target Advice, and

--    Wait Class sections.

define NO_OPTIONS   = 0;

define ENABLE_ADDM  = 8;

 

 

有了上面的数据之后,我们就可以使用如下SQL直接生成AWR报告了。

SQL>select output from table(dbms_workload_repository.awr_report_html(809910293, 2,220,230,0));

 

SQL>select output from table(dbms_workload_repository.awr_report_text(809910293, 2,220,230,0));

 

 

. 生成AWR报告 SQL脚本

       以上写了这么多,就是为了一个脚本:myawrrpt.sql.  这个脚本就是自动的去收集信息。 因为如果我们是调用awrrpt.sql的话,需要输入一些参数。 我们修改一下脚本,让它根据我们的需求来收集信息,这样就不用输入参数了。

 

[oracle@rac1 admin]$ cat myawrrpt.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;

begin

select max(snap_id)-48 into :bid from dba_hist_snapshot;

select max(snap_id) into :eid from dba_hist_snapshot;

select dbid into :dbid from v$database;

select instance_number into :inst_num from v$instance;

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 '1500' lnsz from dual where lower('&report_type') = 'html';

 

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 'awr'||'.'||'&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

[oracle@rac1 admin]$

 

       这个脚本是收集过去48个小时的snap 来生成AWR 生成的文件名称是awr .html,这个也是spool 指定的,可以生成其他名称。

 

 

. 自动上传AWRPython脚本

 

       在这个脚本里做2件事,第一是调用第二步里的SQL脚本,生成awr报告,然后将AWR 发送到指定邮箱。

 

 

createSendAWR.py

 

#!/usr/bin/python

#coding=gbk

#created by tianlesoftware

#2011-4-12

 

import os

import sys

import smtplib

import pickle

import mimetypes

from email.MIMEText import MIMEText

from email.MIMEImage import MIMEImage

from email.MIMEMultipart import MIMEMultipart

 

 

SMTP_SERVER='192.168.1.120'

EMAIL_USER='user'

EMAIL_PASSWD='pwd'

EMAIL_SUBJECT='192.168.88.209 AWR Report'

FROM_USER='daimingming@1876.cn'

TO_USERS=['daimingming@1876.cn','dvd.dba@gmail.com']

 

 

def createawr():

  pipe = os.popen(' /u01/app/oracle/product/10.2.0/db_1/bin/sqlplus /nolog @awrrpt.sql')

 

def mysendmail(fromaddr,toaddrs,subject):

 

        COMMASPACE=','

        msg = MIMEMultipart()

        msg['From'] = fromaddr

        msg['To'] = COMMASPACE.join(toaddrs)

        msg['Subject'] = subject

 

     

        txt = MIMEText("192.168.88.209 AWR Report, The report be send at 9 AM every day ")

        msg.attach(txt)  

 

  

        fileName = r'/home/oracle/awr.html'

        ctype, encoding = mimetypes.guess_type(fileName)

        if ctype is None or encoding is not None:

            ctype = 'application/octet-stream'

        maintype, subtype = ctype.split('/', 1)

        att = MIMEImage((lambda f: (f.read(), f.close()))(open(fileName, 'rb'))[0], _subtype = subtype)

        att.add_header('Content-Disposition', 'attachment', filename = fileName)

        msg.attach(att)  

 

        server=smtplib.SMTP(SMTP_SERVER)

        server.login(EMAIL_USER,EMAIL_PASSWD)

        server.sendmail(fromaddr,toaddrs,msg.as_string())

        server.quit()

 

if __name__=='__main__':

               createawr()

        mysendmail(FROM_USER, TO_USERS, EMAIL_SUBJECT)

        #print 'send successful'

 

 

. Python 添加到crontab

 

 

[oracle@qs-wg-db1 scripts]$ crontab -l

 

40 17 * * * export ORACLE_HOME='/home/oracle_app' && ORACLE_SID=XX  && cd /u01/backup/scripts && /u01/backup/scripts/createSendAWR.py >/u01/backup/scripts/createSendAWR.log 2>&1

 

我这里因为报了

SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory

的错误,所以把变量加了上去。

 

 

  Linux Crontab 定时任务 命令详解

  http://blog.csdn.net/tianlesoftware/archive/2010/02/21/5315039.aspx

 

 

 

 

 

-------------------------------------------------------------------------------------------------------

QQ: 492913789
Email: ahdba@qq.com
Blog: http://www.cndba.cn/dave

DBA1 群:62697716();   DBA2 群:62697977()   DBA3 群:62697850()  

DBA 超级群:63306533();  DBA4 群: 83829929  DBA5群: 142216823   

聊天 群:40132017   聊天2群:69087192

--加群需要在备注说明Oracle表空间和数据文件的关系,否则拒绝申请

这篇关于Oracle AWR 报告 每天自动生成并发送邮箱 Python脚本的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/874656

相关文章

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python正则表达式匹配和替换的操作指南

《Python正则表达式匹配和替换的操作指南》正则表达式是处理文本的强大工具,Python通过re模块提供了完整的正则表达式功能,本文将通过代码示例详细介绍Python中的正则匹配和替换操作,需要的朋... 目录基础语法导入re模块基本元字符常用匹配方法1. re.match() - 从字符串开头匹配2.

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

通过Docker容器部署Python环境的全流程

《通过Docker容器部署Python环境的全流程》在现代化开发流程中,Docker因其轻量化、环境隔离和跨平台一致性的特性,已成为部署Python应用的标准工具,本文将详细演示如何通过Docker容... 目录引言一、docker与python的协同优势二、核心步骤详解三、进阶配置技巧四、生产环境最佳实践

Python一次性将指定版本所有包上传PyPI镜像解决方案

《Python一次性将指定版本所有包上传PyPI镜像解决方案》本文主要介绍了一个安全、完整、可离线部署的解决方案,用于一次性准备指定Python版本的所有包,然后导出到内网环境,感兴趣的小伙伴可以跟随... 目录为什么需要这个方案完整解决方案1. 项目目录结构2. 创建智能下载脚本3. 创建包清单生成脚本4

MyBatis Plus实现时间字段自动填充的完整方案

《MyBatisPlus实现时间字段自动填充的完整方案》在日常开发中,我们经常需要记录数据的创建时间和更新时间,传统的做法是在每次插入或更新操作时手动设置这些时间字段,这种方式不仅繁琐,还容易遗漏,... 目录前言解决目标技术栈实现步骤1. 实体类注解配置2. 创建元数据处理器3. 服务层代码优化填充机制详

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

oracle 11g导入\导出(expdp impdp)之导入过程

《oracle11g导入导出(expdpimpdp)之导入过程》导出需使用SEC.DMP格式,无分号;建立expdir目录(E:/exp)并确保存在;导入在cmd下执行,需sys用户权限;若需修... 目录准备文件导入(impdp)1、建立directory2、导入语句 3、更改密码总结上一个环节,我们讲了