********************需要根据自己的实际环境修改哦****************************
********************
1. 收集awr报告样本 awrreport.sql
--该脚本请用具有 dba 权限的用户执行,普通用户没有权限访问数据库的基表
conn &usr/ &pass @ &oracle_sid
set linesize 1200 ;
set pagesize 0;
set long 99999;
set heading off;
--set termout off;
set echo off;
set feedback off;
set timing off;
set serveroutput on;
exec dbms_output.put_line(‘report_name,for example:hydk‘);
spool awrrpt_tmp.sql;
select
‘spool &report_name‘||‘_awrrpt_‘||snap_id||‘_‘||(snap_id+1)||‘.lst‘||chr(10)||
‘select output ‘||chr(10)||
‘ from table(dbms_workload_repository.awr_report_text(‘||dbid||‘,1,‘||snap_id||‘,‘||(snap_id+1)||‘));‘||chr(10)||‘spool off;‘||chr(10)
from dba_hist_snapshot;
whenever sqlerror continue;
spool off ;
@awrrpt_tmp.sql;
---- 这里要等一段时间,多敲几下回车以保证上面的语句都执行 ---
host del awrrpt_tmp.sql;
exit;
********************
2. .bat文件 批量执行sql脚本,避免 重复的复制粘贴
@echo off
echo ***************************************************
echo * *
echo * 此脚本用于数据库的awr报告文件 *
echo * *
echo ***************************************************
echo
echo 请按照[ ]中的提示输入参数,如不输入,则自动设为默认
echo.
rem -------------------------------------------------------
%~d0
cd %~dp0
set setup=setup.sql
set NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK
echo set echo off >> %setup%
echo set verify off >> %setup%
rem -------------------------------------------------------------------
rem 脚本内容写入setup.sql一起运行
echo.
set model=
if "%model%"=="1" goto model1
if "%model%"=="" goto model1
goto end
:model1
echo @./start/awrreport.sql >> %setup%
goto next
:next
rem 运行脚本
echo exit >> %setup%
sqlplus /nolog @%setup%
echo 运行成功
del setup.sql
::exit
:end
exit
********************
3. perl工具 解析 生成的 .lst 文件
rem 该脚本用于使用perl工具 解析 生成的 .lst 文件 (以实际环境为准)
rem 进入指定盘符
c:
rem 进入该盘符下的指定目录,改盘符就是 那些.lst文件的目录
cd C:\Users\Administrator\Desktop\mon_ora11g\脚本生成awr报告文件
rem 使用绝对路径下的perl工具,解析 生成的 .lst 文件
E:\app\oracle\product\11.2.0\db_1\perl\bin\perl.exe -w awrreport.pl
********************
4. perl 工具的 配置文件
use strict;
#use File::Find;
#
my $root_dir=‘.‘;
open(DataFile, ">$root_dir\\fzjk.txt") || die "can not create file";
print DataFile " 日期 ", " 时间 ", " Redo size",
" Logical reads", " Block changes",
" Physical reads", " Physical writes",
" User calls", " Parses",
" Hard parses",
" Logons", " Executes",
" Transactions",
"\n";
print DataFile " PerSecond PerTransaction PerSecond PerTransaction",
" PerSecond PerTransaction", " PerSecond PerTransaction",
" PerSecond PerTransaction", " PerSecond PerTransaction",
" PerSecond PerTransaction", " PerSecond PerTransaction",
" PerSecond PerTransaction",
" PerSecond PerTransaction",
" PerSecond",
"\n";
my @xljk = ("Buffer Nowait", "Redo NoWait", "Buffer Hit", "In-memory Sort",
"Library Hit", "Soft Parse", "Execute to Parse", "Latch Hit","Parse CPU to Parse Elapsd",
"Non-Parse CPU");
open(XLFile, ">$root_dir\\xljk.txt") || die "can not create file";
print XLFile " 日期 ", " 时间 ";
foreach my $i ([email protected])
{
print XLFile " $xljk[$i]| ";
}
print XLFile "\n";
open(GXZFile, ">$root_dir\\gxcjk.txt") || die "can not create file";
my @gxzjk = ( "Memory Usage ","SQL with executions>1","Memory for SQL w/exec>1");
print GXZFile " 日期 ", " 时间 ";
foreach my $i ([email protected])
{
print GXZFile " $gxzjk[$i]| ";
}
print GXZFile "\n begin end ave begin end begin end";
print GXZFile "\n";
#下例仅遍历当前目录:
my($file);
my @filename;
opendir(myDir, $root_dir);
while ($file = readdir myDir)
{
#@filename = (@filename, $file) if ($file =~/lst$/i);
if ($file =~/lst$/i)
{
@filename = (@filename, $file);
#$file =~/(\d+)\D*(\d+)/;
#print $1, $2, "\n";
}
}
closedir(myDir);
#print @filename, "\n";
#sub subdig($a)
#
@filename = sort {
$_=$a;
m/(\d+)\D*/;
my $aa = $1;
#print $aa;
$_ = $b;
m/(\d+)\D*/;
#print " ", $1, "\n";
$aa <=> $1; } @filename;
my @xlpat = ("Buffer Nowait %:", "Redo NoWait %:", "Buffer Hit %:", "In-memory Sort %:",
"Library Hit %:", "Soft Parse %:", "Execute to Parse %:", "Latch Hit %:",
"Parse CPU to Parse Elapsd %:", "% Non-Parse CPU:");
my @xlpatlen = (13,11,11,14,11,10,16,9,25,13);
my @gxzpat = ("Memory Usage %:","% SQL with executions>1:"," % Memory for SQL w/exec>1:");
my @gxzpatlen = (12,21,23);
foreach my $i ([email protected])
{
open(Spreport, "$root_dir\\$filename[$i]" ) || warn "can not open file\n";
#print DataFile $root_dir ."\\". $filename[$i], "\n";
while(my $line = <Spreport> ) #获取文件中的每一行
{
if( $line =~/Begin Snap:\s+\d+\s+(\S+)\s*(\S+)\s*(\S+)/ )
{
if(index($3,":")!=-1)
{print DataFile $1,$2,$3, " ";
print XLFile $1,$2,$3, " ";
print GXZFile $1,$2,$3, " ";
}
else
{print DataFile $1,$2," ", " ";
print XLFile $1,$2, " ";
print GXZFile $1,$2, " ", " ";
}
}
#Redo size
if(($line =~/Redo size\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/Redo size:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
{
# $line =~/:\s+(\S+)\s*(\S+)/;
printf(DataFile "%-8s %-12s ",$1,$2);
#print DataFile $1," ", $2, " ";
}
#Logical read
if(($line =~/Logical read\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/Logical reads:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
#if( $line =~/Logical reads\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/Redo sizes:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
{
printf(DataFile "%-9s %-12s ",$1,$2);
}
#Block changes
if(($line =~/Block changes\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/Block changes:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
#if( $line =~/Block changes\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ &&$line =~/:\s+(\S+)\s*(\S+)/ )
{
printf(DataFile "%-8s %-12s ",$1,$2);
}
#Physical read
if(($line =~/Physical read\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/Physical reads:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
#if(($line =~/Logical read\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/Logical reads:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
#if( $line =~/Physical reads\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ &&$line =~/:\s+(\S+)\s*(\S+)/ )
{
printf(DataFile "%-8s %-12s ",$1,$2);
}
#Physical write
if(($line =~/Physical write\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/Physical writes:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
#if( $line =~/User calls\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ &&$line =~/:\s+(\S+)\s*(\S+)/ )
{
printf(DataFile "%-8s %-12s ",$1,$2);
}
#User calls
if(($line =~/User calls\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/User calls:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
#if(($line =~/User calls\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/User callss+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
{
printf(DataFile "%-8s %-12s ",$1,$2);
}
#Parses
if(($line =~/Parses\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/Parses:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
#if( $line =~/parses\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ &&$line =~/:\s+(\S+)\s*(\S+)/ )
{
printf(DataFile "%-8s %-12s ",$1,$2);
}
#Hard parses
if(($line =~/Hard parses\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/Hard parses:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
#if( $line =~/Hard parses\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ &&$line =~/:\s+(\S+)\s*(\S+)/ )
{
printf(DataFile "%-8s %-12s ",$1,$2);
}
# if(($line =~/Physical write\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/Physical write:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
# if( $line =~/Sorts\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ &&$line =~/:\s+(\S+)\s*(\S+)/ )
# {
# printf(DataFile "%-8s %-12s ",$1,$2);
# }
#Logons
if(($line =~/Logons\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/Logons:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
#if( $line =~/Logons\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ &&$line =~/:\s+(\S+)\s*(\S+)/ )
{
printf(DataFile "%-8s %-12s ",$1,$2);
}
#Executes
if(($line =~/Executes\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/Executes:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
#if( $line =~/Executes\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ &&$line =~/:\s+(\S+)\s*(\S+)/ )
{
printf(DataFile "%-8s %-12s ",$1,$2);
}
#Transactions
if(($line =~/Transactions\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ || $line =~/Transactions:\s+(\S+)\s*(\S+)/)&& $line =~/:\s+(\S+)\s*(\S+)/ )
#if( $line =~/Transactions\s+(\S+)\s*(\S+):\s+(\S+)\s*(\S+)/ &&$line =~/:\s+(\S+)\s*(\S+)/ )
{
printf(DataFile "%-8s",$1);
}
foreach my $j ([email protected])
{
printf(XLFile "%-$xlpatlen[$j]s ", $1) if($line =~/$xlpat[$j]\s+(\S+)/ );
}
foreach my $j ([email protected])
{
if($line =~/$gxzpat[$j]\s+(\S+)\s+(\S+)/ )
{
printf(GXZFile "%-6s %-6s ", $1, $2) ;
printf(GXZFile "%-7s ",($1+$2)/2) if($j==0);
printf GXZFile " " if($j==1);
}
}
}
print DataFile "\n";
print XLFile "\n";
print GXZFile "\n";
close Spreport;
}
close DataFile;
close XLFile;
close GXZFile;