標籤:style cluster 架構 系統內容 函數 寫法 alt 系統 reg
【環境介紹】
系統內容:Linux + 11G+ 叢集/單機
【背景描述】
需求:每個季度都會有資料庫漏洞掃描修複的事情,瞭解該掃描方式是根據資料庫版本來進行判斷是否當前資料庫版本是否修複相應漏洞。而已一般漏洞資訊分DBMS/OJVM/GRID類型漏洞。如果是當前維護的資料庫,自己取資料庫版本資訊比較容易,但是大多時候接到協助升級的時候收集資訊比較麻煩。需要較快的採取資料庫版本資訊和系統資訊來進行寫方案或者判斷當前資料庫是否需要升級及風險判斷。
【監控最佳化過程及思路】
對於上面描述的問題:
1, 主機資訊資料擷取。 ----直接使用主機命令採集
主機名稱(方案),系統版本(方案及補丁集下載),IP資訊(判斷主機IP資訊),系統空間(是否滿足備份及升級空間)
2, 資料資訊資料擷取。 ----使用Oracle查詢資訊
Opatch版本資訊,資料庫lsinventory資訊,資料庫one-of-patch資訊,審計路徑(備份軟體排除審計記錄檔)
3, 資料結構分類。 ----單機、叢集
【測試結果及指令碼】
通過測試指令碼。結果如下:
1, 主機資料資訊:如下:
2, 資料庫資訊:如下:
【採集資料指令碼資訊】
主要實現功能的指令碼如下:
1, 使用shell進行資料處理,具體指令碼解釋如下:
cat >oracle_message.sh
######################################################################
# oracle_message.sh
# This script is update check data
# Author CZT
######################################################################
#!/bin/bash
ORACLE_USER=`ps -ef |grep "ora_pmon_" |grep -v grep |head -1 |awk ‘{print $1}‘`
GRID_USER=`ps -ef |grep "asm_pmon_" |grep -v grep |head -1 |awk ‘{print $1}‘`
process_names=`ps -ef |grep "ora_pmon_" |grep -v grep |awk ‘{print $NF}‘`
process_asms=`ps -ef |grep "asm_pmon_" |grep -v grep |awk ‘{print $NF}‘`
instance_names=`echo ${process_names} |sed ‘s/ora_pmon_//g‘`
instance_names=`echo ${process_names} |sed ‘s/ora_pmon_//g‘`
instance_asm=`echo ${process_asms} |sed ‘s/asm_pmon_//g‘`
v_date=`date ‘+%Y-%m-%d %H:%M:%S‘ ` 》》》設定Oracle及grid的環境變數
function get_value_of_parameter{
parameter_name=${2}
typeset CONNECT_CMD=‘connect / as sysdba‘
typeset SQL_EXIT_OPT="whenever sqlerror exit sql.sqlcode"
typeset SQL_OPT="set echo off feedback off heading off underline off"
typeset SQL_CMD_CLUSTER=‘select value from v\$system_parameter where name=‘"‘${parameter_name}‘"
database_cluster=`su - ${ORACLE_USER} -c "export ORACLE_SID=${1};sqlplus -s /nolog <<!
${CONNECT_CMD}
${SQL_EXIT_OPT}
${SQL_OPT}
${SQL_CMD_CLUSTER};
!"`
echo $database_cluster} 》》》判斷資料庫是否為叢集函數
function get_dba_registry_history{
typeset CONNECT_CMD=‘connect / as sysdba‘
typeset SQL_EXIT_OPT="whenever sqlerror exit sql.sqlcode"
typeset SQL_OPT="set linesize 200 pagesize 20 echo off feedback off"
typeset SQL_CMD_REGISTRY=‘select * from dba_registry_history‘
typeset SQL_CMD_VERSION=‘select * from v\$version‘
typeset SQL_CMD_AUDIT=‘show parameter audit_file_dest‘
su - ${ORACLE_USER} -c "export ORACLE_SID=${1};sqlplus -s /nolog <<!
col action format a20
col namespace format a10
col version format a28
col comments format a40
col action_time format a30
col bundle_series format a15
${CONNECT_CMD}
${SQL_EXIT_OPT}
${SQL_OPT}
${SQL_CMD_REGISTRY};
${SQL_CMD_VERSION};
${SQL_CMD_AUDIT};
!"} 》》》查詢資料庫字典資訊函數
function get_asm_registry_history{
typeset CONNECT_CMD=‘connect / as sysasm‘
typeset SQL_EXIT_OPT="whenever sqlerror exit sql.sqlcode"
typeset SQL_OPT="set linesize 200 pagesize 20 echo off feedback off"
typeset SQL_CMD_AUDIT=‘show parameter audit_file_dest‘
su - ${GRID_USER} -c "export ORACLE_SID=${1};sqlplus -s /nolog <<!
col action format a20
col namespace format a10
col version format a28
col comments format a40
col action_time format a30
col bundle_series format a15
${CONNECT_CMD}
${SQL_EXIT_OPT}
${SQL_OPT}
${SQL_CMD_AUDIT};
!"} 》》》查詢+ASM執行個體審計路徑資訊函數
function get_value_of_system{
echo -e ‘
==========================
Hostname
==========================
‘
hostname;
echo -e ‘
==========================
Systemrelease
==========================
‘
cat /etc/*release*;
echo -e ‘
==========================
System space
==========================
‘
df -h;
echo -e ‘(4)SYSTEM HOSTS‘
echo -e ‘
==========================
System hosts
==========================
‘
cat /etc/hosts;} 》》》採集主機資訊資料函數
function get_value_of_database{
ORACLE_BASE=`cat /home/oracle/.bash_profile|grep -wi ‘export ORACLE_BASE‘|awk -F "[=:]" ‘{print $NF}‘`
ORACLE_HOME=`cat /home/oracle/.bash_profile|grep -wi ‘export ORACLE_HOME‘|awk -F "[=:]" ‘{print $NF}‘`
echo -e ‘
==========================
Opatch version
==========================
‘
su - ${ORACLE_USER} -c "${ORACLE_HOME}/OPatch/opatch version"
echo -e ‘
==========================
Opatch lsinventory
==========================
‘
su - ${ORACLE_USER} -c "${ORACLE_HOME}/OPatch/opatch lsinv"
echo -e ‘
==========================
Opatch lspatches
==========================
‘
su - ${ORACLE_USER} -c "${ORACLE_HOME}/OPatch/opatch lspatches"
}
###judge the current user if root,need to root run the script
if [ `whoami` = "root" ];then 》》》判斷使用root使用者執行該指令碼
echo -e ‘ *** Start of LogFile *** ‘
echo -e ‘ Oracle Database Upgrade Statistics Geting ‘ $v_date
###check system state
echo -e ‘---------------------------------------------SYSTEM STATE-------------------------------------‘
###get system state
get_value_of_system
###judge the system have many instance
for instance in ${instance_names}
do
###judge the database cluster
v_database_cluster=`get_value_of_parameter ${instance} "cluster_database"`
if [ "${v_database_cluster}" == "TRUE" ];then
echo -e ‘
==========================
The Database Is RAC
==========================
‘
###check database state
echo -e ‘--------------------------------------DATABASE STATE------------------------------------‘
echo -e ‘
=============================================================================
The Database Is RAC 》》》判斷為叢集架構
=============================================================================
‘
###get database opatch state
get_value_of_database 》》》調用採集資料庫資訊函數
echo -e ‘
==========================
Dba Registry History
==========================
‘
get_dba_registry_history ${instance} 》》》調用採集資料庫資訊函數
get_asm_registry_history ${instance_asm} 》》》調用採集ASM執行個體審計路徑函數
else
###check database state
echo -e ‘--------------------------------------------DATABASE STATE------------------------------------‘
echo -e ‘
=============================================================================
The Database Is Standlone 》》》判斷為單機架構
=============================================================================
‘
###get database opatch state
get_value_of_database
echo -e ‘
==========================
Dba Registry History
==========================
‘
get_dba_registry_history ${instance}
fi
done
echo -e ‘ ‘
echo -e ‘ *** End of LogFile *** ‘
else
echo "Please use root run the script"
fi
【問題思考】
1, 存在系統版本限制,該指令碼適用於linux Solaris系統採集資料;
2, 存在資料庫版本限制,適用於11G版本資料庫採集資料;
3, 存在環境變數限制,Oracle使用者及grid使用者的profile需要採用標準的寫法設定變數。
【總結】:
1, 在對外部協助資料庫漏洞核查時,使用該指令碼更加快捷的採集資訊,對編寫方案及更快的對資料庫環境的大致瞭解對資料庫升級操作提供安全保障。
2, 本次指令碼涉及使用root使用者執行指令碼調用Oracle及grid使用者的變數,對使用者的變數設定標準有較大的要求。
針對資料庫升級版本資訊採集指令碼編寫