/ 中存储网

使用nagios监控MySQL数据库的主从复制

2014-12-28 20:15:54 来源:中存储网

检测mysql 主从
一、先安装 nrpe,这样可以实现在远程主机上执行命令
nagios主机 192.168.24.1
mysql主机 192.168.24.3
被监控主机
安装nrpe的server端
tar fvxz nrpe*.tar.gz
./configure --prefix=/usr/local/nagios
useradd nagios
make
make install-daemon
make install-daemon-config
make install-xinetd
make install
把插件拷贝给监控主机nagios
scp /usr/local/nagios/libexec/check_nrpe   root@192.168.24.1ip:/usr/local/nagios/libexec
在被监控主机开启nrpe服务
vim /etc/xinetd.d/nrpe
# default: on
# description: NRPE (Nagios Remote Plugin Executor)
service nrpe

{
flags           = REUSE
socket_type     = stream   
port            = 5666   
wait            = no
user            = nagios
group           = nagios
server          = /usr/local/nagios/bin/nrpe
server_args     = -c /usr/local/nagios/etc/nrpe.cfg --inetd
        log_on_failure  += USERID
disable         = no
only_from       = 192.168.24.1  #监控主机的ip,保证他可以连接进来!
}
vim /etc/services
nrpe 5666/tcp
service xinetd restart

在监控主机上测试
[root@server1 objects]# /usr/local/nagios/libexec/check_nrpe -H 192.168.18.188
NRPE v2.12

在被监控主机安装插件

vim /usr/local/nagios/etc/nrpe.cfg
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_u]=/usr/local/nagios/libexec/check_users -w 5 -c 10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200

通过以上字段来定义命令,以及接收命令后执行的插件,就是可以在远程主机上执行的命令。

定义服务,来检测一下
define host {
host_name       mysql-server
alias           nrpe-server
address         192.168.24.3
check_command   check-host-alive
notification_options    d,u,r
        check_interval  1
max_check_attempts      2
        contact_groups  admins
notification_interval   10
notification_period     24x7
}

定义命令
define command {
command_name    check_nrpe
command_line    /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}

重启nagios服务!

二、建立 MySQL检测用户

mysql> GRANT REPLICATION CLIENT ON *.* TO ztz@localhost identified by '123';
mysql> flush privileges;

编写插件
被监控主机上
vim /usr/local/nagios/libexec/check_slave
#!/bin/bash
MYSQLUSER=ztz
MYSQLPS=123
MYSQLBIN=/usr/bin/mysql
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
STATE_NUM=$($MYSQLBIN -u$MYSQLUSER -p$MYSQLPS -e "show slave statusG" | grep Running |
grep Yes | wc -l)
if [ $? -ne 0 ];then
echo "Please Check the Plugins"
exit $STATE_UNKNOWN fi
if [ "${STATE_NUM}" -eq 2 ];then echo "Check OK,MySQL Replication is running"
exit $STATE_OK
else echo "Check Critical,MySQL Replication is error"
exit $STATE_CRITICAL
fi

被监控主机测试
[root@Nagiost ~]# cd /usr/local/nagios/libexec/
[root@Nagios-Client libexec]# chmod +x check_slave
[root@Nagios-Client libexec]# ./check_slave
Check OK,MySQL Replication is running
修改被监控主机的 nrpe.confg
[root@Nagios-Client ~]# vi /usr/local/nagios/etc/nrpe.cfg
添加:
command[check_slave]=/usr/local/nagios/libexec/check_slave

修改监控主机服务定义文件
[root@Nagios-Server ~]# vi /usr/local/nagios/etc/objects/localhost.cfg
添加: define service {
host_name  Nagios-Client 
service_description  check-slave 
check_period  24x7 
max_check_attempts  4 
normal_check_interval  3 
retry_check_interval  2  
notification_interval  10 
notification_period  24x7 
notification_options  w,u,c,r 
check_command  check_nrpe!check_slave 

3>重新加载 Nagios
[root@Nagios-Server ~]# /etc/init.d/nagios reload
Running configuration check...done.
Reloading nagios configuration...done
4>登陆验证

 

 

三、设置短信报警
1 install
rpm -ivh /tmp/msmtp-1.4.6-1.el5.scopserv.i386.rpm

2 config
[187 tmp]#cd  /etc/
[187 tmp]#vim msmtprc 默认是没有这个文件
account default
host smtp.163.com
port 25
from 13911111111@163.com
tls off
auth login
user 13810599111
password 123
logfile /tmp/msmtp.log

3 mutt
vim /etc/Muttrc
2753 set sendmail="/usr/bin/msmtp"
2754 set from="13911111111@163.com
"
2755 set realname="iori"
测试:
4 mutt+msmtp
echo "hello" |  mutt -s "test" 13911111111@163.com
修改一下mail的内个就行了
5  nagios---command.cfg
define command{
command_name    notify-by-sms
command_line    /usr/bin/printf "%b" "***** Nagios *****nnNotification Type: $NOTIFICATIONTYPE$nHost: $HOSTNAME$nState: $HOSTSTATE$nAddress: $HOSTADDRESS$nInfo: $HOSTOUTPUT$nnDate/Time: $LONGDATETIME$n" | /usr/bin/mutt -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$
        }

define contact {
        contact_name  ydl
alias           ydl
host_notification_period        24x7
host_notification_options       d,u,r
service_notification_period     24x7
service_notification_options    w,u,c,r
service_notification_commands   notify-by-sms 
host_notification_commands      notify-by-sms
        email   13911111111@163.com
}