国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

自動清除statspack所產生的snapshot舊記錄

2024-07-21 02:33:57
字體:
來源:轉載
供稿:網友

  下面的 script可以利用cron排程來自動執行清除超過保留數目的舊有snapshot資料,這個script不需要知道PERFSTAT此帳號的密碼就可執行,并已經經由Oracle8.1.7和9.2.0上測試過。
  
  步驟:
  
  1)儲存這個script取名為sp_purge.ksh在Unix主機上。
  
  2)注重你的系統上tmp目錄是否存在,假如你不想所有產生的log寫到/tmp去,你必須去更改script。
  
  3)假如你的oratab這個目錄位置不是在/var/opt/oracle,你就必須手動去更新script來配合你的環境。
  
  4)設定可執行權限給script: chmod u+x sp_purge.ksh
  
  5)設定cron job來執行這個script。執行這個script需要三個參數:
  
  要清除 snapshot的資料庫名稱。
  
  要保留的 snapshot數量。
  
  執行后要寄發電子郵件的對象。
  
  00 19 * * 1-5 /scripts/sp_purge.ksh prod 60 mrogers@company.com >>/tmp/sp_purge_portal.log 2>&1 &
  
  這個范例是說:星期一到星期五天天晚上七點執行此 script,針對 'PRod' 這個資料庫只保留最近的60個snapshots紀錄,多余的則清除,并且寄發訊息給 mrogers@company.com 。
  
  6)注重這個 script應該配合指定的instance一起執行,假如這臺主機上并沒有這個script所指定的instance在執行中,一個簡單的訊息可在tmp目錄下找到。
  
  *** ERROR: The ORACLE_SID specified in parameter 1 is not a valid SID.
  
  (Note that the SID is case sensitive.)
  
  7)所有產生的執行紀錄都可以在/tmp下找到。
  
  #!/bin/ksh
  # Script Name: sp_purge.ksh
  # This script is designed to purge StatsPack snapshots.
  #
  # Parameter $1 is the name of the database.
  # Parameter $2 is the maximum number of snapshots to retain.
  # Parameter $3 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 30 username@mycompany.com
  # Script History:
  #
  # Who Date Action
  # --------------- ------------ --------------------------------------------
  # Mark J. Rogers 22-Sep-2003 Script creation.
  #
  #
  #
  tmp_dir=/tmp
  # Validate the parameters.
  
  if [[ $# -ne 3 ]]; then
  echo ""
  echo "*** ERROR: You must specify these parameters: "
  echo ""
  echo " 1: the name of the database"
  echo " 2: the maximum # of snapshots to retain"
  echo " 3: the mail recipient for success messages"
  echo ""
  exit 1
  fi
  
  grep "^${1}:" /var/opt/oracle/oratab >> /dev/null
  if [[ $? -ne 0 ]]; then
  echo ""
  echo "*** ERROR: The ORACLE_SID specified in parameter 1 is not a valid SID."
  echo " (Note that the SID is case sensitive.)"
  echo ""
  exit 1
  fi
  
  if [[ ! (${2} -ge 0) ]]; then
  echo ""
  echo "*** ERROR: Parameter 2 must specify the # of snapshots to retain."
  echo ""
  exit 1
  fi
  
  # Ensure that the instance is running on the current machine.
  ps -ef grep pmon grep $1 >> /dev/null
  if [[ $? -ne 0 ]]; then
  echo ""
  echo "*** ERROR: Instance $1 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 1
  fi
  
  # Establish error handling for this UNIX script.
  function errtrap {
  the_status=$?
  echo ""
  echo " *** ERROR: Error message $the_status occured on line number $1."
  echo ""
  echo " *** The script is aborting."
  echo ""
  exit $the_status
  }
  
  trap
  '
  errtrap $LINENO
  '
  ERR
  
  # Set up the Oracle environment.
  
  eXPort ORACLE_SID=${1}
  export ORAENV_ASK=NO
  . oraenv
  
  script_name=${0##*/}
  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 $2 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 := ${2};
  :P_LOSNAPID := -1;
  :P_HISNAPID := -1;
  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 := 0; -- 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 + 1;
  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 60
  spool $tmp_script
  begin
  IF (:P_LOSNAPID <> -1) 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_EOF2');
  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('commit;');
  dbms_output.put_line('exit');
  dbms_output.put_line('SP_EOF2');
  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 1
  fi
  
  if [[ `cat ${tmp_script} wc -l` -ne 0 ]]; 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 command
  grep "^Purge of specified Snapshot range complete." $tmp_output >> /dev/null
  if [[ $? -ne 0 ]]; then
  echo ""
  echo "*** ERROR: The purge did not complete successfully."
  echo " Check the log file $tmp_output."
  echo ""
  exit 1
  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 completed "
  echo " successfully on `date`."
  echo ""
  
  mailx
  -s "sp_purge.ksh in $ORACLE_SID on `uname -n` completed successfully"
  ${3}
  < $tmp_output
  
  # End of script sp_purge.ksh.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 景泰县| 河源市| 襄城县| 平泉县| 江西省| 台中县| 广安市| 呼和浩特市| 龙海市| 宜章县| 通城县| 吴忠市| 瑞安市| 台东市| 涪陵区| 瓦房店市| 斗六市| 巴彦淖尔市| 思茅市| 铜鼓县| 东光县| 奉化市| 民县| 昌图县| 疏勒县| 滨海县| 夏津县| 岱山县| 肥城市| 绵竹市| 凤冈县| 临湘市| 平利县| 洞口县| 牡丹江市| 莲花县| 杭州市| 衢州市| 宁海县| 无为县| 崇仁县|