如何设置Monit以监控MariaDB复制?

j8yoct9x  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(132)

我在我的Debian 11服务器上使用Monit 5.27.2和MariaDB 10.5.15。
我的服务器设置在Multi-Source MariaDB Replication中。
有时,复制停止,我没有得到通知。我还没有在任何地方看到准备好的配置,让Monit发送这样的警报。
通过以下查询可以看到这些错误:

SHOW SLAVE STATUS;

其中那些价值观特别有趣:

我还没有找到任何方法来使用Monit直接监视SQL语句的结果。但是,似乎可以监视Monit 5.29.0之前版本的return code of a command(以及之后的命令输出)。
我可能会准备一个Bash/Shell脚本来检查SQL语句的输出,然后准备一个Monit配置来在出现问题时向我发送警报。如果我这样做了,我会在这里发布这个作为答案。我只是想知道是否已经有了一个已知的解决方案。
我希望这能与MySQL一起工作。
我打算使用以下SQL查询对此进行测试:

STOP SLAVE;
START SLAVE;
mwecs4sa

mwecs4sa1#

我让这个按我预期的那样工作。我在复制的所有服务器源上都这样做了。

正在安装

下面是文件monit_replication.sh


# !/bin/bash

user="YOUR_USER"
password="YOUR_PASSWORD"

result=`echo "show slave status"|mysql -EsB --user="$user" --password="$password"`

contains_with_yes(){
        if echo "$1"|grep -i "$2"|grep -i "Yes" > /dev/null; then
                echo "$2 ok"
        else
                echo "$2 not ok"
                exit 1
        fi
}

contains_with_yes "$result" "Slave_IO_Running"
contains_with_yes "$result" "Slave_SQL_Running"
exit 0

使此文件成为可执行文件:

chmod 700 /YOUR_PATH_TO_THE_FILE/monit_replication.sh

添加到/etc/monit/conf-enabled/mysql

check program MySQL_replication with path "/YOUR_PATH_TO_THE_FILE/monit_replication.sh"
   every 120 cycles
   if status > 0 then alert
   group mysql

测试

已运行此SQL:

STOP SLAVE;

已重新启动监视器:

/etc/init.d/monit restart

/var/log/monit.log中找到:

[2022-07-04T10:46:55+0000] error    : 'MySQL_replication' status failed (1) -- Slave_IO_Running not ok

收到此电子邮件:

monit alert -- Status failed MySQL_replication

Status failed Service MySQL_replication

    Date:        Mon, 04 Jul 2022 10:46:55
    Action:      alert
    Host:        YOUR_HOST
    Description: status failed (1) -- Slave_IO_Running not ok

Your faithful employee,
Monit

这将验证测试。使用此SQL查询还原复制:

START SLAVE;

其他解决方案

如果有任何基于Monit的更简单的解决方案可以提醒我MariaDB上的复制失败,我将很乐意接受它作为这个问题的解决方案。

相关问题