自动收集数据库应用系统信息的脚本

总结一种自动收集数据库应用系统信息的方法,提高远程系统维护的效率. (以下内容仅在suse linux + oracle 11g上测试通过)

 

功能:可以收集某应用程序的进程信息,配置信息;  硬盘设备信息; 数据库进程信息,表空间信息,以及特定数据表的内容,以及指定的sql语句的执行结果.

使用方法: 以root用户将以下两个文件上传至同一目录,再执行rpt_support.sh,即可将信息收集到 "主机名-时间.tar".

 

 

文件1: rpt_support.ini

#application configure: the user of Application system
APPLICATION_USER=ap_user

#prestat configure: the path which Application installed in
APPLICATION_SETUP_PATH=/home/ap

#database configure: the OS User of Oracle Database
ORACLE_USER=oracle

#database configure: the user name of database
DATABASE_USER=db_user_name

#database configure: the user's password of database
DATABASE_USER_PASSWORD=db_user_passwd

#database configure: the important tables should be feedback,multi-table should be separated by ","
DATABASE_TABLES=t_info_version,t_sys_config,t_info_config,t_runlog,

 

 

文件2: rpt_support.sh

#!/usr/bin/ksh

#  ======================================================================
#  File name:   rpt_Support.sh
#  Author:      xuwei
#  Version:    
#  Date:        2009-06-30
#  Description: collect the info of oracle system,out put to result.txt
#  Content:      
#  Others:         NULL
#  History:
#    1.    Date:        2009-06-30
#          Author:      xuwei
#          Description: initial
# ======================================================================

# include configures
BASEDIR=`pwd`
#BASEDIR=`dirname $path`
CONFIG_FILE=$BASEDIR/rpt_support.ini
. $CONFIG_FILE

HOSTNAME=`hostname`
TIMESTAMP=`date +%Y%m%d%H%M`
USER_LOG_FILE=$BASEDIR/${HOSTNAME}_${TIMESTAMP}.log

# description: log a message to the user defined log file
function WriteLog
{
    if [ ! -f "$USER_LOG_FILE" ]
    then
        touch $USER_LOG_FILE
    fi

#   print "$(date '+%Y%m%d%H%M') - Node /"$(HOSTNAME)/": /c" >> $USER_LOG_FILE
    print $* >> $USER_LOG_FILE
#   logfz=`ls -l $USER_LOG_FILE| awk '{printf "%s", $5}'`
#   if [ $logfz -ge $MAX_LOG_FILE_SIZE ]
#   then
#       mv $USER_LOG_FILE $USER_LOG_FILE.old
#       echo "-------backup App HA Agent log file------" >> $USER_LOG_FILE.old
#       touch $USER_LOG_FILE
#   fi
}

# description: log a OS command to the log file
function WriteCMDLog
{
    print "#$(whoami)> $*" >> $USER_LOG_FILE
    print "$($*)" >> $USER_LOG_FILE
}

# description: log a OS command to the log file
function WriteCMDLog
{
    print "#$(whoami)> $*" >> $USER_LOG_FILE
    print "$($*)" >> $USER_LOG_FILE
}

# description: write a sql text, which be executed by sqlplus.
function WriteSqlFile
{
    print "conn / as sysdba"             > $1
    print "set pagesize 10000;"          >> $1
    print "set linesize 400;"            >> $1
    print "set feedback off;"            >> $1
    print "spool ora_${HOSTNAME}.log;"   >> $1
    print "select tablespace_name, bytes / (1024*1024) TotalSpace_MB from sm/$ts_avail;" >> $1
    print "select tablespace_name, bytes / (1024*1024) UsedSpace_MB from sm/$ts_used;"   >> $1
    print "select tablespace_name, bytes / (1024*1024) FreeSpace_MB from sm/$ts_free;"   >> $1
    print "select resource_type,limit from dba_profiles where profile = 'DEFAULT' and resource_name = 'PASSWORD_LIFE_TIME';" >> $1
    print "select resource_type,limit from dba_profiles where profile = 'DEFAULT' and resource_name = 'FAILED_LOGIN_ATTEMPTS';" >> $1
    print "show parameter spfile;"      >> $1
    print "show parameter audit_trail;" >> $1
    print "show parameter recyclebin;"  >> $1
    print "conn ${DATABASE_USER}/${DATABASE_USER_PASSWORD};" >> $1

    TABLE_NAME=`echo ${DATABASE_TABLES} | awk -F, {'print $1'}`
    while [ "X" != "X${TABLE_NAME}" ]
      do
         DATABASE_TABLES=`echo ${DATABASE_TABLES} | awk -F, '{print substr($0,index($0,",")+1)}'`
         print "select * from ${TABLE_NAME};" >> $1
         TABLE_NAME=`echo ${DATABASE_TABLES} | awk -F, {'print $1'}`
      done

    echo "exit;" >> $1
}


############################ Program Entry ####################################
    WriteLog "0.Get base info    =================================="
    tar cf $HOSTNAME-$TIMESTAMP.tar $CONFIG_FILE

    WriteLog "1.collect disk info=================================="
    WriteCMDLog "df -h"
    WriteCMDLog "fdisk -l"
    WriteCMDLog "vgdisplay"
    WriteCMDLog "lvdisplay"

 

 

 

    WriteLog "2.collect application process info======================="
    USER_EXISTS=`cat /etc/passwd | grep "${APPLICATION_USER}:x:" | grep -v "grep"` > /dev/null
    if [ "X" = "X${USER_EXISTS}" ]
    then
       WriteLog "error: user ${APPLICATION_USER} not exists!"
       print "warning:user /"${APPLICATION_USER}/" not exists! please reconfig this file /"${CONFIG_FILE}/"."
    else
       WriteCMDLog "su -c /"pshow/" - ${APPLICATION_USER}"
    fi

    if [ -f "$APPLICATION_SETUP_PATH/CSHRC" ] && [ -d "$APPLICATION_SETUP_PATH/etc" ]
    then
       tar rf $HOSTNAME-$TIMESTAMP.tar $APPLICATION_SETUP_PATH/CSHRC
       tar rf $HOSTNAME-$TIMESTAMP.tar $APPLICATION_SETUP_PATH/lib
       tar rf $HOSTNAME-$TIMESTAMP.tar $APPLICATION_SETUP_PATH/etc
       tar rf $HOSTNAME-$TIMESTAMP.tar $APPLICATION_SETUP_PATH/data
    else
      WriteLog "error: your Prestat software not installed in ${APPLICATION_SETUP_PATH}!"
       print "warning:your Prestat software not installed in ${APPLICATION_SETUP_PATH}!"
    fi

 

 


    WriteLog "3.collect db info===================================="
    USER_EXISTS=`cat /etc/passwd | grep "${ORACLE_USER}:x:"` > /dev/null

    # check the oracle user name in OS
    if [ "X" = "X${USER_EXISTS}" ]
    then
       WriteLog "error: user ${ORACLE_USER} not exists!"
       print "warning:user /"${ORACLE_USER}/" not exists! please reconfig this file /"${CONFIG_FILE}/"."
    else
       # check oracle process in OS
       ps -ef | grep "${ORACLE_USER}" | grep "ora_" | awk {'print $8'} > ora_process_$TIMESTAMP.log
       tar rf $HOSTNAME-$TIMESTAMP.tar ora_process_$TIMESTAMP.log

       # get info from database
       ORACLE_BASE=`more /etc/passwd | grep "${ORACLE_USER}:x:" | awk -F: {'print $6'}` > /dev/null
       SQLFILE=$ORACLE_BASE/ora_support.sql
       WriteSqlFile "${SQLFILE}"
       chmod +rx "${SQLFILE}"
       su -c "sqlplus /nolog @${SQLFILE}" - ${ORACLE_USER} > /dev/null ###> ora_db_$TIMESTAMP.log
       tar rf $HOSTNAME-$TIMESTAMP.tar $SQLFILE
       tar rf $HOSTNAME-$TIMESTAMP.tar $ORACLE_BASE/ora_${HOSTNAME}.log

       rm -f ora_process_$TIMESTAMP.log
       rm -f $SQLFILE
       rm -f $ORACLE_BASE/ora_${HOSTNAME}.log
    fi

    tar uf $HOSTNAME-$TIMESTAMP.tar $USER_LOG_FILE
    rm -f $USER_LOG_FILE

    echo ""
    echo ""
    echo "Attention: All info are collectd, please send $HOSTNAME-$TIMESTAMP.tar to supporter."
############################ Program exit ####################################

你可能感兴趣的:(自动收集数据库应用系统信息的脚本)