MySQL 主從延遲監控指令碼(pt-heartbeat),mysqlpt-heartbeat

來源:互聯網
上載者:User

MySQL 主從延遲監控指令碼(pt-heartbeat),mysqlpt-heartbeat

    對於MySQL資料庫主從複寫延遲的監控,我們可以藉助percona的有力武器pt-heartbeat來實現。pt-heartbeat通過使用時間戳方式在主庫上更新特定表,然後在從庫上讀取被更新的時間戳記然後與本地系統時間對比來得出其延遲。本文主要是通過指令碼來定期檢查從庫與主庫複製的延遲度並發送郵件,供大家參考。

    有關pt-heartbeat工具的安裝可以參考:percona-toolkit的安裝及簡介
    有關pt-heartbeat工具的介紹可以參考:使用pt-heartbeat監控主從複寫延遲

 

1、指令碼概述
   a、指令碼定期使用--check方式單次檢查當前的延遲性(週期性方式可以使用cron job比如每1分鐘或5分鐘)
   b、通過設定指定的延遲閥值來判斷當時的延遲性是否在可控範圍
   c、一旦當前的延遲大於指定閥值,則馬上使用--monitor方式不停的監控其延遲性並寫入到記錄檔
   d、對於--monitor方式,其進程運行超過30分鐘,自kill其進程,以避免無限期運行導致日誌過大,空間不夠用

 

2、指令碼內容 

[mysql@SZDB run]$ more ck_slave_lag.sh #!/bin/bash#set -xif [ $# -ne 3 ];then      echo "usage:"      echo "ck_slave_lag.sh <Servier-id> <MaxLag> <LogDir>"      exit 0;fi# Author : Leshami# Blog   : http://blog.csdn.net/leshamiServerID=$1MaxLag=$2LogDir=$3Timestamp=`date +%Y%m%d_%H%M%S`Rentition=7LogFile=$LogDir/slave_lag_$Timestamp.logLagDetail=$LogDir/slave_lag_Detail_$Timestamp.logmailadd=leshami@12306.cnecho $ServerIDecho $MaxLagecho $LogDirecho $LogFileecho $LagDetailecho $mailaddif [ ! -d $LogDir ];then    mkdir -p $LogDirfiLag=`/usr/bin/pt-heartbeat --user=monitor --password=xxx -S /tmp/mysql.sock -D test --master-server-id=$ServerID --check`Lag=`echo ${Lag%.*}`#Lag=3echo $LagptStatus=`ps -ef|grep pt-heart|grep daemonize`echo $ptStatusif [ $Lag -gt $MaxLag ]; then    echo "The current date is `date` at `hostname`."          >>$LogFile     echo "The current lag log file is $LogFile."              >>$LogFile    echo "The current replication lag is $Lag."               >>$LogFile    echo "The replication lag is larger than max lag $MaxLag." >>$LogFile       if [ -z "$ptStatus" ] ; then        echo "Start a monitor daemon with below command: "        >>$LogFile         echo "pt-heartbeat --user=monitor --password=xxx -S /tmp/mysql.sock -D test " >>$LogFile        echo " --master-server-id=11 --monitor --print-master-server-id --daemonize --log=$LagDetail" >>$LogFile        /usr/bin/pt-heartbeat --user=monitor --password=xxx -S /tmp/mysql.sock -D test \        --master-server-id=$ServerID --monitor --print-master-server-id --daemonize --log=$LagDetail        echo "More detail please check lag log from $LagDetail." >>$LogFile        cat $LogFile | mutt -s "Found slave lag on `hostname`." $mailadd    fifiif [ -n "$ptStatus" ] ; then    STime=`ps -ef|grep pt-heart|grep daemonize |gawk '{print $5}'`    Pid=`ps -ef|grep pt-heart|grep daemonize |gawk '{print $2}'`    STime=`date '+%Y%m%d'`" "$STime    s_STime=`date -d "$STime" '+%s'`    s_ETime=`date +%s`    DiffSec=`expr $s_ETime - $s_STime`    echo $STime    echo $s_STime    echo $s_ETime    echo $DiffSec    if [ "$DiffSec" -gt 1800 ]; then       echo "kill -9 $Pid"       kill -9 $Pid    fifi# Remove history slave lag log.find $LogDir -name "*slave_lag*" -ctime +$Rentition -delete exit

3、部署參考

[mysql@SZDB run]$ crontab -l#check slave lag*/1 * * * * /run/ck_slave_lag.sh 11 3 /log/SlaveLag

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.