#!/bin/sh
###############################################################################################################
#Script name: Db2_Resize_Tablespace.sh
#Script description: Resize tablespace space.
#Current Release Version: 1.0.0
#Script Owner: He, Haibo
#Latest editor: He, Haibo
#Support platform: db2 9.7,10.1,10.5,11.1
#Change log:
#
#
#
###############################################################################################################
export LANG=en_US
paracount=$#
osbox=`uname`
userName=`id | awk -F ' ' {'print $1'} | awk -F '(' {'print $2'} | awk -F ')' {'print $1'}`
RHversion=$(cat /proc/version | sed 's/[^0-9]//g' | cut -b -3)
####判断传入的参数是否为3
if [ $paracount == 3 ];then
DatabaseName=$1
TabSpaceName=$2
TabSpaceSize=$3
else
echo "Example:./Db2_Resize_Tablespace.sh testdb tbs02 1"
echo 1
exit 1
fi
###如果没有 /yunwei/Release/log目录,则创建
mkdirLogPath(){
if [[ ! -d /yunwei/Release/log ]];then
mkdir -p /yunwei/Release/log
chmod 767 /yunwei/Release/log
fi
db2LogPath="/yunwei/Release/log"
db2Log=db2resizetablespace_$(date +%y%m%d).log
}
###判断系统是否为Linux平台,如果不是Linux,则退出.
getOSArchitecture(){
if [[ "$osbox" == "Linux" ]];then
continue
else
echo "Current OS is $osbox,shell is exit now." | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
}
###判断系统是否为Redhat,如果不是则退出,支持Redhat 5、6、7三个版本
getOSVersion(){
cat /proc/version | grep -i redhat > /dev/null
if [[ $? == 0 ]];then
if [[ "$RHversion" -ge 261 ]];then
continue
else
echo "Current Rehat Version will not support." | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
else
echo "Current os is not Redhat."
echo 1
exit 1
fi
}
###判断是否是实例用户,如果是非实例用户则退出
getInstanceUser(){
ps -ef | grep db2sysc | grep -v grep | awk '{print $1}' |uniq | grep -i $userName > /dev/null
if [[ $? == 0 ]];then
continue
else
echo "Current user is $userName,is not InstanceName or DB2 Instance is not exists." | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
}
###判断DB2版本是否为9.7、10.1、10.5、11.1
getDBVersion(){
DB2LEVEL=`db2level |grep tokens|awk -F'"' '{print $2}'|awk -F'v' '{print $2}'`
if [[ $DB2LEVEL == 11.1* || $DB2LEVEL == 10.5* || $DB2LEVEL == 10.1* || $DB2LEVEL == 9.7* ]];then
continue
else
echo "Current $DB2LEVEL does not Support" | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
}
###判断传入的数据库库名是否存在
getDBName(){
db2 list db directory | grep -B5 Indirect | grep 'Database alias' | awk '{print $4}' | grep -iw $DatabaseName > /dev/null
if [[ $? == 0 ]];then
continue
else
echo "Does not have $DatabaseName in Current database" | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
}
###判断传入的库是否激活,如果未激活,则退出
getDBActivate(){
db2 list active databases | grep -iw $DatabaseName > /dev/null
if [[ $? == 0 ]];then
continue
else
echo "Current database $DatabaseName is not activate." | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
}
###判断数据库架构
getDBArchitecture(){
ARCHITECTURE=`cat ~/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=`db2 get db cfg for $DatabaseName | grep -i role | awk '{print $5}'`
if [[ "$hadr_role" == "PRIMARY" || "$hadr_role" == "STANDARD" ]];then
continue
else
echo "ARCHITECTURE is standby" | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
else
echo "ARCHITECTURE is $ARCHITECTURE" | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
}
getConnectDB(){
###db2 connect to <dbname>
db2 connect to $DatabaseName >> $db2LogPath/$db2Log
if [[ $? == 0 ]];then
continue
else
echo "Connect $1 not correct." | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
}
###判断传入的tablespace是否存在
getTableSpaceName(){
TbsName=$(echo $TabSpaceName|tr '[a-z]' '[A-Z]')
db2 -x "select tbspace from syscat.tablespaces where tbspace='$TbsName'" > /dev/null
if [[ $? == 0 ]];then
continue
else
echo "$TabSpaceName does not exists in database $DatabaseName" | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
}
###判断传入的tablespace状态是否为Normal,如果不是normal,则退出。
getTablespaceStatus(){
TbsName=$(echo $TabSpaceName|tr '[a-z]' '[A-Z]')
TbsStatus=`db2 -x "select varchar(TBSP_STATE,20) from table(MON_GET_TABLESPACE('',-2)) where tbsp_name='$TbsName'"`
echo "$TbsName is $TbsStatus"
if [[ "$TbsStatus" =~ "NORMAL" ]];then
continue
else
echo "$TabSpaceName's status is not NORMAL now."
echo 1
exit 1
fi
}
resizeTablespaceSize(){
temp=$(db2 -x "select varchar(tbsp_name,30) as tbsp_name,tbsp_type,tbsp_using_auto_storage,tbsp_auto_resize_enabled from table(MON_GET_TABLESPACE('',-2))")
tbsType=`echo "$temp" | grep -i $TbsName | awk {'print $2'}`
autoStorage=`echo "$temp" | grep -i $TbsName | awk {'print $3'}`
autoResize=`echo "$temp" | grep -i $TbsName | awk {'print $4'}`
# maxSize=`echo "$temp" | grep -i $TbsName | awk {'print $5'}`
# tbsId=`db2 -x "select varchar(tbsp_name,30) as tbsp_name,tbsp_type,tbsp_using_auto_storage,tbsp_auto_resize_enabled,tbsp_max_size,tbsp_id from table(MON_GET_TABLESPACE('',-2))" | grep -i $TbsName | awk {'print $6'}`
# tbsPathName=`db2 list tablespace containers for $tbsId | grep Name | awk -F '=' {'print $2'} | awk -F '/' {'print $2'}`
temp1=`db2 -x "select varchar(TBSP_NAME,30) as TBSP_NAME,varchar(CONTAINER_NAME,100) as CONTAINER_NAME,FS_TOTAL_SIZE/1024/1024/1024-FS_USED_SIZE/1024/1024/1024 from table(MON_GET_CONTAINER('',-2))"`
containerNum=`echo "$temp1" | grep -i $TbsName | awk {'print $2'} | wc -l`
# echo "containerNum is $containerNum"
# TotalFreePathSize=`echo "$temp1" | grep -i $TbsName | awk {'print $3'} | awk '{sum += $1} END {print sum}'`
#获取多路径情况下,最小的path值
MinPathSize=`echo "$temp1" | grep -i $TbsName | awk {'print $3'} | awk 'BEGIN {min = 65536} {if ($1+0 < min+0) min = $1} END {print min}'`
# echo "MinPathSize is $MinPathSize"G""
TablespaceSize_M=`echo $TabSpaceSize*1024/$containerNum|bc`
MinPathSize_M=`echo $MinPathSize*1024|bc`
# echo "MinPathSize_M is $MinPathSize_M"
if [[ $MinPathSize_M -gt $TabSpaceSize_M ]];then
continue
else
echo "Extend Size is bigger than the Free Size,script will exit now." | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
if [[ $autoStorage == 0 && "$tbsType" == "DMS" ]];then
if [[ $autoResize == 0 ]];then
echo "TablespaceSize_M is $TablespaceSize_M"M""
db2 "alter tablespace $TabSpaceName extend (all $TablespaceSize_M M)" >> $db2LogPath/$db2Log
if [[ $? == 0 ]];then
echo "Add tablespace $TabSpaceName size $TabSpaceSize"G" succussfully" | tee -a $db2LogPath/$db2Log
echo 0
exit 0
else
echo "Alter tablespace failed." | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
else
echo "$TabSpaceName autoResize is YES,script will exit now." | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
else
echo "AutoStorage is $autoStorage and TbsType is $tbsType,script will exit now." | tee -a $db2LogPath/$db2Log
echo "$TabSpaceName cannot extend " | tee -a $db2LogPath/$db2Log
echo 1
exit 1
fi
}
main(){
mkdirLogPath
getOSArchitecture
getOSVersion
getInstanceUser
getDBVersion
getDBName
getDBActivate
getDBArchitecture
getConnectDB
getTableSpaceName
getTablespaceStatus
resizeTablespaceSize
}
main
https://www.cndba.cn/hbhe0316/article/22188
https://www.cndba.cn/hbhe0316/article/22188
https://www.cndba.cn/hbhe0316/article/22188
https://www.cndba.cn/hbhe0316/article/22188
https://www.cndba.cn/hbhe0316/article/22188
https://www.cndba.cn/hbhe0316/article/22188
https://www.cndba.cn/hbhe0316/article/22188
https://www.cndba.cn/hbhe0316/article/22188
https://www.cndba.cn/hbhe0316/article/22188
https://www.cndba.cn/hbhe0316/article/22188
版权声明:本文为博主原创文章,未经博主允许不得转载。
DB2