签到成功

知道了

CNDBA社区CNDBA社区

DB2获取所有实例下所有库所有表的count数

2022-03-28 17:41 1074 1 原创 DB2
作者: hbhe0316
#!/bin/ksh
###############################################################################################################
###############################################################################################################
export LANG=en_US
osbox=`uname`
HOSTNAME=$(hostname)
INSTNAME=`ps -ef|grep db2sysc|grep -v grep |awk '{print $1}'|head -1`


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

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
}

get_table_count(){
    instgroup=`id $1 | awk {'print $2'} | awk -F '=' {'print $2'} | awk -F '(' {'print $2'} | awk -F ')' {'print $1'}`
    if [[ ! -f "/tmp/db2check/db2HistoryFilePath_tmp.file" ]];then
        touch /tmp/db2check/db2HistoryFilePath_tmp.file
    fi
    chown $1:$instgroup /tmp/db2check/db2HistoryFilePath_tmp.file
    chmod u+x /tmp/db2check/db2HistoryFilePath_tmp.file
    echo "db2 -x /"connect to $2/" > /dev/null" > /tmp/db2check/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%'/"" >> /tmp/db2check/db2HistoryFilePath_tmp.file
    echo "db2 terminate > /dev/null" >> /tmp/db2check/db2HistoryFilePath_tmp.file

    echo "connect to $2;"  > /tmp/db2check/${inst}_${dbname}.out
    su - $1 -c "/tmp/db2check/db2HistoryFilePath_tmp.file"  >> /tmp/db2check/${inst}_${dbname}.out
    echo "terminate;" >> /tmp/db2check/${inst}_${dbname}.out
    chown $1:$instgroup /tmp/db2check/${inst}_${dbname}.out
    chmod +x /tmp/db2check/${inst}_${dbname}.out
    su - $1 -c "db2 -txf /tmp/db2check/${inst}_${dbname}.out" >> /tmp/db2check/${inst}_${dbname}.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
            get_table_count $inst $dbname           
        done
    done
}

main
http://www.cndba.cn/hbhe0316/article/107896
http://www.cndba.cn/hbhe0316/article/107896
http://www.cndba.cn/hbhe0316/article/107896
http://www.cndba.cn/hbhe0316/article/107896 http://www.cndba.cn/hbhe0316/article/107896
http://www.cndba.cn/hbhe0316/article/107896
http://www.cndba.cn/hbhe0316/article/107896
http://www.cndba.cn/hbhe0316/article/107896 http://www.cndba.cn/hbhe0316/article/107896
http://www.cndba.cn/hbhe0316/article/107896

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

DB2

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

hbhe0316

关注

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

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

        QQ交流群

        注册联系QQ