Home  >  Article  >  System Tutorial  >  Master teaches you to automatically discover and monitor the status of mysql slave database

Master teaches you to automatically discover and monitor the status of mysql slave database

WBOY
WBOYOriginal
2024-08-13 11:40:251143browse

The status of mysql slave database mainly monitors three values, namely Slave SQL Running, Slave IO Running and Seconds Behind Master. Only when Slave SQL Running, Slave IO Running is yes, and then Seconds Behind Master is 0, can the slave database run in a normal state (sometimes if these three values ​​meet the requirements, there will be problems with the slave database data. ). Let’s take a look at the monitoring effect first:

Master teaches you to automatically discover and monitor the status of mysql slave database

Since it is to automatically discover and monitor the status of the mysql slave library, first paste the configuration of the automatic discovery rules, use mysql.slave.discover to obtain the port of the mysql slave library, install zabbix agentd in the slave library, and add the configuration (UserParameter= mysql.slave.discover,python /usr/local/zabbix/discover_mysql_slave.py):

Master teaches you to automatically discover and monitor the status of mysql slave database

Among them, the python script for automatically discovering the mysql port can be found at: https://my.oschina.net/zhuangweihong/blog/785919 This article has a similar script. After automatically discovering the mysql port, the detection project prototype used is as follows. Three prototypes are used to obtain the three values ​​​​that need to be monitored from the mysql slave library:

Master teaches you to automatically discover and monitor the status of mysql slave database

The key values ​​obtained need to be added to the configuration from the library agent, and then restart agentd:

UserParameter=mysql.slave.status[*],sh /usr/local/zabbix/check_mysql_slave.sh -u xxx-p xxx -P $1 -k $2 2>/dev/null

Finally, take a look at the /usr/local/zabbix/check_mysql_slave.sh script, as follows:

#!/bin/sh
while getopts "u:p:P:k:" opt
do
        case $opt in
                u ) user=$OPTARG;;
                p ) password=$OPTARG;;
                P ) port=$OPTARG;;
                k ) key=$OPTARG;;
                ? )
                echo 'parameter is wrong!'
                exit 1;;
        esac
done
if [ ! "${user}" ] || [ ! "${password}" ] || [ ! "${port}" ] || [ ! "${key}" ];then
        echo "parameter is null"        
        exit 1
fi

mysql -u ${user} -p${password} -h 127.0.0.1 -P${port} -e "show slave status\G"|grep "${key}\:"|awk '{pr

The above is the detailed content of Master teaches you to automatically discover and monitor the status of mysql slave database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn