脚步监控mysql主从是否正常
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了脚步监控mysql主从是否正常相关的知识,希望对你有一定的参考价值。
mysql主从配置详见:http://chinawu.blog.51cto.com/10692884/1739327
主从正常状态是slave上的状态值:Slave_IO_Running:YES和Slave_SQL_Running:YES,当出现一个NO,表明mysql主从已破坏,但是,运维人员不可能随时进入mysql的slave上的状态值。所以需要一个脚步在规定时间内检测是否关系正常。
脚步大致内容:
#!/bin/bash
#Check MySQL_Slave Status
#MySQL version: 5.1.73
#2016-01-29
MySQL_PORT=`netstat -an|grep "LISTEN"|grep "3306"|awk -F ‘ ‘ ‘{print $4}‘|awk -F ‘:‘ ‘{print $2}‘` #注意:租后一个print $2,不一定适用所有主机,根据实际情况pring值
MySQL_IP=`ifconfig eth0 |grep "inet addr"|awk -F ‘:‘ ‘{print $2}‘|awk -F ‘ ‘ ‘{print $1}‘`
MySQL_Status=$(/usr/local/mysql/bin/mysql -S /tmp/mysql.socket -uroot -pfgjh123. -e "show slave status\G" |grep -i "running"|head -n2)
IO_env=`echo $MySQL_Status | grep Slave_IO_Running |awk ‘{print $2}‘`
SQL_env=`echo $MySQL_Status |grep Slave_SQL_Running |awk ‘{print $2}‘`
ToDay=`date +"%y-%m-%d %H:%M:%S"`
if [ "$MySQL_PORT" == "3306" ]
then
echo "###### $ToDay ######" >> /data/check_mysql_slave_ok.log
echo "MySQL Server is running !" >> /data/check_mysql_slave_ok.log
else
mail -s "Warn! Server:$MySQL_IP MySQL is Down" [email protected]
fi
if [ "$IO_env" = "Yes" -a "$SQL_env" = "Yes" ]
then
echo "###### $ToDay ######" >> /data/check_mysql_slave_ok.log
echo "Slave MySQL Server Slave_IO and Slave_SQL is running !" >> /data/check_mysql_slave_ok.log
else
echo "###### $ToDay ######" >> /data/check_mysql_slave_warn.log
echo "Slave is not running !" >> /data/check_mysql_slave_warn.log
echo "Slave is not running !" | mail -s "Warn! $MySQL_IP MySQL Slave is not running " [email protected]
fi
随后放入自动执行任务列表中,crontab -e
*/10 * * * * root /bin/sh /root/shell/Check_MySQL_Slave_Status.sh
每十分钟运行一次检测脚步(时间自定义,crontab五个参数请自习)
zabbix添加触发器,检测mysql主从,微信告警
[[email protected] scripts]# cat mysql-replication.sh
#!/bin/bash
mysql -uzabbix -e ‘show slave status\G‘ |grep -E "Slave_IO_Running|Slave_SQL_Running"|awk ‘{print $2}‘|grep -c Yes
[[email protected] scripts]#
[[email protected] etc]# tail -15 zabbix_agentd.conf
UserParameter=mysql.replication, /home/zabbix/scripts/mysql-replication.sh
UserParameter=mysql.status[*],/home/zabbix/scripts/chk_mysql.sh $1
以上是关于脚步监控mysql主从是否正常的主要内容,如果未能解决你的问题,请参考以下文章