zabbix监控mysql replication

mysql的 replication复制使用了三个线程,Binlog Dump(主)、IO线程及SQL线程(从)。其中只要监控IO线程及SQL线程这两个线程就可以确定复制是否出现问题了。
复制正常情况下,Slave_IO_Running及Slave_SQL_Running都应该是Yes,见下图:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.166
Master_User: hzqm
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 98
Relay_Log_File: mysqld-relay-bin.000294
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000007
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
  Replicate_Do_DB: Replicate_Ignore_DB: 
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 98
Relay_Log_Space: 235
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
1 row in set (0.00 sec)

编写脚本检查Slave_IO_Running和Slave_SQL_Running的状态(很简单)
vi /usr/bin/check_mysql_slave
#!/bin/sh
declare -a slave_is
slave_is=($(/usr/bin/mysql -uroot -pquanmai -e "show slave status\G"|grep Running |awk '{print $2}'))
if [ "${slave_is[0]}" = "Yes" -a "${slave_is[1]}" = "Yes" ]
then
echo "OK -slave is running"
exit 0
else
echo "Critical -slave is error"
exit 2
fi
添加配置文件
[[email protected] bin]# vi /etc/zabbix/zabbix_agentd.conf
UserParameter=mysql.slave,/usr/bin/check_mysql_slave |grep OK -c
(可执行/usr/bin/check_mysql_slave |grep OK -c 查看正常返回结果为1,则根据此条件设置触发器)

添加item和trigger

zabbix监控mysql replication
添加触发器:
zabbix监控mysql replication

添加active:
zabbix监控mysql replication

测试完成!