分享两个Shell脚本,一键统计数据库临时表空间和阻塞lock信息

运维 数据库运维
今天主要分享一下两个shell脚本,主要是为了查看数据库的临时表空间和阻塞lock信息,下面一起来看看吧。

今天主要分享一下两个shell脚本,主要是为了查看数据库的临时表空间和阻塞lock信息,下面一起来看看吧~

数据库连接脚本

use script settdb.sh for DB login details registry

  1. #!/bin/bash 
  2. tmp_username=$SH_USERNAME 
  3. tmp_password=$SH_PASSWORD 
  4. tmp_db_sid=$SH_DB_SID 
  5.  
  6. #check $1 and $2 should be mandatory from input 
  7. if [[ -z $1 ]] || [[ -z $2 ]]; then 
  8. echo '***********************************************' 
  9. echo 'WARNING :UserName And PassWord Is Needed!' 
  10. echo '***********************************************' 
  11.  
  12. exit 
  13. fi 
  14. if [[ -z $3 ]] && [[ -z $ORACLE_SID ]];then 
  15. echo '***********************************************' 
  16. echo 'WARNING :There is Instance can be used !' 
  17. echo '***********************************************' 
  18. exit 
  19. fi 
  20.  
  21. SH_USERNAME=`echo "$1"|tr '[a-z]' '[A-Z]'` 
  22. SH_PASSWORD=$2 
  23. echo '***********************************************' 
  24.  
  25. if [[ -z $3 ]] 
  26. then 
  27.  SH_DB_SID=$ORACLE_SID 
  28.  echo 'Using Default Instance :'$ORACLE_SID 
  29.  echo . 
  30. else 
  31.  SH_DB_SID=`echo "$3"|tr '[a-z]' '[A-Z]'` 
  32. fi 
  33.  
  34. if [[ $SH_DB_SID = $tmp_db_sid ]] && [[ $SH_USERNAME = $tmp_username ]] && [[ $SH_PASSWORD = $tmp_password ]];then 
  35.  echo 'Instance '$SH_DB_SID 'has been connected' 
  36.  echo '***********************************************' 
  37.  exit 
  38. fi 
  39.  
  40. export SH_USERNAME=$SH_USERNAME 
  41. export SH_DB_SID=$SH_DB_SID 
  42. export SH_PASSWORD=$SH_PASSWORD 
  43. export DB_CONN_STR=$SH_USERNAME/$SH_PASSWORD 
  44. #echo $DB_CONN_STR 
  45. listfile=`pwd`/listdb 
  46. Num=`echo show user | $ORACLE_HOME/bin/sqlplus -s $DB_CONN_STR@$SH_DB_SID| grep -i 'USER ' | wc -l` 
  47. if [ $Num -gt 0 ] 
  48.  then 
  49.  ## ok - instance is up 
  50.  echo 'Instance '$SH_DB_SID 'has been connected' 
  51.  echo -e '--' `date`'-- \n--'$SH_USERNAME@$SH_DB_SID 'has been connected --\n' >> listdb 
  52.  echo '***********************************************' 
  53.  echo 'Initalize DB login details registry OK!' 
  54.  echo 'Now you can Execution script~' 
  55.  echo '***********************************************' 
  56.  $SHELL 
  57.   
  58.  else 
  59.  ## inst is inaccessible  
  60.  echo Instance: $SH_DB_SID Is Invalid Or UserName/PassWord Is Wrong  
  61.  echo '***********************************************' 
  62.  exit 
  63.  fi 
  64. del_length=3 
  65. tmp_txt=$(sed -n '$=' listdb)  
  66. echo '***********************************************' 
  67. echo '********* ' $SH_USERNAME'@'$SH_DB_SID '**********' 
  68. echo '***********************************************' 
  69. curr_len=`cat $listfile|wc -l` 
  70. if [ $curr_len -gt $del_length ]; then 
  71. echo ' There Are Below Sessions Still Alive ' 
  72. echo '***********************************************' 
  73. fi 
  74. sed $((${tmp_txt}-${del_length}+1)),${tmp_txt}d $listfile | tee tmp_listfile 
  75. mv tmp_listfile $listfile 

输出:./settdb.sh 用户名 用户密码

分享两个shell脚本,一键统计数据库临时表空间和阻塞lock信息

showtsps.sh

  1. #!/bin/bash 
  2. echo "==================================================查看数据库临时表空间=================================================================" 
  3. sqlplus -s $DB_CONN_STR@$SH_DB_SID <<EOF 
  4. set echo off heading on underline on; 
  5. column inst_num heading "Inst Num" new_value inst_num format 99999; 
  6. column inst_name heading "Instance" new_value inst_name format a12; 
  7. column db_name heading "DB Name" new_value db_name format a12; 
  8. column dbid heading "DB Id" new_value dbid format 9999999999 just c; 
  9.  
  10. prompt 
  11. prompt Current Instance 
  12. prompt ~~~~~~~~~~~~~~~~ 
  13.  
  14. select d.dbid dbid 
  15.  , d.name db_name 
  16.  , i.instance_number inst_num 
  17.  , i.instance_name inst_name 
  18.  from v\$database d, 
  19.  v\$instance i; 
  20.   
  21. set term on feedback off lines 130 pagesize 999 tab off trims on 
  22. column MB format 999,999,999 heading "Total MB" 
  23. column free format 9,999,999 heading "Free MB" 
  24. column used format 99,999,999 heading "Used MB" 
  25. column Largest format 999,999 heading "LrgstMB" 
  26. column tablespace_name format a20 heading "Tablespace" 
  27. column status format a3 truncated 
  28. column max_extents format 99999999999 heading "MaxExt" 
  29. col extent_management for a1 trunc head "M" 
  30. col allocation_type for a1 trunc head "A" 
  31. col Ext_Size for a4 trunc head "Init" 
  32. column pfree format a3 trunc heading "%Fr" 
  33.  
  34. break on report 
  35. compute sum of MB on report 
  36. compute sum of free on report 
  37. compute sum of used on report 
  38.  
  39. select  
  40.  d.tablespace_name,  
  41.  decode(d.status,  
  42.  'ONLINE', 'OLN', 
  43.  'READ ONLY', 'R/O', 
  44.  d.status) status, 
  45.  d.extent_management,  
  46.  decode(d.allocation_type, 
  47.  'USER','', 
  48.  d.allocation_type) allocation_type, 
  49.  (case  
  50.  when initial_extent < 1048576  
  51.  then lpad(round(initial_extent/1024,0),3)||'K'  
  52.  else lpad(round(initial_extent/1024/1024,0),3)||'M'  
  53.  end) Ext_Size, 
  54.  NVL (a.bytes / 1024 / 1024, 0) MB, 
  55.  NVL (f.bytes / 1024 / 1024, 0) free,  
  56.  (NVL (a.bytes / 1024 / 1024, 0) - NVL (f.bytes / 1024 / 1024, 0)) used, 
  57.  NVL (l.large / 1024 / 1024, 0) largest,  
  58.  d.MAX_EXTENTS , 
  59.  lpad(round((f.bytes/a.bytes)*100,0),3) pfree, 
  60.  (case when round(f.bytes/a.bytes*100,0) >= 20 then ' ' else '*' end) alrt 
  61. FROM sys.dba_tablespaces d, 
  62.  (SELECT tablespace_name, SUM(bytes) bytes 
  63.  FROM dba_data_files 
  64.  GROUP BY tablespace_name) a, 
  65.  (SELECT tablespace_name, SUM(bytes) bytes 
  66.  FROM dba_free_space 
  67.  GROUP BY tablespace_name) f, 
  68.  (SELECT tablespace_name, MAX(bytes) large 
  69.  FROM dba_free_space 
  70.  GROUP BY tablespace_name) l 
  71. WHERE d.tablespace_name = a.tablespace_name(+) 
  72.  AND d.tablespace_name = f.tablespace_name(+) 
  73.  AND d.tablespace_name = l.tablespace_name(+) 
  74.  AND NOT (d.extent_management LIKE 'LOCAL' AND d.contents LIKE 'TEMPORARY') 
  75. UNION ALL 
  76. select  
  77.  d.tablespace_name,  
  78.  decode(d.status,  
  79.  'ONLINE', 'OLN', 
  80.  'READ ONLY', 'R/O', 
  81.  d.status) status, 
  82.  d.extent_management,  
  83.  decode(d.allocation_type, 
  84.  'UNIFORM','U', 
  85.  'SYSTEM','A', 
  86.  'USER','', 
  87.  d.allocation_type) allocation_type, 
  88.  (case  
  89.  when initial_extent < 1048576  
  90.  then lpad(round(initial_extent/1024,0),3)||'K'  
  91.  else lpad(round(initial_extent/1024/1024,0),3)||'M'  
  92.  end) Ext_Size, 
  93.  NVL (a.bytes / 1024 / 1024, 0) MB, 
  94.  (NVL (a.bytes / 1024 / 1024, 0) - NVL (t.bytes / 1024 / 1024, 0)) free, 
  95.  NVL (t.bytes / 1024 / 1024, 0) used,  
  96.  NVL (l.large / 1024 / 1024, 0) largest,  
  97.  d.MAX_EXTENTS , 
  98.  lpad(round(nvl(((a.bytes-t.bytes)/NVL(a.bytes,0))*100,100),0),3) pfree, 
  99.  (case when nvl(round(((a.bytes-t.bytes)/NVL(a.bytes,0))*100,0),100) >= 20 then ' ' else '*' end) alrt 
  100. FROM sys.dba_tablespaces d, 
  101.  (SELECT tablespace_name, SUM(bytes) bytes 
  102.  FROM dba_temp_files 
  103.  GROUP BY tablespace_name order by tablespace_name) a, 
  104.  (SELECT tablespace_name, SUM(bytes_used ) bytes 
  105.  FROM v\$temp_extent_pool 
  106.  GROUP BY tablespace_name) t, 
  107.  (SELECT tablespace_name, MAX(bytes_cached) large 
  108.  FROM v\$temp_extent_pool 
  109.  GROUP BY tablespace_name order by tablespace_name) l 
  110. WHERE d.tablespace_name = a.tablespace_name(+) 
  111.  AND d.tablespace_name = t.tablespace_name(+) 
  112.  AND d.tablespace_name = l.tablespace_name(+) 
  113.  AND d.extent_management LIKE 'LOCAL' 
  114.  AND d.contents LIKE 'TEMPORARY' 
  115.  ORDER by 1 
  116. prompt 
  117. exit 
  118. EOF 

输出:./showtsps.sh

分享两个shell脚本,一键统计数据库临时表空间和阻塞lock信息

showlock.sh

这里主要是查看阻塞lock信息,脚本内容如下:

  1. #!/bin/bash 
  2. sqlplus -S $DB_CONN_STR@$SH_DB_SID <<EOF  
  3. set pages 500 
  4. set feedback off 
  5. set verify off 
  6.  
  7. set echo on 
  8. set linesize 1000 
  9. col object_name format a25 
  10. col osuser format a10 
  11. col machine format a12 
  12. col program format a20 
  13. --col object_type format a10 
  14. col state format a10 
  15. col status format a10 
  16. col oracle_username format a12 
  17. col sid_serial format a12 
  18. col sec_wait format 99999999 
  19. col lock_type format a5 
  20. col mode_held format a10 
  21.  
  22. prompt Current Locks 
  23. prompt ------------------------------------------------------------------------------------------------------ 
  24.  
  25. select ses.sid||','||ses.serial# sid_serial,loc.oracle_username,object_name, 
  26. --object_type, 
  27. ses.LOGON_TIME,ses.SECONDS_IN_WAIT sec_wait,ses.osuser,ses.machine,ses.program,ses.state,ses.status, 
  28.  decode(d.type, 
  29.  'MR', 'Media Recovery', 
  30.  'RT', 'Redo Thread', 
  31.  'UN', 'User Name', 
  32.  'TX', 'Transaction', 
  33.  'TM', 'DML', 
  34.  'UL', 'PL/SQL User Lock', 
  35.  'DX', 'Distrib Xaction', 
  36.  'CF', 'Control File', 
  37.  'IS', 'Instance State', 
  38.  'FS', 'File Set', 
  39.  'IR', 'Instance Recovery', 
  40.  'ST', 'Disk Space Transaction', 
  41.  'TS', 'Temp Segment', 
  42.  'IV', 'Library Cache Invalidation', 
  43.  'LS', 'Log Start or Switch', 
  44.  'RW', 'Row Wait', 
  45.  'SQ', 'Sequence Number', 
  46.  'TE', 'Extend Table', 
  47.  'TT', 'Temp Table', 
  48.  d.type) lock_type, 
  49.  decode(d.lmode, 
  50.  0, 'None', /* Mon Lock equivalent */ 
  51.  1, 'Null', /* N */ 
  52.  2, 'Row-S (SS)', /* L */ 
  53.  3, 'Row-X (SX)', /* R */ 
  54.  4, 'Share', /* S */ 
  55.  5, 'S/Row-X (SSX)', /* C */ 
  56.  6, 'Exclusive', /* X */ 
  57.  to_char(d.lmode)) mode_held 
  58.  from v\$locked_object loc,v\$session ses,dba_objects obj,v\$lock d 
  59.  where loc.object_id=obj.object_id  
  60.  and loc.session_id=ses.sid 
  61.  and obj.object_id=d.id1 
  62.  and ses.sid=d.sid 
  63.  order by oracle_username,seconds_in_wait desc 
  64. set head off  
  65. SELECT 'There are also '||count(*)||' transaction locks' 
  66. FROM v\$transaction_enqueue; 
  67. prompt ------------------------------------------------------------------------------------------------------ 
  68.  
  69. set head on 
  70. set linesize 1000 pagesize 1000 
  71. col 进程SID for 99999 trunc  
  72. col 锁类型 format a10 
  73. col SQL语句 format a60 
  74. col 等待事件 format a20 
  75. col 锁时间 format a20 
  76. col 锁角色 format a15 
  77. col 阻塞会话SID format a30 
  78.  
  79.  
  80. prompt 
  81. prompt Blocking Session Details 
  82. prompt ------------------------------------------------------------------------------------------------------ 
  83.  
  84. SELECT mm.inst_id "实例ID",  
  85.  mm.sid "进程SID",  
  86.  mm.TYPE "锁类型",  
  87.  mm.id1 "事务号ID1",  
  88.  mm.id2 "事务号ID2",  
  89.  LPAD(TRUNC(mm.ctime / 60 / 60), 3) || ' Hour ' || LPAD(TO_CHAR(TRUNC(mm.ctime / 60) - TRUNC(mm.ctime / 60 / 60) * 60, 'fm09'), 2) || ' Min ' || LPAD(TO_CHAR(mm.ctime - TRUNC(mm.ctime / 60) * 60, 'fm09'), 2) || ' Sec' 
  90.  "锁时间", CASE WHEN mm.block = 1  
  91.  AND mm.lmode != 0 THEN 'holder'  
  92.  WHEN mm.block = 0  
  93.  AND mm.request != 0 THEN 'waiter'  
  94.  ELSE NULL END "锁角色",  
  95.  CASE WHEN ee.blocking_session IS NOT NULL THEN 'waiting for SID ' || ee.blocking_session  
  96.  ELSE NULL END "阻塞会话SID",  
  97.  dd.sql_text "SQL语句",  
  98.  cc.event "等待事件" 
  99.  FROM gv\$lock mm,  
  100.  gv\$session ee,  
  101.  gv\$sqlarea dd,  
  102.  gv\$session_wait cc  
  103.  WHERE mm.sid IN (SELECT nn.sid  
  104.  FROM (SELECT tt.*,  
  105.  COUNT(1) OVER (PARTITION BY tt.TYPE,  
  106.  tt.id1,  
  107.  tt.id2) cnt, MAX(tt.lmode) OVER (PARTITION BY tt.TYPE,  
  108.  tt.id1,  
  109.  tt.id2) lmod_flag, MAX(tt.request) OVER (PARTITION BY tt.TYPE,  
  110.  tt.id1,  
  111.  tt.id2) request_flag  
  112.  FROM gv\$lock tt) nn  
  113.  WHERE nn.cnt > 1  
  114.  AND nn.lmod_flag != 0  
  115.  AND nn.request_flag != 0)  
  116.  AND mm.sid = ee.sid (+)  
  117.  AND ee.sql_id = dd.sql_id (+)  
  118.  AND mm.sid = cc.sid (+)  
  119.  AND ((mm.block = 1  
  120.  AND mm.lmode != 0)  
  121.  OR (mm.block = 0  
  122.  AND mm.request != 0))  
  123.  ORDER BY mm.TYPE, mm.id1, mm.id2, mm.lmode DESC,  
  124.  mm.ctime DESC; 
  125.  
  126. exit 
  127. EOF 
[[283107]]

输出:./showlock.sh

分享两个shell脚本,一键统计数据库临时表空间和阻塞lock信息

责任编辑:赵宁宁 来源: 今日头条
相关推荐

2023-12-01 15:50:46

2019-08-26 10:15:29

脚本索引数据库

2010-04-21 11:27:55

Oracle数据库

2019-10-31 08:22:39

shell脚本Linux

2010-05-04 16:50:04

Oracle数据库

2019-07-31 08:03:45

Oracle数据库巡检脚本

2020-04-24 09:01:23

网络安全数据泄露黑客

2022-10-25 09:11:47

物联网IoT工业物联网

2019-06-27 05:00:26

物联网统计数据IOT

2018-10-22 06:27:32

网络犯罪数据泄露攻击

2022-01-26 07:18:57

oracle临时表空间数据库

2009-07-02 00:26:00

临时表空间Oracle数据库性能

2021-08-17 06:48:43

SpringbootKafkaStream

2011-10-09 10:33:12

2024-03-06 08:03:09

2019-07-22 05:01:38

物联网IOT技术

2020-11-06 22:48:01

物联网数据技术

2021-02-18 16:10:03

物联网工业4.0人工智能

2015-07-29 11:21:13

JavaScript统计数据

2023-07-18 10:43:14

物联网IOT
点赞
收藏

51CTO技术栈公众号