日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Oracle?Session每日統(tǒng)計功能實現(xiàn)_oracle

作者:空白葛 ? 更新時間: 2022-04-12 編程語言

背景

客戶最近有這樣的需求,想通過統(tǒng)計Oracle數(shù)據(jù)庫活躍會話數(shù),并記錄在案,利用比對歷史的活躍會話的方式,實現(xiàn)對系統(tǒng)整體用戶并發(fā)量有大概的預(yù)估。

功能分析

客戶現(xiàn)場有不少Oracle數(shù)據(jù)庫,如果每一套都進(jìn)行查詢,效率太慢,而且數(shù)據(jù)也不能保留,所以需要通過腳本批量查詢的方式實現(xiàn)。具體功能要點如下:

  • 編寫統(tǒng)計會話的SQL腳本
  • 通過shell 批量管理眾多Oracle實例,for實現(xiàn)
  • 輸出每次查詢的記錄留存,根據(jù)實例分別保存
  • 在有一次初始數(shù)據(jù)之后,以后的每天查出增量數(shù)據(jù)

眾多Oracle實例統(tǒng)計完成之后,統(tǒng)一輸出到一個文件便于查看

具體實現(xiàn)

統(tǒng)計會話SQL腳本

根據(jù)客戶要求,統(tǒng)計活躍會話數(shù),一天運行一次即可,沒必要實時數(shù)據(jù),數(shù)據(jù)可以從數(shù)據(jù)庫V$SESSION,V$ACTIVE_SESSION_HISTORY,DBA_HIST_ACTIVE_SESS_HISTORY三個視圖中獲取,V$SESSION視圖為當(dāng)前實時活躍會話信息(內(nèi)存中保存,實時信息),V$ACTIVE_SESSION_HISTORY視圖為1s從V$SESSION獲取的活躍會話信息(內(nèi)存中保存,保存時長依賴內(nèi)存ash_buffers參數(shù)),DBA_HIST_ACTIVE_SESS_HISTORY視圖每10s從V$ACTIVE_SESSION_HISTORY視圖中獲取一份(持久化保存,一般為7天),經(jīng)過以上分析,SQL所需數(shù)據(jù)理應(yīng)通過DBA_HIST_ACTIVE_SESS_HISTORY視圖獲取。
以下是具體的SQL查詢語句,參考redo log切換統(tǒng)計語句格式。

prompt
prompt Session cnt
prompt ~~~~~~~~~~~~~~~~
set linesize 200
set pages 2000
col 00 for 9999
col 01 for 9999
col 02 for 9999
col 03 for 9999
col 04 for 9999
col 05 for 9999
col 06 for 9999
col 07 for 9999
col 08 for 9999
col 09 for 9999
col 10 for 9999
col 11 for 9999
col 12 for 9999
col 13 for 9999
col 14 for 9999
col 15 for 9999
col 16 for 9999
col 17 for 9999
col 18 for 9999
col 19 for 9999
col 20 for 9999
col 21 for 9999
col 22 for 9999
col 23 for 9999
select INST_ID,
       a.ttime,
       sum(c0) "00",
       sum(c1) "01",
       sum(c2) "02",
       sum(c3) "03",
       sum(c4) "04",
       sum(c5) "05",
       sum(c6) "06",
       sum(c7) "07",
       sum(c8) "08",
       sum(c9) "09",
       sum(c10) "10",
       sum(c11) "11",
       sum(c12) "12",
       sum(c13) "13",
       sum(c14) "14",
       sum(c15) "15",
       sum(c16) "16",
       sum(c17) "17",
       sum(c18) "18",
       sum(c19) "19",
       sum(c20) "20",
       sum(c21) "21",
       sum(c22) "22",
       sum(c23) "23"
  from (select INST_ID,
               ttime,
               decode(tthour, '00', c_cnt, 0) c0,
               decode(tthour, '01', c_cnt, 0) c1,
               decode(tthour, '02', c_cnt, 0) c2,
               decode(tthour, '03', c_cnt, 0) c3,
               decode(tthour, '04', c_cnt, 0) c4,
               decode(tthour, '05', c_cnt, 0) c5,
               decode(tthour, '06', c_cnt, 0) c6,
               decode(tthour, '07', c_cnt, 0) c7,
               decode(tthour, '08', c_cnt, 0) c8,
               decode(tthour, '09', c_cnt, 0) c9,
               decode(tthour, '10', c_cnt, 0) c10,
               decode(tthour, '11', c_cnt, 0) c11,
               decode(tthour, '12', c_cnt, 0) c12,
               decode(tthour, '13', c_cnt, 0) c13,
               decode(tthour, '14', c_cnt, 0) c14,
               decode(tthour, '15', c_cnt, 0) c15,
               decode(tthour, '16', c_cnt, 0) c16,
               decode(tthour, '17', c_cnt, 0) c17,
               decode(tthour, '18', c_cnt, 0) c18,
               decode(tthour, '19', c_cnt, 0) c19,
               decode(tthour, '20', c_cnt, 0) c20,
               decode(tthour, '21', c_cnt, 0) c21,
               decode(tthour, '22', c_cnt, 0) c22,
               decode(tthour, '23', c_cnt, 0) c23
          from (select instance_number INST_ID,
                       to_char(sample_time, 'YYYY-MM-DD') ttime,
                       to_char(sample_time, 'HH24') tthour,
                       count(1) c_cnt
                  from dba_hist_active_sess_history
                 where sample_time >= trunc(sysdate) - 7
                   and sample_time < trunc(sysdate)  - 1
                 group by instance_number,
                          to_char(sample_time, 'YYYY-MM-DD'),
                          to_char(sample_time, 'HH24'))) a
 group by INST_ID, ttime
 order by ttime desc;

語句執(zhí)行完成之后,效果如下

批量Oracle實例 for實現(xiàn)

根據(jù)客戶現(xiàn)場環(huán)境,有多臺服務(wù)器,每套服務(wù)器上部署多個實例,為了避免重復(fù)的代碼,此處大概需要2個嵌套for循環(huán),外層負(fù)責(zé)服務(wù)器,內(nèi)存負(fù)責(zé)實例,外層實現(xiàn)邏輯如下:

function Startmain
{
 #machines=(Xx)
 machines=(Xx Xx Xx xX Xx)
 v_flag=$1
 for i in ${!machines[*]}
 do
  local v_machine="${machines[$i]}"
  if [[ $v_machine == "Xx" ]];then
    v_args=("XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521")
    v_ip="xxx.xxx.xxx.xxx"
  elif [[ $v_machine == "Xx" ]];then
    v_args=("XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521")
    v_ip="xxx.xxx.xxx.xxx"
  elif [[ $v_machine == "Xx" ]];then
    v_args=("XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521")
    v_ip="xxx.xxx.xxx.xxx"
  elif [[ $v_machine == "xx" ]];then
    v_args=("XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "XXX,1521" "xxxx,1527")
    v_ip="xxx.xxx.xxx.xxx"
  elif [[ $v_machine == "Xx" ]];then
    v_args=("XXX,1521" "XXX,1521" "xxxx,1526")
    v_ip="xxx.xxx.xxx.xxx"
  else
    echo "Error"
  fi
  echo "$v_machine------------------------------------"|tee -a $newfilename
  GetSesscnt  $v_machine "${v_args[*]}" $v_ip $v_flag
 done
}

外層for通過調(diào)用GetSesscnt函數(shù),實現(xiàn)對服務(wù)器內(nèi)部實例的解析,(注:在增量查詢之后調(diào)用了日志轉(zhuǎn)存函數(shù),把數(shù)據(jù)統(tǒng)一輸出到一個文件)內(nèi)層實現(xiàn)邏輯如下:

function GetSesscnt
{
 if [[ $# -ne 4 ]];then
   echo "GetSesscnt XX (XXX XXX XXX XXX XXX) xxx.xxx.xxx.xxx flag"
 fi
 machine=$1
 args=($2)
 s_ip=$3
 v_flag=$4
 for i in ${!args[*]} 
 do
 local v_arg="${args[$i]}"
 v_name=${v_arg%,*}
 v_port=${v_arg#*,}
 recho "$machine $v_name start.." |tee -a $newfilename
 if [[ $v_flag == "incr" ]];then
 $ORACLE_HOME/bin/sqlplus -S "system/password@$s_ip:$v_port/$v_name" <<EOF > $pathpwd/$1_$v_name
@$pathpwd/sessioncntincr.sql
exit;
EOF
 LogConvert $pathpwd $1_$v_name
 else
 $ORACLE_HOME/bin/sqlplus -S "system/password@$s_ip:$v_port/$v_name" <<EOF > $pathpwd/$1_$v_name".log"
@$pathpwd/sessioncntinit.sql
exit;
EOF
 fi
 recho "$machine $v_name end"|tee -a $newfilename
 done
}

上述shell腳本中,同時包含了對不同服務(wù)器不同實例的結(jié)果留存,根據(jù)傳輸?shù)氖窃隽窟€是全量參數(shù),實現(xiàn)對不同數(shù)據(jù)的生成保存,全量數(shù)據(jù)為.log后綴,增量數(shù)據(jù)沒有l(wèi)og后綴保存記錄如下

全量數(shù)據(jù)格式如下:

增量數(shù)據(jù)格式如下:

數(shù)據(jù)統(tǒng)一匯總

通過以上2步基本把所需的數(shù)據(jù)全部查詢出來,有全量數(shù)據(jù)之后,每天跑增量即可,剩下工作就把每天跑的增量數(shù)據(jù)結(jié)合全量數(shù)據(jù)匯總和,統(tǒng)一輸出到一個文件中,實現(xiàn)代碼如下:

function LogConvert
{
 v_log_path=$1
 v_log_name=$2
 grep -v '^$' $v_log_path/$v_log_name > $v_log_path/log_temp
 v_date=(`cat $v_log_path/log_temp | awk '{print $2}'`)
 v_cnt=(`grep $v_date $v_log_path/$v_log_name".log"|wc -l`)
 if [[ $v_cnt == 0 ]]; then
  sed -i "10 r $v_log_path/log_temp" $v_log_path/$v_log_name".log"
 fi 
 head -40 $v_log_path/$v_log_name".log" >> $newfilename
}

至此,session 統(tǒng)計腳本工作完成,文章中只羅列了部分代碼實現(xiàn)邏輯。

原文鏈接:https://www.cnblogs.com/bicewow/p/13846259.html

欄目分類
最近更新