shell脚本之连接DB2并监控数据库实战

[db2inst1@t3-dtpoc-dtpoc-web04 monitor]$ pwd
/home/db2inst1/monitor

vi monitor.sh

脚本代码:

dbname=TEST
LOG_PATH="/home/db2inst1/monitor/"
lock_waiting_seconds=0

# Init env
. ~/sqllib/db2profile

cd $LOG_PATH

# Check if hadr standby

HADR_ROLE=`db2pd -d $dbname -hadr |grep HADR_ROLE|awk '{print $3}'`

if [ "$HADR_ROLE" != "STANDBY" ]
then
        db2 connect to $dbname 2>&1 > /dev/null
        if [ $? -eq 0 ]
        then
                lock_waiting_seconds=`db2 -x "select coalesce(max (timestampdiff(2,char(lw.SNAPSHOT_TIMESTAMP-lw.LOCK_WAIT_START_TIME)) ),0) as locks_time_s from sysibmadm.lockwaits  lw,sysibmadm.snapappl_info ai_h where   lw.AGENT_ID=ai_h.AGENT_ID   with ur"`
                echo $lock_waiting_seconds >> locks_time_s
                if [ $lock_waiting_seconds -gt 1200 ]
                then
                        db2pd -d $dbname -wlocks -alldbp > /home/db2inst1/monitor/logs/wlocks.$timeStamp
                        db2pd -d $dbname -api > /home/db2inst1/monitor/logs/api.$timeStamp
                fi
        fi

else
        echo $lock_waiting_seconds >> locks_time_s


fi

制造锁等应用,然后执行监控脚本 sh moniotr.sh

窗口1:db2 +C "update COURSE set c_id=22 where c_id=20"

窗口2:db2 "delete from course where c_id=22"

可以看到锁等的信息和应用具体信息都已经捕获

Database Member 0 -- Database TEST -- Active -- Up 0 days 00:08:04 -- Date 2023-09-27-14.00.24.280674

Locks being waited on :
AppHandl [nod-index] TranHdl    Lockname                   Type       Mode Conv Sts CoorEDU    AppName  AuthID   AppID
9364     [000-09364] 14         02000001130003000000000052 RowLock    ..X       G   474        db2bp    DB2INST1 *LOCAL.db2inst1.230927055556
9365     [000-09365] 15         02000001130003000000000052 RowLock    ..X       W   472        db2bp    DB2INST1 *LOCAL.db2inst1.230927055603
 

[db2inst1@t3-dtpoc-dtpoc-web04 monitor]$ pwd
/home/db2inst1/monitor
[db2inst1@t3-dtpoc-dtpoc-web04 monitor]$ cd logs
[db2inst1@t3-dtpoc-dtpoc-web04 logs]$ ls
api.  wlocks.
[db2inst1@t3-dtpoc-dtpoc-web04 logs]$ vi wlocks

你可能感兴趣的:(服务器,linux,前端)