網站首頁 編程語言 正文
背景
客戶最近有這樣的需求,想通過統計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 統計腳本工作完成,文章中只羅列了部分代碼實現邏輯。
原文鏈接:https://www.cnblogs.com/bicewow/p/13846259.html
相關推薦
- 2021-12-13 基于C#實現端口掃描器(單線程和多線程)_C#教程
- 2022-05-22 docker部署訪問postgres數據庫的實現方法_docker
- 2022-03-22 .NET?6開發TodoList應用之實現查詢排序_實用技巧
- 2022-08-19 WPF使用DrawingContext實現二維繪圖_C#教程
- 2023-01-04 Opencv實現鼠標事件與窗口互動功能過程_python
- 2022-04-01 安裝k8s Error initializing network controller: Error
- 2022-10-30 使用AVFoundation實現視頻錄制詳解_IOS
- 2023-01-08 Flutter開發技巧ListView去除水波紋方法示例_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支