知识大全 自动清除statspack所产生的snapshot旧记录

Posted 讯息

篇首语:真正的知识是道德。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 自动清除statspack所产生的snapshot旧记录相关的知识,希望对你有一定的参考价值。

  下面的 script可以利用cron排程来自动执行清除超过保留数目的旧有snapshot资料 这个script不需要知道PERFSTAT此帐号的密码就可执行 并已经经由Oracle 和 上测试过      步骤      )储存这个script取名为sp_purge ksh在Unix主机上      )注意你的系统上tmp目录是否存在 如果你不想所有产生的log写到/tmp去 你必须去更改script      )如果你的oratab这个目录位置不是在/var/opt/oracle 你就必须手动去更新script来配合你的环境      )设定可执行权限给script chmod u+x sp_purge ksh     )设定cron job来执行这个script 执行这个script需要三个参数     要清除 snapshot的资料库名称     要保留的 snapshot数量     执行后要寄发电子邮件的对象      * * /scripts/sp_purge ksh prod >>/tmp/sp_purge_portal log >& &    这个范例是说 星期一到星期五每天晚上七点执行此 script 针对 prod 这个资料库只保留最近的 个snapshots纪录 多余的则清除 并且寄发讯息给      )注意这个 script应该配合指定的instance一起执行 如果这台主机上并没有这个script所指定的instance在执行中 一个简单的讯息可在tmp目录下找到     *** ERROR: The ORACLE_SID specified in parameter is not a valid SID     (Note that the SID is case sensitive )     )所有产生的执行纪录都可以在/tmp下找到     #!/bin/ksh  # Script Name: sp_purge ksh  # This script is designed to purge StatsPack snapshots   #  # Parameter $ is the name of the database   # Parameter $ is the maximum number of snapshots to retain   # Parameter $ is the mail recipient for success messages   #  # To succeed this script must be run on the machine on which the  # instance is running   # Example for calling this script:  #  # sp_purge ksh prod us  # Script History:  #  # Who Date Action  #   # Mark J Rogers Sep Script creation   #  #  #  tmp_dir=/tmp  # Validate the parameters     if [[ $# ne ]]; then  echo   echo *** ERROR: You must specify these parameters:   echo   echo : the name of the database   echo : the maximum # of snapshots to retain   echo : the mail recipient for success messages   echo   exit   fi    grep ^$ : /var/opt/oracle/oratab >> /dev/null  if [[ $? ne ]]; then  echo   echo *** ERROR: The ORACLE_SID specified in parameter is not a valid SID   echo (Note that the SID is case sensitive )   echo   exit   fi    if [[ ! ($ ge ) ]]; then  echo   echo *** ERROR: Parameter must specify the # of snapshots to retain   echo   exit   fi    # Ensure that the instance is running on the current machine   ps ef | grep pmon | grep $ >> /dev/null  if [[ $? ne ]]; then  echo   echo *** ERROR: Instance $ is not running on machine `uname n`   echo on `date`   echo The instance must be running on the current machine for this   echo script to function properly   echo   echo Exiting   echo   exit   fi    # Establish error handling for this UNIX script   function errtrap   the_status=$?  echo   echo *** ERROR: Error message $the_status occured on line number $   echo   echo *** The script is aborting   echo   exit $the_status      trap     errtrap $LINENO     ERR    # Set up the Oracle environment     export ORACLE_SID=$   export ORAENV_ASK=NO   oraenv    script_name=$ ##*/  echo   echo Script: $script_name   echo started on: `date`   echo by user: `id`   echo on machine: `uname n`   echo   echo This script is designed to purge StatsPack snapshots for the   echo $ORACLE_SID database   echo   echo You have requested to retain no more than $ StatsPack snapshots   echo     tmp_script=$tmp_dir/sp_purge_tmp_$ORACLE_SID ksh # script to actually purge  tmp_output=$tmp_dir/sp_purge_tmp_$ORACLE_SID out # output to be mailed    rm f $tmp_script  rm f $tmp_output    sqlplus s <<EOF_SP<br />/ as sysdba    whenever sqlerror exit failure rollback  whenever oserror exit failure rollback    SET SERVEROUTPUT ON  SET FEEDBACK OFF    VARIABLE P_SNAPS_TO_RETAIN NUMBER  VARIABLE P_LOSNAPID NUMBER  VARIABLE P_HISNAPID NUMBER    BEGIN  /* Assign values to these variables */  :P_SNAPS_TO_RETAIN := $ ;  :P_LOSNAPID := ;  :P_HISNAPID := ;  END;  /     Identify the snapshot ids to purge if any     DECLARE    V_LOSNAPID NUMBER := NULL; Low snapshot ID to purge   V_HISNAPID NUMBER := NULL; High snapshot ID to purge   V_COUNT NUMBER := NULL; Number of snapshots current saved   V_COUNTER NUMBER := ; Temporary counter variable   V_DBID NUMBER := NULL; Current database ID   V_INSTANCE_NUMBER NUMBER := NULL; Current instance number   V_SNAPS_TO_RETAIN NUMBER := :P_SNAPS_TO_RETAIN; Max snaps to retain     BEGIN    select  d dbid   i instance_number  INTO  v_DBID   V_INSTANCE_NUMBER  from  v$database d   v$instance i;    select  count(snap_id)  into  v_count  from  perfstat stats$snapshot  where  dbid = V_DBID AND  instance_number = V_INSTANCE_NUMBER;    IF V_COUNT <= V_SNAPS_TO_RETAIN THEN     We do NOT need to perform a purge     DBMS_OUTPUT PUT_LINE ( NOTE: There are only ||  to_char(v_count) || snapshots currently saved );    ELSE     We DO need to perform a purge     DBMS_OUTPUT PUT_LINE ( There are currently ||  to_char(v_count) || snapshots saved );     Obtain the low snapshot id to be purged     select  min(snap_id)  into  V_LOSNAPID  from  perfstat stats$snapshot  where  dbid = V_DBID AND  instance_number = V_INSTANCE_NUMBER;     Obtain the high snapshot id to be purged     FOR V_HISNAPID_REC IN  (SELECT  SNAP_ID  FROM  perfstat stats$snapshot  WHERE  dbid = V_DBID AND  instance_number = V_INSTANCE_NUMBER  ORDER BY  SNAP_ID DESC)  LOOP  V_COUNTER := V_COUNTER + ;  IF V_COUNTER > V_SNAPS_TO_RETAIN THEN  V_HISNAPID := V_HISNAPID_REC SNAP_ID;  EXIT; Exit this LOOP and proceed to the next statement   END IF;  END LOOP;    :P_LOSNAPID := V_LOSNAPID;  :P_HISNAPID := V_HISNAPID;    END IF;    END;  /    prompt   Generate the specific purge script   set linesize   spool $tmp_script  begin  IF (:P_LOSNAPID <> ) THEN  /* Build the script to purge the StatsPack snapshots */  dbms_output put_line( #!/bin/ksh );  dbms_output put_line( #THIS IS THE SCRIPT TO ACTUALLY PERFORM THE PURGE );  dbms_output put_line( trap exit $? ERR );  dbms_output put_line( sqlplus s << SP_EOF );  dbms_output put_line( / as sysdba );  dbms_output put_line( whenever sqlerror exit failure rollback );  dbms_output put_line( whenever oserror exit failure rollback );  dbms_output put_line( @ $ORACLE_HOME/rdbms/admin/sppurge sql );  dbms_output put_line(:P_LOSNAPID);  dbms_output put_line(:P_HISNAPID);  dbms_output put_line( the following are needed again );  dbms_output put_line( whenever sqlerror exit failure rollback );  dbms_output put_line( whenever oserror exit failure rollback );  dbms_output put_line( mit; );  dbms_output put_line( exit );  dbms_output put_line( SP_EOF );  dbms_output put_line( exit $? );  END IF;  end;  /  spool off    exit  EOF_SP    if [[ ! ( f $tmp_script) ]]; then  echo   echo *** ERROR: Temporary script: $tmp_script does not exist   echo   exit   fi    if [[ `cat $tmp_script | wc l` ne ]]; then  # Execute the newly generated StatsPack snapshot purge script   chmod u+x $tmp_script  echo   echo Performing the purge   echo   $tmp_script > $tmp_output  cat $tmp_output # display the output  # Check the output file for a success message:  trap ERR # temporarily reset error handling for the grep mand  grep ^Purge of specified Snapshot range plete $tmp_output >> /dev/null  if [[ $? ne ]]; then  echo   echo *** ERROR: The purge did not plete successfully   echo Check the log file $tmp_output   echo   exit   fi  trap errtrap $LINENO ERR # re establish desired error handler  else  # No purge script was created   echo No snapshot purge was necessary > $tmp_output  fi    echo   echo The $script_name script appears to have pleted   echo successfully on `date`   echo     mailx   s sp_purge ksh in $ORACLE_SID on `uname n` pleted successfully   $   < $tmp_output    # End of script sp_purge ksh cha138/Article/program/Oracle/201311/16809

相关参考

知识大全 学习Oracle--Statspack分析

学习Oracle--Statspack分析  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  ~~~

知识大全 Statspack的安装使用说明

Statspack的安装使用说明  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  在数据库管理中O

知识大全 ORACLE性能诊断―学习statspack笔记(二)

ORACLE性能诊断―学习statspack笔记(二)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧

知识大全 ORACLE性能诊断―学习statspack笔记(一)

ORACLE性能诊断―学习statspack笔记(一)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧

知识大全 ZT-Statspack安装配置使用说明二

ZT-Statspack安装配置使用说明二  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  spc

知识大全 用Oracle中的Statspack诊断数据库性能实例

用Oracle中的Statspack诊断数据库性能实例  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧

知识大全 c#做的计算器,如何在下次输入数字时,textbox里的结果自动清除

c#做的计算器,如何在下次输入数字时,textbox里的结果自动清除你可以增加一个按钮控件,命为清空具体是双击控件,进入后台编写代码即可实现publicvoidButton_click(.....)t

怎样清除厨房里的油烟污染

人们往往为厨房里的油烟污染所困扰,特别是为沾在厨房纱窗上的灰尘和油垢而苦恼。这个问题不解决,对人们的身体健康也构成一定的威胁。如厨房产生的一氧化碳、氮氧化合物、可吸入的颗粒物以及某种化学致癌物质等,由

知识大全 确保正确的清除

  Java不具备象C++的破坏器那样的概念在C++中一旦破坏(清除)一个对象就会自动调用破坏器方法之所以将其省略大概是由于在Java中只需简单地忘记对象不需强行破坏它们垃圾收集器会在必要的时候自动回

知识大全 垃圾自动收集系统指导 (2)

  停止和复制  标记和清除法的兄弟就是停止和复制收集法了停止和复制法解决了标记和清除法的碎片问题但是对内存提出了更高的要求(或者是对一个较小的对象池进行更加频繁的收集)微软的Java虚拟机使用的就是