1. 程式人生 > >Mysql主從狀態監控腳本

Mysql主從狀態監控腳本

mysql主從監控腳本

目標:對Mysql從庫進行狀態監控,如果從庫復制異常就告警

思路:

1、首先查找Mysql從庫的4個參數值(show slave status根據這個命令);分別是從庫的IO線程、SQL線程、復制延遲時間(這個在以下腳本沒用到,可以再寫個腳本對其延遲進行告警)、錯誤碼

2、函數Mysql_status判斷,對IO線程、SQL線程、錯誤碼三者邏輯與判斷

3、如果為假則執行函數Mysql_Errno

4、對於mysql錯誤碼為1158 1159 1008 1007 1062 我們可以進行停止slave,跳過當前復制條數,進行下一條復制


[[email protected] script]# cat mysql_check.sh

#!/bin/bash

#

#2017.5.8

#mysql狀態監測告警

. /etc/init.d/functions

Mysql_IO_cmd=`mysql -h 10.222.1.5 -uroot -p123456 -e ‘show slave status\G‘|grep Slave_IO_Running |awk ‘{print $NF}‘`

Mysql_SQL_cmd=`mysql -h 10.222.1.5 -uroot -p123456 -e ‘show slave status\G‘|grep Slave_SQL_Running |awk ‘{print $NF}‘`

Mysql_Be_cmd=`mysql -h 10.222.1.5 -uroot -p123456 -e ‘show slave status\G‘|grep Seconds_Behind_Master |awk ‘{print $NF}‘`

Mysql_Errno_cmd=`mysql -h 10.222.1.5 -uroot -p123456 -e ‘show slave status\G‘|grep Last_SQL_Errno |awk ‘{print $NF}‘`

ErrorNo=(1158 1159 1008 1007 1062)

##判斷mysql復制的錯誤碼

Mysql_Errno() {

for sum in $ErrorNo

do

if [ "$Mysql_Errno_cmd" == "$sum" ];then

echo "$(date +%F_%R)錯誤狀態碼:$Mysql_Errno_cmd" >> /tmp/mysql_error.log

mysql -h 10.222.1.5 -uroot -p123456 -e ‘stop slave;set global sql_slave_skip_counter=1;start slave;‘

echo `sort -r /tmp/mysql_error.log` |mail -s "MySQL Slave is error" [email protected]

else

echo "$(date +%F_%R)錯誤狀態碼:$Mysql_Errno_cmd" >> /tmp/mysql_error.log

#echo `sort -r /tmp/mysql_error.log` |mail -s "MySQL Slave is error" [email protected]

uniq /tmp/mysql_error.log |mail -s "MySQL Slave is error" [email protected]

fi

done

}

##監測mysql狀態函數

Mysql_status() {

if [ "$Mysql_IO_cmd" == "Yes" -a "$Mysql_SQL_cmd" == "Yes" -a "$Mysql_Errno_cmd" == "0" ];then

echo

action "mysql status" /bin/true

else

echo

action "mysql status" /bin/false

Mysql_Errno

fi

}

Mysql_status


本文出自 “常想一二” 博客,請務必保留此出處http://250919938.blog.51cto.com/962010/1923784

Mysql主從狀態監控腳本