1、环境准备,主从已经添加到zabbix主机里面
192.168.121.100 master.example.com master
192.168.100.40 slave.example.com slave
192.168.121.30 zabbix.example.com zabbix
两台都下载zabbix的客户端,前面有
再zabbix主机上
#vim /etc/hosts
192.168.121.100 master.example.com master
192.168.100.40 slave.example.com slave
#scp /etchosts root@192.168.121.100:/etc/hosts
#scp /etchosts root@192.168.121.40:/etc/hosts
在客户端master和slave上
#vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.121.40
ServerActive=192.168.121.40
Hostname=slave
#vim /etc/zabbix/zabbix_agentd.conf
Server=192.168.121.40
ServerActive=192.168.121.40
Hostname=master
#systemctl restart zabbix-agent.service
#systemctl enable zabbix-agent.service
创建脚本
在salve主机上
#mkdir /etc/zabbix/script
#vim mysql_slave_status.sh
#!/bin/bash
USER="root" //数据库的名字root
PASSWD="123" //数据库的密码
NAME=$1 //调用的函数io和sql
function IO {
Slave_IO_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_IO_Running |awk '{print $2}'`
if [ $Slave_IO_Running == "Connecting" ];then
echo 0
else
echo 1
fi
}
function SQL {
Slave_SQL_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_SQL_Running: |awk '{print $2}'`
if [ $Slave_SQL_Running == "Yes" ];then
echo 0
else
echo 1
fi
}
case $NAME in
io)
IO
;;
sql)
SQL
;;
*)
echo -e "Usage: $0 [io | sql]"
esac
#chmod +x mysql_slave_status.sh
#chown -R zabbix.zabbix /etc/zabbix/script/
#cd /etc/zabbix/zabbix_agentd.d/
#vim userparameter_mysql_slave.conf
#UserParameter=mysql_slave[*],/etc/zabbix/script/mysql_slave_status.sh $1
在服务端
#zabbix-get -s 192.168.121.40 -k mysql_slave[io]
#zabbix-get -s 192.168.121.40 -k mysql_slave[sql]