#ZABBIX监控mysql主从状态
安装配置 | IP |
---|---|
zabbix-server | 192.168.89.130 |
mysql主端/zabbix-agentd | 192.168.89.135 |
mysql从端/zabbix-agentd | 192.168.89.129 |
1.YUM安装zabbix-agentd添加监控项(192.168.89.135和192.168.89.129都安装)
1.1关闭防火请和selinux
1.1.1关闭SeLinux
临时关闭:setenforce 0
永久关闭:vim /etc/selinux/config
SELINUX=disabled 将这一项的内容改成这样即可
1.1.2关闭防火请
1、临时关闭
systemctl stop firewalld.service
2、永久关闭
systemctl disable firewalld.service
1.2 安装zabbix-agentd
[root@localhost ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-2.el7.noarch.rpm 下载zabbix3.4版本的yum源安装包
[root@localhost ~]# yum install -y zabbix-server-mysql zabbix-get zabbix-web zabbix-web-mysql zabbix-agent zabbix-sender
1.3配置zabbix-agentd配置文件
[root@localhost ~]# cd /etc/zabbix/
[root@localhost zabbix]# ls
web zabbix_agentd.conf zabbix_agentd.d zabbix_server.conf
[root@localhost zabbix]# vim zabbix_agentd.conf 只需要修改下面三项的内容即可
···
# Default:
# Server=
Server=192.168.138.130
···
# Mandatory: no
# Default:
# ServerActive=
ServerActive=192.168.138.130 Server和ServerActive填写zabbix-server端的主机IP地址,这两个ip一样,只不过一个是主动模式,一个是被动模式
···
# Mandatory: no
# Default:
# Hostname=
Hostname=192.168.138.129 这里的Hostname必须和后面再zabbix的网页添加主机时的主机名一样,我这里用的是zabbix agentd的ip地址
···
1.4启动zabbix agentd
[root@localhost ~]# systemctl start zabbix-agent.service
[root@localhost ~]# ss -antl 我这里开启了ipv6,所以会有两个10050端口,只要启动ipv4的10050端口即可,这里不需要想server端启动那么多端口,只需要保证这一个端口的启动即可
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:10050 *:*
LISTEN 0 128 :::10050 :::*
2.zabbix监控mysql主从同步
部署了个mysql从数据库,需要时时监控这个从数据库的主从状态。可以通过从mysql上的zabbix执行show slave status命令获取。
**Slave_IO_Running和Slave_SQL_Running状态是否都为Yes来判断主从是否正常。 **
MySQL同步功能由3个线程(主端1个,从端2个)来实现。
也可以通过监控Read_Master_Log_Pos和Exec_Master_Log_Pos是否相等监控正常
2.1 通过命令获取主从状态(Slave_IO_Running和Slave_SQL_Running)
[root@localhost zabbix]# mysql -uroot -pwscl1996. -e 'show slave status\G'
mysql: [Warning] Using a password on the command line interface can be insecure.
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.89.135
Master_User: kong
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 154
Relay_Log_File: mysql-relay-bin.000006
Relay_Log_Pos: 367
Relay_Master_Log_File: mysql-bin.000002
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: 154
Relay_Log_Space: 740
···
提取关键字
[root@localhost zabbix]# mysql -uroot -pwscl1996. -e 'show slave status\G' |grep -E "Slave_IO_Running|Slave_SQL_Running"|awk '{print $2}'|grep -c Yes 如果返回值为2,就代表主从正常
2
2.2 通过命令获取主从状态(Read_Master_Log_Pos和Exec_Master_Log_Pos)
[root@localhost zabbix]# mysql -uroot -pwscl1996. -e 'show slave status\G' |grep -E "Read_Master_Log_Pos|Exec_Master_Log_Pos"|awk '{print $2}' 当输出的值相同时,就代表主从正常
154
154
2.3 书写脚本
[root@localhost zabbix]# vim mysql_Slave.sh
#!/bin/bash
Slave=$(mysql -uroot -pwscl1996. -e 'show slave status\G' 2>/dev/null |grep -E "Slave_IO_Running|Slave_SQL_Running"|awk '{print $2}'|grep -c Yes)
if [ "$Slave" -eq 2 ] ;then
echo 0
else
echo 1
fi
[root@localhost zabbix]# vim mysql_Log_Pos.sh
#!/bin/bash
Read_Master_Log_Pos=$(mysql -uroot -pwscl1996. -e 'show slave status\G' 2>/dev/null |grep -E "Read_Master_Log_Pos"|awk '{print $2}')
Exec_Master_Log_Pos=$(mysql -uroot -pwscl1996. -e 'show slave status\G' 2>/dev/null |grep -E "Exec_Master_Log_Pos"|awk '{print $2}')
if [ "$Read_Master_Log_Pos" -eq "$Exec_Master_Log_Pos" ] ;then
echo 0
else
echo 1
fi
[root@localhost ~]# chmod +x mysql_*
[root@localhost ~]# ./mysql_Log_Pos.sh 返回值为0即为正常
0
[root@localhost ~]# ./mysql_Slave.sh
0
2.4 修改mysql从端的zabbix配置文件(192.168.89.129)
[root@localhost zabbix]# vim /etc/zabbix/zabbix_agentd.conf
# UnsafeUserParameters=0
UnsafeUserParameters=1
### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined parameters.
# Format: UserParameter=<key>,<shell command>
# See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
# UserParameter=
UserParameter=check_mysql_Slave,/bin/bash /etc/zabbix/scripts/mysql_Slave.sh
UserParameter=check_mysql_Log_Pos,/bin/bash /etc/zabbix/mysql_Log_Pos.sh
2.5 重启服务(192.168.89.129)
[root@localhost ~]# killall zabbix_agentd
[root@localhost ~]# zabbix_agentd
2.6 在zabbix-server端测试(192.168.89.130)
[root@localhost ~]# zabbix_get -s 192.168.89.129 -k 'check_mysql_Log_Pos'
0
[root@localhost ~]# zabbix_get -s 192.168.89.129 -k 'check_mysql_Slave'
0
2.7 网页端设置
2.7.1 创建监控项
2.7.2 添加触发器
一共要添加两个监控
2.8 测试
关闭192.168.89.130端的mysql
[root@localhost ~]# systemctl stop mysql