實驗之前搭建好Mysql的主從同步,以及LAMP環境,然後搭建nagios伺服器,再在slave端配置。
1、在主要資料庫伺服器增加一個使用者
mysql > grant Replication client on *.* to 'nagios'@'%' identified by 'nagios';
mysql> flush privileges;
2、登陸從伺服器驗證一下,看是否正常。操作為
[root@slave libexec]# mysql -uroot -p123456 -e "show slave status\G"
*************************** 1. row ***************************
Slave_IO_State: Reconnecting after a failed master event read
Master_Host: 192.168.56.105
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 645
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 705
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
3、在從伺服器安裝 nrpe,然後在設定檔nrpe.cfg加入一行
安裝過程這裡就不再重複,只講配置)
slave上的配置
1)nrpe配置
添加下面的命令
[root@slave~]# vim /usr/local/nagios/etc/nrpe.cfg
command[check_mysql_slave]=/usr/local/nagios/libexec/check_mysql_slave
配置完成後重啟nrpe
[root@localhost nagios]# netstat -nultp |grep nrpe
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 3730/nrpe
[root@localhost nagios]# kill 3730
[root@localhost nagios]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
2)建立指令碼(從伺服器上)
[root@slave ~]# vim /usr/local/nagios/libexec/check_mysql_slave
#!/bin/sh
declare -a slave_is
slave_is=($(/usr/local/mysql/bin/mysql -uroot -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
[root@slave ~]# chmod +x /usr/local/nagios/libexec/check_mysql_slave
測試指令碼運行情況:
[root@localhost libexec]# ./check_mysql_slave
OK -slave is running
================
伺服器端的配置:
測試能不能通過nrpe 擷取到主從同步運行情況
[root@localhost libexec]# /usr/local/nagios/libexec/check_nrpe -H 192.168.56.102 -c check_mysql_slave
OK -slave is running
首先配置監控slave這台主機:
[root@localhost etc]# vim /usr/local/nagios/etc/hosts/slave.cfg
define host{
use linux-server,hosts-pnp
host_name slave
alias slave
address 192.168.56.102
}
配置監控的服務:
[root@localhost etc]# vim /usr/local/nagios/etc/services/slave.cfg
define service{
use generic-service,services-pnp
host_name slave
service_description ping
check_command check_ping!100.0,20%!500.0,60%
max_check_attempts 5
normal_check_interval 1
}
define service{
use generic-service,services-pnp
host_name slave
service_description tcp
check_command check_tcp!22
max_check_attempts 5
normal_check_interval 1
}
......
define service{
use generic-service,services-pnp
host_name slave
service_description check_mysql_slave
check_command check_nrpe!check_mysql_slave
max_check_attempts 5
normal_check_interval 1
}
重新載入nagios配置
[root@localhost etc]# service nagios reload
Running configuration check...done.
Reloading nagios configuration...done
開啟nagios 的服務頁,可以看到已成功監控主從複製
650) this.width=650;" title="clipboard.png" src="http://www.bkjia.com/uploads/allimg/131227/1I1055404-0.png" />
類比發生故障,主伺服器掛掉,導致主從複製失敗,看nagios能不能監控到:
[root@client ~]# service mysqld stop
Shutting down MySQL.... SUCCESS!
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Reconnecting after a failed master event read
Master_Host: 192.168.56.105
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 645
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 705
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
。。。。。。
650) this.width=650;" title="clipboard.png" src="http://www.bkjia.com/uploads/allimg/131227/1I1053535-1.png" />
實驗至此就完成了!
這裡提出個疑問:從上面的實驗可以看到,nagios只是監控mysql主從複製是否正常運行,如果我還要監控主從複製是否即時同步,或者說是否同步完成,又該怎麼做?