针对数据库升级版本信息采集脚本编写

【环境介绍】

  系统环境: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用户的变量,对用户的变量设置标准有较大的要求。

posted @ 2018-03-10 20:48  zetan·chen  阅读(263)  评论(0编辑  收藏  举报