nagios 監控mysql主從複製

檢測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   [email protected]:/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 status\G" | 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 [email protected]
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="[email protected]"
2755 set realname="iori"
測試:
4 mutt+msmtp
echo "hello" |  mutt -s "test" [email protected]

修改一下mail的內個就行了
5  nagios---command.cfg
define command{
        command_name    notify-by-sms
        command_line    /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/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   [email protected]
}

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章