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

简介:

准备工作

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

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

set echo off heading on underline on;

column inst_numheading "Inst Num"new_value inst_numformat 99999;

column inst_name heading "Instance"new_value inst_name format a12;

column db_nameheading "DB Name"new_value db_nameformat a12;

column dbidheading "DB Id"new_value dbidformat 9999999999 just c;

prompt

prompt Current Instance

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

select d.dbiddbid

, d.namedb_name

, i.instance_number inst_num

, i.instance_nameinst_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

promptDefaults 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_snapshotorder by snap_id) where rownum<10;

SNAP_IDDBID INSTANCE_NUMBER

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

1848099102932

1848099102931

1858099102932

1858099102931

1868099102932

1868099102931

1878099102932

1878099102931

1888099102932

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

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

 









本文转自东方之子736651CTO博客,原文链接: http://blog.51cto.com/ecloud/1378460,如需转载请自行联系原作者






相关文章
|
11月前
|
JSON 算法 API
深度分析小红书城API接口,用Python脚本实现
小红书作为以UGC内容为核心的生活方式平台,其非官方API主要通过移动端抓包解析获得,涵盖内容推荐、搜索、笔记详情、用户信息和互动操作等功能。本文分析了其接口体系、认证机制及请求规范,并提供基于Python的调用框架,涉及签名生成、登录态管理与数据解析。需注意非官方接口存在稳定性与合规风险,使用时应遵守平台协议及法律法规。
|
11月前
|
JSON API 数据安全/隐私保护
【干货满满】分享微店API接口到手价,用python脚本实现
微店作为知名社交电商平台,其开放平台提供商品查询、订单管理等API接口。本文介绍如何通过微店API获取商品到手价(含优惠、券等),涵盖认证机制、Python实现及关键说明。
|
11月前
|
JSON API 数据安全/隐私保护
【干货满满】分享淘宝API接口到手价,用python脚本实现
淘宝开放平台通过API可获取商品到手价,结合商品详情与联盟接口实现优惠计算。需使用AppKey、AppSecret及会话密钥认证,调用taobao.tbk.item.info.get接口获取最终价格。代码示例展示签名生成与数据解析流程。
|
11月前
|
JSON API 数据格式
深度分析大麦网API接口,用Python脚本实现
大麦网为国内领先演出票务平台,提供演唱会、话剧、体育赛事等票务服务。本文基于抓包分析其非官方接口,并提供Python调用方案,涵盖演出列表查询、详情获取及城市列表获取。需注意非官方接口存在稳定性风险,使用时应遵守平台规则,控制请求频率,防范封禁与法律风险。适用于个人学习、演出信息监控等场景。
|
11月前
|
JSON API 开发者
深度分析阿里妈妈API接口,用Python脚本实现
阿里妈妈是阿里巴巴旗下营销平台,提供淘宝联盟、直通车等服务,支持推广位管理、商品查询等API功能。本文详解其API调用方法,重点实现商品推广信息(佣金、优惠券)获取,并提供Python实现方案。
|
11月前
|
JSON API 数据安全/隐私保护
深度分析虾皮城API接口,用Python脚本实现
虾皮开放平台提供丰富的API接口,支持商品管理、订单处理及促销信息查询等功能。本文详解API认证机制与调用方法,基于Python实现商品价格及到手价获取方案,适用于电商数据分析与运营。
|
11月前
|
JSON API 数据安全/隐私保护
【干货满满】分享拼多多API接口到手价,用python脚本实现
拼多多开放平台提供商品价格查询API,通过“pdd.ddk.goods.detail”接口可获取商品基础价、优惠券、拼团价等信息。结合client_id、client_secret及签名机制实现身份认证,支持推广位ID获取专属优惠。本文提供完整Python实现,涵盖签名生成、接口调用与价格解析逻辑,适用于比价工具、导购平台等场景。
|
11月前
|
API 数据安全/隐私保护 开发者
深度分析苏宁API接口,用Python脚本实现
深度分析苏宁API接口,用Python脚本实现
|
11月前
|
前端开发 Shell API
深度分析58同城API接口,用Python脚本实现
58同城为国内知名分类信息平台,涵盖房产、招聘、二手车等多领域。本文基于网页抓包与解析,分享其非官方接口的Python实现方案,分析核心接口特性与反爬应对策略,适用于数据学习与信息聚合。注意:非官方接口存在风险,使用需遵守平台规则。
|
11月前
|
JSON API 数据安全/隐私保护
【干货满满】分享京东API接口到手价,用python脚本实现
淘宝开放平台提供丰富API,通过商品详情接口与淘宝联盟接口,可获取含优惠券、满减后的商品到手价。本文介绍基于Python的实现方案,涵盖签名生成、接口调用、价格解析及错误处理,适用于比价工具、导购平台等场景。

热门文章

最新文章