nagios监控mysql的主从,我这里介绍两种方法,一种是用nagios自带监控mysql 的插件实现,另一种是自己写脚本,下面是两种方法的具体实现过程:

一、nagios自带监控mysql的插件实现

1、编辑/usr/local/nagios/etc/commands.cfg,添加监控mysql主从的字段:

define command{
        command_name   check_mysql_slave
        command_line   $USER1$/check_mysql -H $HOSTADDRESS$ -P13267 -S -uroot -p123456 -s /data/mysql/nagios/logs/mysql.sock 
        }

PS:-u指定用户,-P指定端口,-p指定密码,-s指定sock文件路径,-S监控主从

2、编辑/usr/local/nagios/etc/objects/cfg/navyserver.cfg ,添加监控mysql主从项目:

define service{
        use             generic-service
        host_name       navy
        service_description     check_mysql_slave1
        check_command           check_mysql_slave
        }

3、重启nagios就OK了

/etc/init.d/nagios restart

二、脚本监控

1、撰写监控脚本

vi /usr/local/nagios/libexec/check_mysql_slave
#!/bin/sh 
declare -a    slave_is 
slave_is=($(cd /data/mysql/nagios; /data/mysql/nagios/bin/mysql --sock=logs/mysql.sock  -unagios -p123456  -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 

2、在/usr/local/nagios/etc/nrpe.cfg中添加监控mysql主从的命令

command[check_mysql_slave]=/usr/local/nagios/libexec/check_mysql_slave

3、编辑/usr/local/nagios/etc/objects/cfg/navyserver.cfg ,添加监控mysql主从项目:

define service{
        use             generic-service
        host_name       navy
        service_description     check_mysql_slave
        check_command           check_nrpe!check_mysql_slave
        }

4、重启nagios就OK了

/etc/init.d/nagios restart