zabbix监控MySQL主从状态

环境要求:
系统平台 IP 安装的服务
redhat7 192.168.225.128 MySQL
redhat7 192.168.225.129 MySQL zabbix_agentd
redhat7 192.168.225.130 lamp架构 zabbix
  • 在192.168.225.128上做主数据库,在192.168.225.128上做从数据库,在192.168.225.130上搭建lamp架构并安装zabbix。
  • lamp架构
  • zabbix部署
  • MySQL主从
在从数据库上安装zabbix_agentd
[root@localhost ~]# yum -y install net-snmp-devel libevent-devel gcc gcc-c++ wget
[root@localhost ~]# cd /usr/src/
[root@localhost ~]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.4.12/zabbix-3.4.12.tar.gz
[root@localhost src]#  tar -xf zabbix-3.4.12.tar.gz
[root@localhost src]# cd zabbix-3.4.12/
[root@localhost zabbix-3.4.12]# ./configure --enable-agent
[root@localhost zabbix-3.4.12]# make install
[root@localhost ~]# groupadd -r zabbix
[root@localhost ~]# useradd -r -M -s /sbin/nologin -g zabbix zabbix
[root@localhost ~]# zabbix_agentd
[root@localhost ~]# ss -antl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128          *:22                       *:*                  
LISTEN     0      100    127.0.0.1:25                       *:*                  
LISTEN     0      128          *:10050                    *:*                  
LISTEN     0      128         :::22                      :::*                  
LISTEN     0      100        ::1:25                      :::*                  
LISTEN     0      80          :::3306                    :::*     

//修改配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf

Server=192.128.225.130
ServerActive=192.168.225.130
Hostname=192.168.225.129

192.168.225.129

  • 写监控MySQL主从状态的脚本
[root@localhost scripts]# vim zhucongsql.sh
#!/bin/bash
A=$(mysql -e'show slave status \G' |grep -i 'running:' |awk '{print $2}' |head -1)

if [ "$A" == "Yes" ];then
    echo "0"
else
    echo "1"
fi

[root@localhost scripts]# vim zhucongsql2.sh 
#!/bin/bash

B=$(mysql -e'show slave status \G' |grep -i 'running:' |awk '{print $2}' |tail -1)
if [ "$B" == "Yes" ];then
    echo "0"
else
    echo "1"
fi
//修改客户端zabbix配置文件
[root@localhost ~]# vim /usr/local/etc/zabbix_agentd.conf
//修改如下配置
UnsafeUserParameters=1
UserParameter=check_mysql_IO,/bin/bash /scripts/zhucongsql.sh
UserParameter=check_mysql_SQL,/bin/bash /scripts/zhucongsql2.sh

在客户端测试脚本

[root@localhost ~]# zabbix_get -s 192.168.225.129 -k check_mysql_IO
0
[root@localhost ~]# zabbix_get -s 192.168.225.129 -k check_mysql_SQL
0

配置web界面
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
再将数据库的主从停掉,触发报警
192.168.225.128

[root@localhost ~]# mysql -uroot -phxd123456. -e'stop slave;'
mysql: [Warning] Using a password on the command line interface can be insecure.

在这里插入图片描述

再将MySQL主从开启
192.168.225.129

[root@localhost ~]# mysql -uroot -phxd123456. -e'start slave;'
mysql: [Warning] Using a password on the command line interface can be insecure.

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/dubaiToT/article/details/83931209