码迷,mamicode.com
首页 > 数据库 > 详细

Linux运维之Shell编程------(一)监控MySQL错误码及主从复制同步异常

时间:2018-08-10 01:26:04      阅读:245      评论:0      收藏:0      [点我收藏+]

标签:使用   mail -s   tin   短信   %s   email   ase   user   dmi   

Linux运维之Shell编程

一、监控MySQL错误码及主从复制同步异常

题目:监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员。提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:
阶段1:开发一个守护进程脚本每30秒实现检测一次。
阶段2:如果同步出现如下错误号(1158,1159,1008,1007,1062),则跳过错误。
阶段3:请使用数组技术实现上述脚本(获取主从判断及错误号部分)

 

阶段2:

#!/bin/bash
#
CONTACT=‘root@node77‘
ERR_LOG=‘/var/log/mariadb/mariadb.log‘

email_to_admin() {
  mailsubject="MySQL Error."
  mailbody="`date ‘+%F %H:%M:%S‘`: MySQL Error,please check and repair."
  echo $mailbody | mail -s "$mailsubject" $CONTACT
}

while true;do
  for ERROR_CODE in `grep ‘Error_code: [[:digit:]]\+‘ $ERR_LOG | grep -o ‘[[:digit:]]\+‘` ;do
    case $ERROR_CODE in
    1007)
      continue
      ;;
    1008)
      continue
      ;;
    1158)
      continue
      ;;
    1159)
      continue
      ;;
    1162)
      continue
      ;;
    *) ;;
    esac
    email_to_admin
  done
  sleep 30
done

 

阶段3:

#!/bin/bash
#
DB_USER=‘root‘
DB_PASSWORD=‘jingpei‘
DB_PORT=‘3306‘
MASTER=‘192.168.0.76‘
SLAVE=‘192.168.0.77‘
CMD_CONN_MASTER=‘mysql -u$DB_USER -p$DB_PASSWORD -P$DB_PORT -h$MASTER‘
CMD_CONN_SLAVE=‘mysql -u$DB_USER -p$DB_PASSWORD‘ -P$DB_PORT -h$SLAVE‘
CONTACT=‘root@node77‘

declare -a master_status
master_status[0]="CMD_CONN_MASTER -e ‘show master status\G‘ | egrep File | cut -d: -f2"
master_status[1]="CMD_CONN_MASTER -e ‘show master status\G‘ | egrep Position | cut -d: -f2"

declare -a slave_status
slave_status[0]="CMD_CONN_SLAVE -e ‘show slave status\G‘ | egrep Master_Log_File | cut -d: -f2"
slave_status[1]="CMD_CONN_SLAVE -e ‘show slave status\G‘ | egrep Read_Master_Log_Pos | cut -d: -f2"
slave_status[2]="CMD_CONN_SLAVE -e ‘show slave status\G‘ | egrep Last_Errno | cut -d: -f2"

email_to_admin() {
  mailsubject="Syncing failed."
  mailbody="`date ‘+%F %H:%M:%S‘`: Syncing of master-slave is failed,please check and repair."
  echo $mailbody | mail -s "$mailsubject" $CONTACT
}

while true ; do
  if [ ${master_status[0]} == ${slave_status[0]} -a ${master_status[1]} == ${slave_status[1]} -a ${slave_status[2]} == 0 ]; then
    echo "Syncing of master-slave is OK."
  else
    echo "Syncing of master-slave is failed."
    email_to_admin
  fi
  sleep 30
done

Linux运维之Shell编程------(一)监控MySQL错误码及主从复制同步异常

标签:使用   mail -s   tin   短信   %s   email   ase   user   dmi   

原文地址:https://www.cnblogs.com/cured/p/9452401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!