增加了备库的判断
#!/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