zabbix自定义监控MySQL主从状态和延迟
1.监控MySQL主从状态
//首先得部署mysql主从复制,步骤省略
//创建脚本
[root@web scripts]# cat check_mysql.sh
#!/bin/bash
count=$(mysql -uroot -p123 -e "show slave status\G" |grep -v grep |grep -c 'Yes')
if [ $count -ne 2 ];then
echo '1'
else
echo '0'
fi
//在agent端编辑配置文件
[root@web scripts]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=check_mysql[*],/bin/bash /scripts/check_mysql.sh
//重启服务
[root@web scripts]# pkill zabbix_agentd
[root@web scripts]# zabbix_agentd
//在服务端去测试
[root@server ~]# zabbix_get -s 192.168.159.102 -k check_mysql
0
//添加监控项
//添加触发器
//手动添加动作,测试主从异常
//主库开启防火墙
[root@master ~]# systemctl start firewalld.service
//重启主从
MariaDB [(none)]> stop slave;
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]> start slave;
Query OK, 0 rows affected (0.001 sec)
2.监控mysql延迟状态
//创建一个脚本
#!/bin/bash
count=`mysql -uroot -p123 -e "show slave status\G" 2> /dev/null |awk '/Seconds_Behind_Master/ {print $2}' `
if [ $count -ne 0 ];then
echo '1'
else
echo '0'
fi
[root@web scripts]# chmod +x check_mysqlyc.sh
//编辑agent配置文件
[root@web scripts]# vim /usr/local/etc/zabbix_agentd.conf
UnsafeUserParameters=1
UserParameter=check_mysqlyc[*],/bin/bash /sceipts/check_mysqlyc.sh
//重启服务
[root@web scripts]# pkill zabbix_agentd
[root@web scripts]# zabbix_agentd
//去服务端测试
[root@server ~]# zabbix_get -s 192.168.159.102 -k check_mysqlyc
0
//添加监控项
//添加触发器