基于搭建mysql主从,在客户端操作
1,创建监控mysql主从状态脚本,并创建授权用户
[root@localhost ~] cd /etc/zabbix/scripts/
[root@localhost scripts] vim check_mysql.sh
[root@localhost scripts] cat check_mysql.sh
#!/bin/bash
case $1 in
slave_status)
mysql -ucheck -p123 -e "show slave status \G"|grep "Running"|grep "Yes"|awk '{print $2}'|wc -l
;;
esac
#在slave库创建授权用户
MariaDB [(none)]> grant all on *.* to 'check'@'localhost' identified by '123';
MariaDB [(none)]> flush privileges;
2,编辑客户端配置文件,并重启
[root@localhost scripts] cd /etc/zabbix/zabbix_agentd.d/
[root@localhost zabbix_agentd.d] vim check_mysql.conf
[root@localhost zabbix_agentd.d] cat check_mysql.conf
UserParameter=mysql_status[*],/etc/zabbix/scripts/check_mysql.sh $1
[root@localhost zabbix_agentd.d] systemctl restart zabbix-agent
3,在服务端测试
[root@localhost ~] zabbix_get -s 192.168.59.112 -k mysql_status[slave_status]
2
#返回值为2,是正常状态
4,在web端设置监控
5,宕掉slave,会触发报警
6,添加图形
二,流量监控
1,过滤出监控内容,并写进脚本
[root@localhost scripts] mysqladmin extended-status |grep "Bytes_sent"|awk '{print $4}'
3969067
[root@localhost scripts] mysqladmin extended-status |grep "Bytes_received"|awk '{print $4}'
8960924
[root@localhost scripts] vim check_mysql.sh
[root@localhost scripts] cat check_mysql.sh
#!/bin/bash
case $1 in
slave_status)
mysql -ucheck -p123 -e "show slave status \G"|grep "Running"|grep "Yes"|awk '{print $2}'|wc -l
;;
Bytes_sent) #发送流量
mysqladmin extended-status |grep "Bytes_sent"|awk '{print $4}'
;;
Bytes_received) #接受流量
mysqladmin extended-status |grep "Bytes_received"|awk '{print $4}'
;;
esac
2,在服务端设置监控
三,监控增删改查
1,将监控项写进脚本
[root@localhost scripts] vim check_mysql.sh
[root@localhost scripts] cat check_mysql.sh
#!/bin/bash
case $1 in
slave_status)
mysql -ucheck -p123 -e "show slave status \G"|grep "Running"|grep "Yes"|awk '{print $2}'|wc -l
;;
Bytes_sent) #发送流量
mysqladmin extended-status |grep "Bytes_sent"|awk '{print $4}'
;;
Bytes_received) #接受流量
mysqladmin extended-status |grep "Bytes_received"|awk '{print $4}'
;;
Com_insert) #增加插入数据
mysqladmin extended-status |grep -w "Com_insert"|awk '{print $4}'
;;
Com_delete) #删除数据
mysqladmin extended-status |grep -w "Com_delete"|awk '{print $4}'
;;
Com_update) #修改数据
mysqladmin extended-status |grep -w "Com_update"|awk '{print $4}'
;;
Com_select) #查看数据
mysqladmin extended-status |grep -w "Com_select"|awk '{print $4}'
;;
Com_commit) #数据回滚
mysqladmin extended-status |grep -w "Com_commit"|awk '{print $4}'
;;
Com_rollback) #数据提交
mysqladmin extended-status |grep -w "Com_rollback"|awk '{print $4}'
;;
esac
2,在服务端设置