签到成功

知道了

CNDBA社区CNDBA社区

DB2获取所有实例下所有库所有表的count数--改进版

2022-03-31 08:43 1065 0 原创 DB2
作者: hbhe0316

增加了备库的判断

http://www.cndba.cn/hbhe0316/article/107900
http://www.cndba.cn/hbhe0316/article/107900http://www.cndba.cn/hbhe0316/article/107900http://www.cndba.cn/hbhe0316/article/107900http://www.cndba.cn/hbhe0316/article/107900http://www.cndba.cn/hbhe0316/article/107900http://www.cndba.cn/hbhe0316/article/107900http://www.cndba.cn/hbhe0316/article/107900http://www.cndba.cn/hbhe0316/article/107900
http://www.cndba.cn/hbhe0316/article/107900

#!/bin/ksh
###############################################################################################################
#Script name: db2_get_table_count.sh
#Script description: get db2 table count
#Current Release Version: 1.0.0
#Script Owner: hbhe0316
#Latest editor: hbhe0316
#Support platform:  Linux OS for redhat and AIX.
#Change log:use root user and excute sh db2_get_table_count.ksh scripts. result is in /tmp/db2check directory.
#2022/3/25    
#2022/3/30 add getDBArchitecture function
#Descript:date 2022/3/25
#
#
###############################################################################################################
export LANG=en_US
osbox=`uname`
HOSTNAME=$(hostname)
INSTNAME=`ps -ef|grep db2sysc|grep -v grep |awk '{print $1}'|head -1`
DATE_M=$(date '+%Y-%m-%d')

createCheckDir(){
    if [[ ! -d /tmp/db2check ]];then
        mkdir -p /tmp/db2check
        chmod 777 /tmp/db2check
    fi
    RES_PATH="/tmp/db2check"
}

getInstance(){
    if [[ "$INSTNAME" == "" ]];then
            echo 1
            exit 1
    else
        DB2LEVEL=`su - $INSTNAME -c "db2level" |grep tokens|awk -F'"' '{print $2}'|awk -F'v' '{print $2}'`
    fi
}

getOSArchitecture(){
    if [[ "${osbox}" == "Linux" ]];then
        continue
    elif [[ "${osbox}" == "AIX" ]];then
        continue
    else
        echo "Current OS is ${osbox},not support."
        exit 1
    fi
}

getDBArchitecture(){
    instancepath=`cat /etc/passwd | grep -i $1 | awk -F ':' {'print $6'}`
    ARCHITECTURE=`cat "$instancepath"/sqllib/db2nodes.cfg | awk '{if(NF > 3) print "Purescale"; else if(NR > 1) print "DPF"; else print "Alone"}' | tail -1`
    if [[ "$ARCHITECTURE" == "Alone" ]];then
        #cause HADR only in "Alone" architecture
        hadr_role=`su - $1 -c "db2 get db cfg for $2 | grep -i role" | awk '{print $5}'`
        if [[ "$hadr_role" == "PRIMARY" || "$hadr_role" == "STANDBY" ]];then
            ARCHITECTURE="HADR"
        else
            ARCHITECTURE="STANDARD"
        fi
    else
            ARCHITECTURE="DPF"
    fi
}

get_table_count(){
    if [[ ${hadr_role} == "STANDBY" ]];then
        echo "Current DB2 ARCHITECTURE IS ${hadr_role},shell will exit."
        exit 1
    fi
    instgroup=`id $1 | awk {'print $2'} | awk -F '=' {'print $2'} | awk -F '(' {'print $2'} | awk -F ')' {'print $1'}`
    if [[ ! -f "${RES_PATH}/db2HistoryFilePath_tmp.file" ]];then
        touch ${RES_PATH}/db2HistoryFilePath_tmp.file
    fi
    chown $1:$instgroup ${RES_PATH}/db2HistoryFilePath_tmp.file
    chmod u+x ${RES_PATH}/db2HistoryFilePath_tmp.file
    echo "db2 -x /"connect to $2/" > /dev/null" > ${RES_PATH}/db2HistoryFilePath_tmp.file 
#    echo "db2 -x /"select strip(tabschema) || '.' || strip(tabname) from  SYSCAT.tables where  tabschema NOT LIKE 'SYS%' and tabname not like 'EXPLAIN%' and tabname not like 'ADVISE%' and tabname not like 'IBMQ%' and TYPE='T'/"" >> /tmp/db2check/db2HistoryFilePath_tmp.file
    echo "db2 -x /"select 'select ''' || strip(tabschema) || '.' || strip(tabname) || ''' , count(*) from '|| strip(tabschema) || '.' || strip(tabname) || ';' from SYSCAT.tables where type in ('T') AND tabschema NOT LIKE 'SYS%' and tabname not like 'EXPLAIN%' and tabname not like 'ADVISE%' and tabname not like 'IBMQ%'/"" >> ${RES_PATH}/db2HistoryFilePath_tmp.file
    echo "db2 terminate > /dev/null" >> ${RES_PATH}/db2HistoryFilePath_tmp.file

    echo "connect to $2;"  > ${RES_PATH}/${inst}_${dbname}.out
    su - $1 -c "${RES_PATH}/db2HistoryFilePath_tmp.file"  >> ${RES_PATH}/${inst}_${dbname}.out
    echo "terminate;" >> ${RES_PATH}/${inst}_${dbname}.out
    chown $1:$instgroup ${RES_PATH}/${inst}_${dbname}.out
    chmod +x ${RES_PATH}/${inst}_${dbname}.out
    su - $1 -c "db2 -txf ${RES_PATH}/${inst}_${dbname}.out" >> ${RES_PATH}/${inst}_${dbname}_${DATE_M}.res
    cat ${RES_PATH}/${inst}_${dbname}_${DATE_M}.res | grep -Ev "DB20000I|Database|authorization|=" | sort -rn -k2 | tr -s '/n' > ${RES_PATH}/${inst}_${dbname}_${DATE_M}_sort.res
}

getDb2Level(){
    if [[ $DB2LEVEL == 11.1* ]];then
        continue
    else
        echo "Current DB2 VERSION will not test."
        exit 1
    fi

}


getIndirectDBnames(){
    if [[ "$osbox" == "Linux" ]];then
            dblist=`su - $1 -c "db2 list db directory | grep -B5 Indirect | grep 'Database name'" | grep -v 'YOU HAVE NEW MAIL' | awk '{print $4}' | uniq`
    elif [[ "$osbox" == "AIX" ]];then
            dblist=`su - $1 -c "db2 list db directory" | awk '{a[NR]=$0}END{for (i=1;i<=NR;i++) if (a[i]~/Indirect/) for (j=i-5;j<i;j++) print a[j]}' | grep 'Database name' | grep -v 'YOU HAVE NEW MAIL' | awk '{print $4}' | uniq`
    else
        exit 1
    fi
}

main(){
    getOSArchitecture
    getInstance    
    createCheckDir
    getDb2Level
     for inst in `ps -ef | grep db2sysc | grep -v grep | awk '{print $1}' |uniq`;do
        getIndirectDBnames $inst
        for dbname in `echo $dblist`;do
            getDBArchitecture $inst $dbname
            get_table_count $inst $dbname           
        done
    done
}

main

版权声明:本文为博主原创文章,未经博主允许不得转载。

DB2

用户评论
* 以下用户言论只代表其个人观点,不代表CNDBA社区的观点或立场
hbhe0316

hbhe0316

关注

1.只有承认无知,才能装下新的东西; 2.进步来自一点点滴滴的积累; 3.广博让你更优秀,而专业让你无法替代; 4.挫折和失败能够转换为一种财富。

  • 889
    原创
  • 1
    翻译
  • 13
    转载
  • 24
    评论
  • 访问:1039172次
  • 积分:1523
  • 等级:核心会员
  • 排名:第6名
精华文章
    最新问题
    查看更多+
    热门文章
      热门用户
      推荐用户
        Copyright © 2016 All Rights Reserved. Powered by CNDBA · 皖ICP备2022006297号-1·

        QQ交流群

        注册联系QQ