nagios监控mysql主从复制

说明:
可以通过nagios监控mysql主从复制是否正常

思路:
在mysql从服务器,通过执行 show slave status\G; 查看输出可以判定主从复制是否正常。以下为某个从服务器的输出

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.93.16
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 1752541
               Relay_Log_File: hy-mysql3-relay-bin.000088
                Relay_Log_Pos: 2339
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 

这个输出,最关键处就是”Slave_IO_Running: Yes“和“Slave_SQL_Running: Yes”,这两个值全是”Yes”就表明主从复制正常,否则就是有问题。

实现:
1、在主数据库服务器增加一个用户,给予较低的权限(直接在从库添加也可以)。之所以在主库建nagios用户,是因为有mysql主从,主库上新建的用户会自动同步到从库上。除了你主从排除mysql,不复制mysql外。

> grant Replication client on *.* to 'nagios'@'localhost' identified by 'nagios';    
> flush privileges;

 
2、登陆从服务器验证一下,看是否正常

# mysql -unagios -pnagios -e "show slave stutas\G"
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.2.15
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000252
          Read_Master_Log_Pos: 175211684
               Relay_Log_File: game-relay-bin.000091
                Relay_Log_Pos: 12907395
        Relay_Master_Log_File: mysql-bin.000252
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
其余省略。。。。

 
3、修改从服务器上的nrpe配置文件nrpe.cfg

# vim /usr/local/nagios/etc/nrpe.cfg    //添加以下内容
command[check_mysql_slave]=/usr/local/nrpe/libexec/chech_mysql_slave

 
4、在从服务器上编写脚本/usr/local/nrpe/libexec/check_mysql_slave(这是监控其作用的核心),其内容如下:

#!/bin/sh 
declare -a    slave_is 
slave_is=($(/usr/local/mysql/bin/mysql -unagios -pnagios    -e "show slave status\G"|grep Running |awk '{print $2}')) 
if [ "${slave_is[0]}" = "Yes" -a "${slave_is[1]}" = "Yes" ] 
     then 
     echo "OK -slave is running" 
     exit 0 
else 
     echo "Critical -slave is error" 
     exit 2 
fi 

 
5、在从服务器上手动运行这个脚本,输出显示是正常的

# sh /usr/local/nagios/libexec/check_mysql_slave
OK -slave is running

 
6、在nagios服务器上执行以下命名,测试从nagios服务器监控客户端是否正常。192.168.1.17为被监控端ip,亦即我们这边的mysql从数据库

# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.17 -c check_mysql_slave
OK -slave is running

 
7、在nagios服务器上修改nagios,添加检查mysql主从复制

# vim /usr/local/nagios/etc/servers/192.168.1.17.cfg   //在最后添加以下内容
define service{
    use     generic-service
    host_name       192.168.1.17
    service_description     check_mysql_slave
    check_command           check_nrpe!check_mysql_slave
    #notifications_enabled 0
}
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg  //每次重载nagios配置前检查下语法是好习惯。
# /etc/init.d/nagios reload  //重载nagios

 
附录:
暂空!

发表评论

邮箱地址不会被公开。 必填项已用*标注