From 1e6b9e159a015ebcac53684bcfba3911d2fa9833 Mon Sep 17 00:00:00 2001 From: Prudhviraj P <46192880+patratap@users.noreply.github.com> Date: Mon, 16 Feb 2026 17:39:52 +0530 Subject: [PATCH] Refactor runstats_rebind.sh for consistency and clarity Updated script to improve variable naming and formatting consistency. Adjusted paths for temporary files and logging. Added to verify day of the week to trigger rebinds --- .../files/runstats_rebind.sh | 111 ++++++++++-------- 1 file changed, 59 insertions(+), 52 deletions(-) diff --git a/instance-applications/120-ibm-db2u-database/files/runstats_rebind.sh b/instance-applications/120-ibm-db2u-database/files/runstats_rebind.sh index 7415d6b4a..3ce46234f 100755 --- a/instance-applications/120-ibm-db2u-database/files/runstats_rebind.sh +++ b/instance-applications/120-ibm-db2u-database/files/runstats_rebind.sh @@ -1,41 +1,43 @@ #!/bin/bash # *************************************************************************** -# Author: Fu Le Qing (Roking) -# Email: leqingfu@cn.ibm.com -# Date: 10-31-2018 +# Author: Fu Le Qing (Roking) +# Email: leqingfu@cn.ibm.com +# Date: 10-31-2018 # -# Description: This script updates statistics of tables, -# associated indexes in the database, and sends an email -# to a specified email list. +# Description: This script updates statistics of tables, +# associated indexes in the database, and sends an email +# to a specified email list. # # ******** THIS NEEDS TO BE RUN AS INSTANCE OWNER. ************** # -# Revision history: -# 10-31-2018 Fu Le Qing (Roking) -# Original version -# 11-16-2018 Fu Le Qing (Roking) -# Skip the tables which are ongoing with reorg -# 09-08-2023 Fu Le Qing (Roking) -# Update for MAS +# Revision history: +# 10-31-2018 Fu Le Qing (Roking) +# Original version +# 11-16-2018 Fu Le Qing (Roking) +# Skip the tables which are ongoing with reorg +# 09-08-2023 Fu Le Qing (Roking) +# Update for MAS # # *************************************************************************** # # *************************************************************************** + +# -- Source the Props file if [ -f /mnt/backup/bin/.PROPS ] then . /mnt/backup/bin/.PROPS - DOW=`date | awk '{print $1}'` - if [ ${DOW} != ${DAYOFFULL} ] - then - exit 0 - fi + DOW=`date | awk '{print $1}'` fi -instance=`whoami` -instance_home=`/usr/local/bin/db2greg -dump | grep -ae "I," | grep -v "/das," | grep "${instance}" | awk -F ',' '{print $5}'| sed 's/\/sqllib//'` +# -- Standard Parameters +INSTANCE=`whoami` +INSTANCE_HOME=`/usr/local/bin/db2greg -dump | grep -ae "I," | grep -v "/das," | grep "${INSTANCE}" | awk -F ',' '{print $5}'| sed 's/\/sqllib//'` +DOW=`date | awk '{print $1}'` +mkdir -p ${INSTANCE_HOME}/maintenance/logs +DATESTAMP=`date "+%Y-%m-%d-%H.%M.%S"` -pidfile="$instance_home/.`basename ${0}`.pid" -if [ -e ${pidfile} ] && $kill -0 `cat ${pidfile}` 2>/dev/null +pidfile="${INSTANCE_HOME}/.`basename ${0}`.pid" +if [ -e ${pidfile} ] && $kill -0 `cat ${pidfile}` 2>/dev/null then exit 0 fi @@ -43,48 +45,53 @@ fi echo $$ > ${pidfile} trap "rm -f ${pidfile}; exit" SIGHUP SIGINT SIGQUIT SIGABRT SIGTERM EXIT -if [ ! -f "$instance_home/sqllib/db2profile" ] +if [ ! -f "${INSTANCE_HOME}/sqllib/db2profile" ] then - echo "ERROR - $instance_home/sqllib/db2profile not found" + echo "ERROR - ${INSTANCE_HOME}/sqllib/db2profile not found" exit 1 else - . $instance_home/sqllib/db2profile + . ${INSTANCE_HOME}/sqllib/db2profile fi -RUNSTATS_TMP_FILE="$instance_home/.runstats.sql" -REBIND_TMP_FILE="$instance_home/.rebind.sql" - -mkdir -p $instance_home/maintenance/logs -DATESTAMP=`date "+%Y-%m-%d-%H.%M.%S"` +RUNSTATS_TMP_FILE="${INSTANCE_HOME}/bin/.runstats.sql" +REBIND_TMP_FILE="${INSTANCE_HOME}/bin/.rebind.sql" -for db in `db2 list db directory | grep -B 5 Indirect | grep "Database name" | cut -d= -f2` +for DB in `db2 list db directory | grep -B 5 Indirect | grep "Database name" | cut -d= -f2` do - role=`db2 get db cfg for ${db} | grep "HADR database role" | cut -d= -f2 |sed 's/ //g'` + RUNSTATS_REBIND_LOG="${INSTANCE_HOME}/maintenance/logs/runstats_rebind_${DB}_${DATESTAMP}.log" + + role=`db2 get db cfg for ${DB} | grep "HADR database role" | cut -d= -f2 |sed 's/ //g'` if [ "$role" != "STANDBY" ]; then - if [ -f $RUNSTATS_TMP_FILE ] - then - rm $RUNSTATS_TMP_FILE + if [[ -f ${RUNSTATS_TMP_FILE} ]]; then + rm ${RUNSTATS_TMP_FILE} fi - if [ -f $REBIND_TMP_FILE ] - then - rm $REBIND_TMP_FILE + if [[ -f ${REBIND_TMP_FILE} ]]; then + rm ${REBIND_TMP_FILE} fi - db2 connect to ${db} | tee $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} - if [ $? -eq 0 ]; then - db2 -x "select 'RUNSTATS ON TABLE \"' ||rtrim(tab.tabschema)||'\".\"'|| tab.tabname ||'\" WITH DISTRIBUTION ON KEY COLUMNS AND DETAILED INDEXES ALL ALLOW WRITE ACCESS;' + + db2 connect to ${DB} | tee ${RUNSTATS_REBIND_LOG} + if [[ $? -eq 0 ]]; then + db2 -x "select 'RUNSTATS ON TABLE \"' ||rtrim(tab.tabschema)||'\".\"'|| tab.tabname ||'\" WITH DISTRIBUTION ON KEY COLUMNS AND DETAILED INDEXES ALL ALLOW WRITE ACCESS;' from syscat.tables tab left join sysibmadm.SNAPTAB_REORG reg on tab.tabschema=reg.TABSCHEMA and tab.tabname=reg.TABNAME and reg.REORG_STATUS not in ('COMPLETED','STOPPED') - where tab.type='T' and reg.tabname is null" > $RUNSTATS_TMP_FILE - #db2 -x "select 'rebind package \"' ||rtrim(PKGSCHEMA)||'\".\"'|| PKGNAME ||'\";' from syscat.packages where PKGSCHEMA not in ('NULLID','NULLIDR1','NULLIDRA','SYSIBMADM','SYSIBMINTERNAL') and PKGSCHEMA not like 'NULL%' " > $REBIND_TMP_FILE - echo "Begin processing of runstats @ $DATESTAMP ..." | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} - db2 -txvf $RUNSTATS_TMP_FILE | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} - echo "End processing of runstats @ $DATESTAMP" | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} + where tab.type='T' and reg.tabname is null" > ${RUNSTATS_TMP_FILE} + + echo -e "Begin processing of runstats @ ${DATESTAMP} ...\n" | tee -a ${RUNSTATS_REBIND_LOG} + db2 -txvf ${RUNSTATS_TMP_FILE} | tee -a ${RUNSTATS_REBIND_LOG} + echo -e "\nEnd processing of runstats @ ${DATESTAMP}" | tee -a ${RUNSTATS_REBIND_LOG} + rm ${RUNSTATS_TMP_FILE} - #echo "Begin processing of rebind @ $DATESTAMP ..." | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} - #db2 -txvf $REBIND_TMP_FILE | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} - #echo "End processing of rebind @ $DATESTAMP" | tee -a $instance_home/maintenance/logs/runstats_${db}_${DATESTAMP} - rm $RUNSTATS_TMP_FILE - #rm $REBIND_TMP_FILE + if [[ ${DOW} == ${DAYOFFULL} ]]; then + + db2 -x "select 'rebind package \"' ||rtrim(PKGSCHEMA)||'\".\"'|| PKGNAME ||'\";' from syscat.packages where PKGSCHEMA not like 'SYSIBM%' and PKGSCHEMA not like 'NULL%' " > ${REBIND_TMP_FILE} + + echo -e "\n ----------------------------------------------- " | tee -a ${RUNSTATS_REBIND_LOG} + echo -e "Begin processing of rebind @ ${DATESTAMP} ...\n" | tee -a ${RUNSTATS_REBIND_LOG} + db2 -txvf ${REBIND_TMP_FILE} | tee -a ${RUNSTATS_REBIND_LOG} + echo -e "\nEnd processing of rebind @ ${DATESTAMP}" | tee -a ${RUNSTATS_REBIND_LOG} + + rm ${REBIND_TMP_FILE} + fi db2 terminate - fi + fi fi done