mysql replication环境检查脚本

author:skate
time:2013/05/13

 

mysql replication环境检查脚本

 

此脚本部署在slave端,可以通过短信或邮件监控slave的mysqld健康,replication环境健康,及复制延迟。

 

脚本:mysql_rep_moni.sh

#!/bin/bash
### author: skate
### time: 2013-05-14
### function: This script detects the mysqld health, replicaton health, replication latency


#variables
pwd=pwd
dir=/mysql/script/
ipaddr=1.1.1.1

###Determine whether there is connection
mysql_status=`netstat -nl | awk 'NR>2{if ($4 ~ /.*:3306/) {print "Yes";exit 0}}'`
  if [ "$mysql_status" == "Yes" ] ; then
        slave_status=`mysql -uroot -p$pwd -e "show slave status\G" | grep "Running" | awk '{if ($2 != "Yes") {print "No";exit 1}}'`
        slave_delay=`mysql -uroot -p$pwd -e "show slave status\G" | grep "Seconds_Behind_Master" | awk '{if ($2 >=0) {print $2 ;exit
 0}}'`
       echo "delay"$slave_delay
       echo "status"$slave_status
###replication is not working
         if [ "$slave_status" == "No" ];then
             echo "slave is not working!"
             sh "$dir"notify.sh "phone" "$ipaddr""_warn_zxg_rep" "$ipaddr""_mysql_replication_is_not_working" "dba"
             sh "$dir"notify.sh "email" "$ipaddr""_error_zxg_rep" "mysql_replication_is_not_working" "dba"
###replication is working,but replication have delay
        elif [ "$slave_status" != "No" ] && [ $slave_delay -ge 10 ] ; then
             echo "slave is working,but replication have delay!"
             sh "$dir"notify.sh "phone" "$ipaddr""_warn_zxg_rep" "$ipaddr""_mysql_replication_is_delay:$slave_delay" "dba"
             sh "$dir"notify.sh "email" "$ipaddr""_warn_zxg_rep" "mysql_replication_is_delay:$slave_delay" "dba"
        else
             echo "slave is working."
        fi
  else
       echo "mysqld is down!"
       sh "$dir"notify.sh "email" "$ipaddr""_error_zxg_mysqld" "$ipaddr""_mysqld_is_down" "dba" 
       sh "$dir"notify.sh "phone" "$ipaddr""_error_zxg_mysqld" "mysqld_is_down" "dba"
  fi

创建定时crontab
*/5     *       *       *       *       su - mysql -c "sh /mysql/script/mysql_rep_moni.sh> /dev/null 2>&1"


说明:
上面的报警API(notify.sh)可以根据自己的使用方式修改

 

------end------

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值