Oracle监视表空间,并自动增加数据文件脚本

Sql代码   收藏代码
  1. --- 创建view  
  2.   
  3. --- 百分比  
  4.   
  5. create view tablespace_used_percent as  
  6. select useage from  
  7. (  
  8. select  
  9. a.tablespace_name,  
  10. a.file_name,  
  11. a.total "Total(MB)",  
  12. round(a.total-b.Free_Space) "Used(MB)",  
  13. round(((a.total-b.Free_Space)/a.total)*100,2) as useage,  
  14. a.auto_extend  
  15. from  
  16. (select  
  17. FILE_ID,  
  18. tablespace_name,  
  19. file_name,  
  20. bytes/(1024*1024) Total,  
  21. AUTOEXTENSIBLE auto_extend  
  22. from  
  23. dba_data_files ddf) a,  
  24. (select  
  25. file_id,  
  26. sum(bytes)/(1024*1024) Free_Space  
  27. from  
  28. dba_free_space  
  29. group by file_id) b  
  30. where  
  31. a.file_id=b.file_id  
  32. )  
  33. where tablespace_name = 'YOUR TABLESPACE NAME';  
  34.   
  35.   
  36. --- deails   
  37. create view TABLESPACE_USAGE as  
  38. select  
  39. a.tablespace_name,  
  40. a.file_name,  
  41. a.total "Total(MB)",  
  42. round(a.total-b.Free_Space) "Used(MB)",  
  43. round(((a.total-b.Free_Space)/a.total)*100,2) "Used(%)",  
  44. a.auto_extend  
  45. from  
  46. (select  
  47. FILE_ID,  
  48. tablespace_name,  
  49. file_name,  
  50. bytes/(1024*1024) Total,  
  51. AUTOEXTENSIBLE auto_extend  
  52. from  
  53. dba_data_files ddf) a,  
  54. (select  
  55. file_id,  
  56. sum(bytes)/(1024*1024) Free_Space  
  57. from  
  58. dba_free_space  
  59. group by file_id) b  
  60. where  
  61. a.file_id=b.file_id;  




Linux 脚本 

Java代码   收藏代码
  1. #####################################################################  
  2. ## checkTabsp.sh ##  
  3. ## This Script will add the new datafile if  Tablespace's data  
  4. ## file, which is greater than  the 80% of one datafiles size  
  5. #####################################################################  
  6. #!/bin/bash  
  7. usedPercentNO=(`sqlplus -s '/as sysdba' <<\EOF   
  8. SET heading OFF;   
  9. SET verify OFF;  
  10. SELECT * FROM tablespace_used_percent;  
  11. EOF`  
  12. )  
  13. #get  the length of array  
  14. len=${#usedPercentNO[*]}  
  15.   
  16. echo "The array has $len members."  
  17.   
  18. i=0  
  19.   
  20.  while [ $i -lt $len ]; do  
  21.       echo "$i: ${usedPercentNO[$i]}"  
  22.       arrNo=`echo "${usedPercentNO[$i]}" | awk -F. '{print $1}'`  
  23.       if [ -z $arrNo ]  
  24.       then  
  25.         arrNo=1  
  26.       fi  
  27. if usedPercentNo >= 80 then we add new data file,which will have 8G size   
  28.       if [ $arrNo -gt 80 ]  
  29.       then  
  30.         let sigNo=$i+1  
  31.      sqlplus -s "/ as sysdba" <<EOF  
  32.         ALTER TABLESPACE DB_TABLESPACE ADD DATAFILE '/opt/oracle/oradata/DB/DB_DATA$sigNo.dbf' SIZE 2G  AUTOEXTEND ON MAXSIZE 8G;  
  33. EOF  
  34. # we need send email to report the tablespace stats info  
  35.       sqlplus -s "/as sysdba" <<\EOF   
  36.         col tablespace_name for a30  
  37.         col file_name for a60  
  38.         col auto_extend for a12  
  39.         col tablespace_name justify center  
  40.         col file_name justify center  
  41.         col autoextend justify right  
  42.         set linesize 200  
  43.         set pagesize 500  
  44.         SPOOL tablespace.alert    
  45.         SELECT * FROM TABLESPACE_USAGE;  
  46.         SPOOL OFF;  
  47.         EXIT  
  48. EOF  
  49.       fi  
  50.   let i++  
  51. done  
  52.   
  53. #we needn't send email from there the crontab will do  
  54.   
  55.   if [ `cat tablespace.alert|wc -l` -gt 0 ]  
  56.   then  
  57.     cat tablespace.alert >tablespace.tmp  
  58.   mailx -s "TABLESPACE ALERT for DB"  EMAIL-ADDRESS  < tablespace.alert  
  59.   fi  



上面这个脚本会导致如果有一个文件超过80%的话,脚本会不停添加数据文件.... 

更新修改后的,而且把sql直接用文本文件来代替了以前使用的view 

Java代码   收藏代码
  1. #!/bin/bash  
  2.   
  3. # Managed by Puppet  
  4.   
  5. #####################################################################  
  6. ## checkTabsp.sh ##  
  7. ## This Script will add the new datafile if BOCC Tablespace's data  
  8. ## file, which is greater than  the 80% of one datafiles size  
  9. #####################################################################  
  10.   
  11. # Avoid have the script run if already running  
  12. source /opt/app/inc/some_functions.sh  
  13. pgrpfile=/tmp/checkTabsp.pgrp  
  14. check_if_running  
  15. # end  
  16.   
  17.   
  18. source /home/oracle/.profile  
  19.   
  20. usedDatafileNO=(`sqlplus -s '/as sysdba' <<\EOF   
  21. SET heading OFF;   
  22. SET verify OFF;  
  23. @/opt/app/sql/chktabspused.sql  
  24. EOF`  
  25. )  
  26. # check whether it needs add data file  
  27. if [ $usedDatafileNO -eq 0 ]  
  28. then  
  29. usedDatNO=(`sqlplus -s '/as sysdba' <<\EOF   
  30. SET heading OFF;   
  31. SET verify OFF;  
  32. @/opt/app/sql/chkdatno.sql  
  33. EOF`  
  34. )  
  35.   
  36. let sigNO=$usedDatNO+1  
  37. sigNO=`printf "%03d" $sigNO`  
  38.   
  39. sqlplus -s "/ as sysdba" <<EOF  
  40.        ALTER TABLESPACE DB_TABLESPACE ADD DATAFILE '/opt/oracle/oradata/DB/DB_DATA$sigNO.dbf' SIZE 500M AUTOEXTEND ON NEXT 50M;  
  41. EOF  
  42. # we need send email to report the tablespace stats info to check whether add data file successful  
  43.       sqlplus -s "/as sysdba" <<\EOF   
  44.         col tablespace_name for a30  
  45.         col file_name for a60  
  46.         col auto_extend for a12  
  47.         col tablespace_name justify center  
  48.         col file_name justify center  
  49.         col autoextend justify right  
  50.         set linesize 200  
  51.         set pagesize 500  
  52.         @/opt/bocc/sql/chktabspstats.sql  
  53.         EXIT  
  54. EOF  
  55. # out put the disk space useage  
  56. df -h  
  57. fi  
  58. #we will don't send email from there the crontab will do  
  59. #  if [ `cat tablespace.alert|wc -l` -gt 0 ]  
  60. #  then  
  61. #    cat tablespace.alert >tablespace.tmp  
  62. #  mailx -s "TABLESPACE ALERT for DB" YOUR_EMAIL_ADDRESS t  < tablespace.alert  
  63. #  fi  

猜你喜欢

转载自jayyanzhang2010.iteye.com/blog/1861247
今日推荐