Home Database Mysql Tutorial Oracle数据库管理常用的监控脚本------极大的简化运维工作

Oracle数据库管理常用的监控脚本------极大的简化运维工作

Jun 07, 2016 pm 05:27 PM
oracle database

最近几天都研究SHELL脚本,为了方便对公司的Oracle运维,简化管理,学习一些SHELL脚本是非常有必要的,通过书本和网上的一些资料

最近几天都研究SHELL脚本,为了方便对公司的Oracle运维,简化管理,学习一些SHELL脚本是非常有必要的,通过书本和网上的一些资料,整理出了一些比较精典的脚本,都是经过清自测试可行的,放上来共大家分享。

# 监控Oracle监听状态(chk_lsnr_stat.sh)
# ======================================================================================
# 监控Oracle监听器状态,发现状态异常启动监听,并发送邮件通知管理员,如果启动监听失败,发送邮件
# 通知管理员。
# ======================================================================================
 
#! /bin/bash
 
. /home/oracle/.bash_profile
 
tempfile=$ORACLE_BASE/admin/$ORACLE_SID/tempfile.lis
 
su - oracle -c "lsnrctl status" > /dev/null
 
if [ $? != '0' ]; then
  echo "" >> $tempfile
 
  echo "======================================================" >> $tempfile
 
  echo "`date +%D-%T`" >> $tempfile
 
  su - oracle -c "lsnrctl start" >> $tempfile
 
  if [ $? = '0' ]; then
   
      cat $tempfile | mail dba@163.com -s "The Listener Shutdown,and Restarted Success"
 
  else
 
    cat $tempfile | mail dba@163.com -s "The Listener Shutdown,and Restarted Failed"
 
  fi
 
fi
 
 
 
----------------------------------------------------------------------------------------
 
 
# 监控Oracle实例状态(chk_inst_stat.sh)
# =====================================================================================
# 监控Oracle实例是否打开,实例打开时,数据库是否可用,当实例关闭,数据库不可用时发送告警邮件
# 通知管理员(判断时除开+ASM这个特殊实例)
# =====================================================================================
 
#! /bin/bash
 
ORATAB=/etc/oratab
 
tempfile=/home/oracle/tempfile.lis
 
db=`cat $ORATAB |egrep -i ":Y|:N"|cut -d ":" -f 1|grep -v "^+"`
 
pslist="`ps -ef | grep pmon|grep -v grep`" 
 
mark=n
 
dbstat=`su - oracle  
    sqlplus -s /nolog
 
    conn / as sysdba
 
    set feedback off heading off pagesize 0
 
    select status from v\\$instance;
 
    exit
 
EOF`
 
for db_name in $db; do
 
      echo "$pslist" | grep "ora_pmon_$db_name" > /dev/null 2>&1
       
    if [ $? = "0" ]; then 
       
      if [ $dbstat != "OPEN" ];then
 
          mark=y
         
          break
   
      fi
       
    else
   
      mark=y
       
      break
       
    fi 
   
done
 
if [ $mark != 'n' ];then
 
  echo '>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' >> $tempfile
  echo "SERVER: $HOSTNAME" >> $tempfile
  echo "`date +%D-%T`"  >> $tempfile
  echo 'WARN!!! Oracle Database Unavailable' >> $tempfile
  echo "Maybe The Following Reasons: The Instance or Database is not OPEN" >>$tempfile
   
  echo | mail -s "Oracle Database Abnormal" dba@163.com   rm -f $tempfile
   
fi
 
------------------------------------------------------------------------------------------
 
 
# 监控归档目录空间(chk_arc_space.sh)
#=====================================================================================
# 将日志目录空间控制在200M,当容量大于200M时,将最旧日志打包复制到其它目录,并删除之
# 直到日志目录空间容量小于200M为止。
#=====================================================================================
 
#! /bin/bash
 
ARC_DIR=/disk01/tbs03
 
BAK_DIR=/opt/arcbackup
 
limit=200
 
capacity()
 
{
 
  du -sh $ARC_DIR|awk -F " " '{print $1}'|tr -d M
 
}
 
 
oldlog()
 
{
 
  ls -l|sort -k6|sed '1d'|head -1|awk -F " " '{print $9}'
 
}
 
cd $ARC_DIR
 
if [ `capacity` -gt $limit ]; then
 
    echo ""|mail -s "The Archivelog Directory is Over Than $limit"\
 dba@163.com
 
fi
 
while [ `capacity` -gt $limit ] 
 
do 
   
    file=`oldlog`
   
    tar -czf $file.`date +%Y%m%d%H%M`.tar.gz $file
   
    cp $file.`date +%Y%m%d%H%M`.tar.gz $BAK_DIR/
   
    rm -rf $ARC_DIR/$file $ARC_DIR/$file.`date +%Y%m%d%H%M`.tar.gz
   
done
 
 
 
# 监控警告文件错误信息(chk_alert_info.sh)
#================================================================================
# 固定时间间隔内检查alert_$ORACLE_SID.log文件中是否包含ORA-开头的错误信息,如果存在
# 则将其以邮件的形式通知管理员。
#================================================================================
 
#! /bin/bash
 
. /home/oracle/.bash_profile
 
cd $ORACLE_BASE/admin/$ORACLE_SID/bdump/
 
mv alert_$ORACLE_SID.log alert_temp.log
 
touch alert_$ORACLE_SID.log
 
cat alert_temp.log >> alert.$ORACLE_SID.hist
 
grep ORA- alert_temp.log > alert.err
 
if [ `cat alert.err|wc -l` -gt 0 ];then
 
    mail -s "ORACLE ALERT ERROR" dba@163.com      
fi
 
rm -rf alert.err
 
rm -rf alert_temp.log
 
 
 
# 监控磁盘空间利用率(chk_disk_space)
# ====================================================================
# 判断/dev开头的磁盘或分区空间利用率,当磁盘空间利用率超过90%则发送邮件通知
# 管理员
# ====================================================================
 
#! /bin/bash
 
limit=90%
 
tempfile=chk_disk_space.tmp
 
mark=n
 
diskusage()
 
{
 
  df -h|grep -v Filesystem|sed '/\/dev\/mapper/N;s/\n//'|grep "^/dev"|awk -F " " '{print $5}'
   
}
 
for percent in `diskusage`
 
do
 
  if [[ $percent > $limit]];then
   
      mark=y
       
      break
       
  fi
   
done
 
if [ $mark != 'n' ];then
 
    df -h > $tempfile
   
    mail -s "Disk Usage Over than $limit on `hostname`"  
    rm -rf $tempfile   
fi
 
 
 
 
# 监控表空间空闲表空间(chk_tbs_free.sh)
#==================================================================================
# 监控空闲表空间,当空闲表空间低于20%时,发送邮件通知管理员
#==================================================================================
 
#! /bin/bash
 
su - oracle > /dev/null sqlplus -s /nolog
conn / as sysdba
set feedback off 
set heading off
set verify off
set pagesize 0
set linesize 200
 
spool tbsfree.alert
 
select t.tablespace_name,f.free_space/t.total_space from 
(select tablespace_name,sum(bytes) total_space from 
dba_data_files group by tablespace_name) t,
(select tablespace_name,sum(bytes) free_space from 
dba_free_space group by tablespace_name) f 
where t.tablespace_name=f.tablespace_name and f.free_space/t.total_space /
 
spool off
 
exit
 
EOF
 
if [ `cat tbsfree.alert|wc -l` -gt 0 ];then
 
      cat tbsfree.alert|mail -s "No Free Space in Oracle db" dba@163.com
     
      rm -rf tbsfree.alert
     
fi
 
 
# 全库冷备份(full_cold_backup.sh)
# =================================================================================
# 数据库打开时,自动生成备份脚本。然后关闭数据库,对控制文件,数据文件,重做日志文件,
# 初始化参数文件及口令文件做冷备,完成后打开数据库。
# =================================================================================
 
#! /bin/bash
 
. /home/oracle/.bash_profile
 
backup_dir=/disk01/backup/coldbak
 
log_file=/disk01/backup/coldbak/cold_backup_$ORACLE_SID.log
 
echo 'Begin Cold Backup>>>>>>>>>>>>>>>>'  >> $log_file
 
date >> $log_file
 
su - oracle > /dev/null  
sqlplus -s /nolog
 
conn / as sysdba
 
set feedback off heading off pagesize 0 line 1000
 
spool file_copy_$ORACLE_SID.sh
 
select 'cp ' || name || ' $backup_dir/' from v$controlfile;
 
select 'cp' || file_name || ' $backup_dir/' from dba_data_files;
 
select 'cp' || member || ' $backup_dir/' from v$logfile;
 
spool off
 
shutdown immediate
 
! bash file_copy_$ORACLE_SID.sh
 
startup
 
exit
 
EOF
 
if [ -e $ORACLE_HOME/dbs/init$ORACLE_SID.ora ];then
 
    cp $ORACLE_HOME/dbs/init$ORACLE_SID.ora $backup_dir/
     
fi
 
if [ -e $ORACLE_HOME/dbs/spfile$ORACLE_SID.ora ];then
 
    cp $ORACLE_HOME/dbs/spfile$ORACLE_SID.ora $backup_dir/
     
fi
 
if [ -e $ORACLE_HOME/dbs/orapw$ORACLE_SID ];then
 
    cp $ORACLE_HOME/dbs/orapw$ORACLE_SID $backup_dir/
     
fi
 
 
echo 'Cold Backup Finished>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>' >>$log_file
 
date >> $log_file
 
 
# RMAN备份SHELL脚本(rman_backup.sh)
#===========================================================================
# 实现指定级别的增量备份,由用户传入备份级别参数,,如果不指参数则进行0级备份
#===========================================================================
 
#! /bin/bash
 
. /home/oracle/.bash_profile
 
if [ $1 ];then
 
  backup_level=$1
   
else
 
  backup_level=0
   
fi
 
backup_user=sys
 
backup_user_pw=oracle
 
#catalog_user=rman
 
#catalog_user_pw=rman
 
log_file=/home/oracle/rman_backup.log
 
echo 'Begining rman backup  >>>>>>>>>>>>>>>>>>>>>>>>>>'  >> $log_file
 
date >> $log_file
 
su - oracle >> $log_file  
rman target $backup_user/$backup_user_pw 
    # catalog $catalog_user/$catalog_user_pw
     
backup incremental level = $backup_level database;
 
quit;
 
EOF
 
echo 'rman backup finished  >>>>>>>>>>>>>>>>>>>>>>>>>'  >> $log_file
 
date >> $log_file
 
 
 
# 逻辑备份SHELL脚本(schema_exp.sh)
#=========================================================================
# EXP对数据库schema对象进行备份,用户可以将需要备份的用户名做为参数传入SHELL脚本
#=========================================================================
 
#! /bin/bash
 
BAK_DIR=/disk01/backup/logical_bak/
 
log_file=/disk01/backup/logical_bak/user_full_bak.log
 
exp_par="userid=system/oracle buffer=10485760 owner=$1"
 
if [ $2 ];then
 
    exp_par="$exp_par file=$2"
   
else
 
    exp_par="$exp_par file="$BAK_DIR/$1_`date +%Y%m%d%H%M`.dmp""
   
fi
 
echo "Begining User $1 Export ---------------------" >> $log_file
 
echo "Export with following parameters: $exp_par" >> $log_file
 
date >> $log_file
 
su - oracle -c "exp $exp_par" >> $log_file 2>&1
 
echo "Backup Finished ---------------------" >> $log_file
 
date >> $log_file

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1258
29
C# Tutorial
1232
24
How to check which table space a table belongs to in Oracle How to check which table space a table belongs to in Oracle Jul 06, 2023 pm 01:31 PM

How to check which table space a table belongs to in Oracle: 1. Use the "SELECT" statement and specify the table name to find the table space to which the specified table belongs; 2. Use the database management tools provided by Oracle to check the table space to which the table belongs. Tools usually provide a graphical interface, making the operation more intuitive and convenient; 3. In SQL*Plus, you can view the table space to which the table belongs by entering the "DESCRIBEyour_table_name;" command.

How to connect to Oracle database using PDO How to connect to Oracle database using PDO Jul 28, 2023 pm 12:48 PM

Overview of how to use PDO to connect to Oracle database: PDO (PHPDataObjects) is an extension library for operating databases in PHP. It provides a unified API to access multiple types of databases. In this article, we will discuss how to use PDO to connect to an Oracle database and perform some common database operations. Step: Install the Oracle database driver extension. Before using PDO to connect to the Oracle database, we need to install the corresponding Oracle

How to retrieve only one piece of duplicate data in oracle How to retrieve only one piece of duplicate data in oracle Jul 06, 2023 am 11:45 AM

Steps for Oracle to fetch only one piece of duplicate data: 1. Use the SELECT statement combined with the GROUP BY and HAVING clauses to find duplicate data; 2. Use ROWID to delete duplicate data to ensure that accurate duplicate data records are deleted, or use "ROW_NUMBER" ()" function to delete duplicate data, which will delete all records except the first record in each set of duplicate data; 3. Use the "select count(*) from" statement to return the number of deleted records to ensure the result.

How to use php to extend PDO to connect to Oracle database How to use php to extend PDO to connect to Oracle database Jul 29, 2023 pm 07:21 PM

How to use PHP to extend PDO to connect to Oracle database Introduction: PHP is a very popular server-side programming language, and Oracle is a commonly used relational database management system. This article will introduce how to use PHP extension PDO (PHPDataObjects) to connect to Oracle database. 1. Install the PDO_OCI extension. To connect to the Oracle database, you first need to install the PDO_OCI extension. Here are the steps to install the PDO_OCI extension: Make sure

Implement data import into PHP and Oracle databases Implement data import into PHP and Oracle databases Jul 12, 2023 pm 06:46 PM

Implementing data import into PHP and Oracle databases In web development, using PHP as a server-side scripting language can conveniently operate the database. As a common relational database management system, Oracle database has powerful data storage and processing capabilities. This article will introduce how to use PHP to import data into an Oracle database and give corresponding code examples. First, we need to ensure that PHP and Oracle database have been installed, and that PHP has been configured to

How to query table space size in oracle How to query table space size in oracle Jul 06, 2023 pm 01:45 PM

Steps to query the table space size in Oracle: 1. Log in to the Oracle database using a database administrator account; 2. Use the "SELECT" statement to view the space list; 3. There are three methods to query the table space size: use the dbms_utility package to query, and use the dba_segments view Query, use the dba_data_files view query; 4. Use the "DBMS_OUTPUT.PUT_LINE" function or other methods to display the results to display the query results.

Does oracle database require jdk? Does oracle database require jdk? Jun 05, 2023 pm 05:06 PM

The oracle database requires jdk. The reasons are: 1. When using specific software or functions, other software or libraries included in the JDK are required; 2. Java JDK needs to be installed to run Java programs in the Oracle database; 3. JDK provides Develop and compile Java application functions; 4. Meet Oracle's requirements for Java functions to help implement and implement specific functions.

How to use PHP and Oracle database connection pools efficiently How to use PHP and Oracle database connection pools efficiently Jul 12, 2023 am 10:07 AM

How to efficiently use connection pooling in PHP and Oracle databases Introduction: When developing PHP applications, using a database is an essential part. When interacting with Oracle databases, the use of connection pools is crucial to improving application performance and efficiency. This article will introduce how to use Oracle database connection pool efficiently in PHP and provide corresponding code examples. 1. The concept and advantages of connection pooling Connection pooling is a technology for managing database connections. It creates a batch of connections in advance and maintains a

See all articles