nagios 监控 mysql 主从同步状态
http://blog.163.com/ly_89/blog/static/18690229920111129113543770/作为一名运维人员,对于 Mysql 的主从同步我们并不陌生。我们如何能得知主从库是否在实时的同步呢?每隔一分钟登录到数据库到数据库执行 show slave status\G显然是不靠谱。不要忘记我们有监控之神 nagios,我们可以通过 nagios 来监控主从库的同步状态。那如何来实现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: 172.16.117.251
Master_User: test1
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000008
Read_Master_Log_Pos: 16755097
Relay_Log_File: Slave_sql-relay-bin.000728
Relay_Log_Pos: 235
Relay_Master_Log_File: mysql-bin.000008
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 16755097
Relay_Log_Space: 235
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
1 row in set (0.00 sec)
这个输出,最关键处就是"Slave_IO_Running: Yes“和“Slave_SQL_Running: Yes”,这两个值全是"Yes"就表明主从库同步成功
##############################################################################################
nagios 监控 Mysql 主从同步的操作步骤:
第一部分:客户端配置
1. 编写脚本/usr/local/nagios/libexec/check_mysql_slave (这个脚本是监控mysql 主从同步状态的核心)
#!/bin/sh
slave_is=($(/usr/local/mysql/bin/mysql -uroot -pabc -e "show slave status\G"|grep Running |awk '{print $2}'))
if [ "${slave_is}" = "Yes" -a "${slave_is}" = "Yes" ]
then
echo "OK C2-slave is running"
exit 0
else
echo "Critical C2-slave is error"
exit 2
fi
2. 编辑 /usr/local/nagios/libexec/etc/nrpe.cfg 加入监控 mysql 主从状态同步的命令
command=/usr/local/nagios/libexec/check_mysql_slave
3. 启动 nrpe 服务,并检查其端口是否监听
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d # netstat -npl | grep nrpe
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 27500/nrpe
4. 启动 mysql 服务手动执行该脚本
#./check_mysql_slave
OK C2-slave is running
5. 关闭 mysql 服务手动执行该脚本
#./check_mysql_slave
Critical C2-slave is error
第二部分:服务器端的配置:
1. 编辑 /usr/local/nagios/etc/objects/host.cfg 定义监控的 client
define host {use linux-server
host_name 195-Slave-mysql
address 192.35.91.195
check_command check-host-alive
max_check_attempts 3
normal_check_interval 2
retry_check_interval 2
check_period 24x7
notification_interval 300
notification_period 24x7
notification_options d,u,r
contact_groups admins
process_perf_data 1
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
}
2. 编辑 /usr/local/nagios/etc/objects/ service.cfg
define service {
use generic-service
host_name 195-Slave-mysql
service_description check_mysql_replication_status
check_command check_nrpe!check_mysql_slave
max_check_attempts 2
normal_check_interval 2
retry_check_interval2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_optionsw,u,c,r
contact_groups admins
process_perf_data 1
action_url /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$
}
3. 重启 nagios 和 apache 服务
# /etc/init.d/nagios restart
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.
# /etc/init.d/httpd restart
Stopping httpd:
Starting httpd:
查看 nagios 监控图像
页:
[1]