zabbix監控MySQL主從狀態
阿新 • • 發佈:2018-12-16
環境要求:
系統平臺 | 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
[[email protected] ~]# yum -y install net-snmp-devel libevent-devel gcc gcc-c++ wget
[[email protected] ~]# cd /usr/src/
[[email protected] ~]# wget https://nchc.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.4.12/zabbix-3.4.12.tar.gz
[ [email protected] src]# tar -xf zabbix-3.4.12.tar.gz
[[email protected] src]# cd zabbix-3.4.12/
[[email protected] zabbix-3.4.12]# ./configure --enable-agent
[[email protected] zabbix-3.4.12]# make install
[[email protected] ~]# groupadd -r zabbix
[[email protected] ~]# useradd -r -M -s /sbin/nologin -g zabbix zabbix
[ [email protected] ~]# zabbix_agentd
[[email protected] ~]# 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 :::*
//修改配置檔案
[[email protected] ~]# 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主從狀態的指令碼
[[email protected] 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
[[email protected] 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配置檔案
[[email protected] ~]# 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
在客戶端測試指令碼
[[email protected] ~]# zabbix_get -s 192.168.225.129 -k check_mysql_IO
0
[[email protected] ~]# zabbix_get -s 192.168.225.129 -k check_mysql_SQL
0
配置web介面
再將資料庫的主從停掉,觸發報警
192.168.225.128
[[email protected] ~]# mysql -uroot -phxd123456. -e'stop slave;'
mysql: [Warning] Using a password on the command line interface can be insecure.
再將MySQL主從開啟
192.168.225.129
[[email protected] ~]# mysql -uroot -phxd123456. -e'start slave;'
mysql: [Warning] Using a password on the command line interface can be insecure.