自定义监控MySQL主从状态与主从延迟

自定义监控mysql主从

写脚本,脚本需要在从库上监控

[root@slave scripts]# cat check_mysql.sh 
#!/bin/bash
pass=1
count=$(mysql -uroot -p$pass -e "show slave status \G;" 2>/dev/null | grep -E 'agent_IO_Running: Yes|Slave_SQL_Running: Yes' | grep -c Yes)
if [ $count -eq 2 ];then
    echo '0'
else    
    echo '1'
fi 

修改客户端的配置文件也就是从库的配置

[root@slave scripts]# vim /usr/local/etc/zabbix_agentd.conf
 UnsafeUserParameters=1  //取消注释将0改为1
UserParameter=check_mysql,/scripts/check_mysql.sh

手动触发并验证

[root@slave scripts]# ./check_mysql.sh 
0

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.170.129
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000005
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql_relay.000018
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql_bin.000005
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.170.129
                  Master_User: test
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql_bin.000005
          Read_Master_Log_Pos: 154
               Relay_Log_File: mysql_relay.000018
                Relay_Log_Pos: 367
        Relay_Master_Log_File: mysql_bin.000005
             Slave_IO_Running: No
            Slave_SQL_Running: No

[root@slave scripts]# ./check_mysql.sh 
1

[root@zabbix ~]# zabbix_get -s 192.168.170.129 -k check_mysql
1

//修改zabbix文件
[root@slave scripts]# vim /usr/local/etc/zabbix_agentd.conf        //在此文件下添加下面这行
UserParameter=check_mysqlmd,/bin/bash /scripts/mysqlmd.sh
//重启服务
[root@slave scripts]# pkill zabbix
[root@slave scripts]# zabbix_agentd 

测试脚本

//在服务端上面执行
[root@zabbix ~]# zabbix_get -s 192.168.170.129 -k check_mysqlmd
0



配置主从延迟

编写脚本

[root@slave ~]# cd /scripts/
[root@slave scripts]# vim mysql_delay.sh 
[root@slave scripts]# cat mysql_delay.sh 
#!/bin/bash

delay=$(mysql -uroot -pRuntime123! -e 'show slave status\G' 2> /dev/null| grep 'Seconds_Behind_Master' | awk '{print $2}')

echo $delay
[root@slave scripts]# chmod +x mysql_delay.sh       //添加执行权限

配置zabbix文件

[root@slave scripts]# vim /usr/local/etc/zabbix_agentd.conf    //添加下面这行
UserParameter=check_mysql_delay,/bin/bash /scripts/mysql_delay.sh
//重启zabbix服务
[root@slave scripts]# pkill zabbix
[root@slave scripts]# zabbix_agentd 

测试脚本

[root@slave scripts]# ./mysql_delay.sh        
0
[root@zabbix ~]# zabbix_get -s 192.168.170.129 -k check_mysql_delay        
0

配置监控项

posted @ 2022-07-12 01:22  夏天的海  阅读(67)  评论(0)    收藏  举报