Shell脚本范例(九)——MySQL主从复制异常监控

本文分享常用的一些Shell脚本的例子,这是第九篇——MySQL主从复制异常监控。

要求:开发一个守护进程脚本,每30秒监控一次MySQL主从复制是否异常(包括不同步及延迟),如果异常,则发送短信并发送邮件给管理员存档。

参考:

#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

count=0
status=($(awk -F ':' '/_Running|_Behind/{print $NF}' slave.log))

for((i=0;i<${#status[*]};i++))
do
    if [ "${status[${i}]}" != "Yes" -a "${status[${i}]}" != "0" ]
    then
        let count+=1
    fi
done

if [ $count -ne 0 ]
then
    echo "mysql replcation is faild"
else
    echo "mysql replcation is success"
fi
#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

function CheckDB(){
    count=0
    status=($(awk -F ':' '/_Running|_Behind/{print $NF}' slave.log))

    for((i=0;i<${#status[*]};i++))
    do
        if [ "${status[${i}]}" != "Yes" -a "${status[${i}]}" != "0" ]
        then
            let count+=1
        fi
    done

    if [ $count -ne 0 ]
    then
        echo "mysql replcation is faild"
        return 1
    else
        echo "mysql replcation is success"
        return 0
    fi
}

function main(){
    while true
    do
        CheckDB
        sleep 30
    done
}

main
#!/bin/bash
#Author: Oliver King
#Blog: http://www.oliver.ren

path=/server/scripts
MAIL_GROUP="oliver.csu@gmail.com"
PAGER_GROUP="18888888888 18666666666"
LOG_FILE="/tmp/web_check.log"
USER=root
PASSWORD=password
PORT=3307
MYSQLCMD="mysql -u$USER -p$PASSWORD -S /data/$PORT/mysql.sock"

error=(1008 1007 1062)
RETVAL=0

[ ! -d "$path" ] && mkdir -p $path

function JudgeError(){
    for((i=0;i<${#error[*]};i++))
    do
        if [ "$1" == "${error[$i]}" ]
        then
            echo "MySQL slave errorno is $1, auto repairing it."
            $MYSQLCMD -e "stop slave;set global sql_slave_skip_counter=1;start slave;"
        fi
    done
    return $1
}

function CheckDb(){
    status=($(awk -F ':' '/_Running|Last_Errno|_Behind/{print $NF}' slave.log))
    expr ${status[3]} + 1 &> /dev/null
    if [ $? -ne 0 ]
    then
        status[3]=300
    fi
    if [ "${status[0]}" == "Yes" -a "${status[1]}" == "Yes" -a "{status[3]} -lt 120 ]
    then
       return 0
   else
      JudgeError ${status[2]}
  fi 
}

function MAIL(){
    local SUBJECT_CONTENT=$1
    for MAIL_USER in `echo $MAIL_GROUP`
    do
        mail -s "$SUBJECT_CONTENT " $MAIL_USER < $LOG_FILE
    done
}

function PAGER(){
    for PAGER_USER in `echo $PAGER_GROUP`
    do
        TITLE=$1
        CONTACT=$PAGER_USER
        HTTPGW=http://oliver.sms.cn/smsproxy/sendsms.action
        curl -d cdkey=5ADF-EFA -d password=OLDBOY -d phone=$CONTACT -d message="$TITLE[$2]" $HTTPGW
    done    
}

function SendMsg(){
    if [ $1 -ne 0 ]
    then
        RETVAL=1
        NOW_TIME=`date +"%Y-%m-%d %H:%M:%S"`
        SUBJECT_CONTENT="mysql slave is error, errorno is $2, ${NOW_TIME}."
        echo -e "$SUBJECT_CONTENT"|tee $LOG_FILE
        MAIL $SUBJECT_CONTENT
        PAGER $SUBJECT_CONTENT $NOW_TIME
    else
        echo "MySQL slave status is ok"
        RETVAL=0
    fi
    return $RETVAL
}

function main(){
    while true
    do
        CheckDb
        SendMsg $?
        sleep 30
    done
}

main

标签: none