首頁 > 軟體

Oracle Session每日統計功能實現

2022-02-08 13:02:02

背景

客戶最近有這樣的需求,想通過統計Oracle資料庫活躍對談數,並記錄在案,利用比對歷史的活躍對談的方式,實現對系統整體使用者並行量有大概的預估。

功能分析

客戶現場有不少Oracle資料庫,如果每一套都進行查詢,效率太慢,而且資料也不能保留,所以需要通過指令碼批次查詢的方式實現。具體功能要點如下:

  • 編寫統計對談的SQL指令碼
  • 通過shell 批次管理眾多Oracle範例,for實現
  • 輸出每次查詢的記錄留存,根據範例分別儲存
  • 在有一次初始資料之後,以後的每天查出增量資料

眾多Oracle範例統計完成之後,統一輸出到一個檔案便於檢視

具體實現

統計對談SQL指令碼

根據客戶要求,統計活躍對談數,一天執行一次即可,沒必要實時資料,資料可以從資料庫V$SESSION,V$ACTIVE_SESSION_HISTORY,DBA_HIST_ACTIVE_SESS_HISTORY三個檢視中獲取,V$SESSION檢視為當前實時活躍對談資訊(記憶體中儲存,實時資訊),V$ACTIVE_SESSION_HISTORY檢視為1s從V$SESSION獲取的活躍對談資訊(記憶體中儲存,儲存時長依賴記憶體ash_buffers引數),DBA_HIST_ACTIVE_SESS_HISTORY檢視每10s從V$ACTIVE_SESSION_HISTORY檢視中獲取一份(持久化儲存,一般為7天),經過以上分析,SQL所需資料理應通過DBA_HIST_ACTIVE_SESS_HISTORY檢視獲取。
以下是具體的SQL查詢語句,參考redo log切換統計語句格式。

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;

語句執行完成之後,效果如下

批次Oracle範例 for實現

根據客戶現場環境,有多臺伺服器,每套伺服器上部署多個範例,為了避免重複的程式碼,此處大概需要2個巢狀for迴圈,外層負責伺服器,記憶體負責範例,外層實現邏輯如下:

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通過呼叫GetSesscnt函數,實現對伺服器內部範例的解析,(注:在增量查詢之後呼叫了紀錄檔轉存函數,把資料統一輸出到一個檔案)內層實現邏輯如下:

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指令碼中,同時包含了對不同伺服器不同範例的結果留存,根據傳輸的是增量還是全量引數,實現對不同資料的生成儲存,全量資料為.log字尾,增量資料沒有log字尾儲存記錄如下

全量資料格式如下:

增量資料格式如下:

資料統一彙總

通過以上2步基本把所需的資料全部查詢出來,有全量資料之後,每天跑增量即可,剩下工作就把每天跑的增量資料結合全量資料彙總和,統一輸出到一個檔案中,實現程式碼如下:

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 統計指令碼工作完成,文章中只羅列了部分程式碼實現邏輯。

到此這篇關於Oracle Session每日統計的文章就介紹到這了,更多相關Oracle Session每日統計內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com