Centos下nagios監控mysql主從配置的例子

來源:互聯網
上載者:User

方法一

一、mysql從伺服器設定

1、mysql從伺服器使用者添加

 代碼如下 複製代碼

執行如下語句添加使用者:
mysql> GRANT REPLICATION CLIENT ON *.* TO monitor@localhost IDENTIFIED BY 'PassWord';
2、下載check-mysql-slave.pl指令碼
cd /usr/local/bin/
wget http://www.centos.bz/wp-content/uploads/2012/10/check-mysql-slave.pl
chmod +x check-mysql-slave.pl
3、在mysql從伺服器上配置extend mysql-slave
在/etc/snmp/snmpd.conf檔案的末尾添加如下代碼:
extend mysql-slave /usr/local/bin/check-mysql-slave.pl --user monitor --pass PassWord --sock /var/lib/mysql/mysql.sock
注意相關參數修改為自己的。
之後重載snmp:
service snmpd reload

二、監控伺服器設定

1、下載check_snmp_extend.sh指令碼

 代碼如下 複製代碼
mkdir /usr/local/nagios/libexec.local
cd /usr/local/nagios/libexec.local
wget http://www.centos.bz/wp-content/uploads/2012/10/check_snmp_extend.sh
chmod +x check_snmp_extend.sh
2、定義USER10變數
在檔案/usr/local/nagios/etc/resource.cfg添加如下變數:
$USER10$=/usr/local/nagios/libexec.local
3、定義check_snmp_extend命令
在/usr/local/nagios/etc/objects/commands.cfg添加:
define command{
command_name check_snmp_extend
command_line $USER10$/check_snmp_extend.sh $HOSTADDRESS$ $ARG1$
}

4、定義監控mysql主從服務

在主機設定檔,如/usr/local/nagios/etc/objects/www.centos.bz.cfg中添加如下service(注意,此www.cnetos.bz.cfg檔案已經在nagios.cfg設定檔中包含)

 代碼如下 複製代碼
define host{
        use                     linux-server
        host_name               www.centos.bz
        alias                   www.centos.bz
        address                 142.4.33.74
        }
......
......
define service{
## This is an example service configured as
## extend servicename /path/to/service-check.sh
## on remote.server in /etc/snmp/snmpd.conf
use  generic-service
host_name  www.centos.bz
service_description mysql slave status
check_command  check_snmp_extend!mysql-slave
}

方法二


下面是監控MYSQL複製某個從伺服器的輸出:

 代碼如下 複製代碼
mysql> show slave statusG
*************************** 1. row***************************
              Slave_IO_State: Waiting for master to send event
                 Master_Host: 192.168.93.16
                 Master_User: rep1
                 Master_Port: 3306
               Connect_Retry: 60
             Master_Log_File: mysql-bin.000004
         Read_Master_Log_Pos: 1752541
              Relay_Log_File: hy-mysql3-relay-bin.000088
               Relay_Log_Pos: 2339
       Relay_Master_Log_File: mysql-bin.000004
            Slave_IO_Running: Yes
           Slave_SQL_Running: Yes
             Replicate_Do_DB:

這個輸出,最關鍵處就是”Slave_IO_Running: Yes“和“Slave_SQL_Running:Yes”,這兩個值全是”Yes”就表明主從複製正常,否則就是有問題。
操作步驟:
1、在主要資料庫伺服器增加一個使用者,給予較低的許可權,操作為

 代碼如下 複製代碼
mysql > grantReplication client on *.* to  ‘nagios’@'%’identified by ‘nagios’;
mysql> flush privileges;

2、登陸從伺服器驗證一下,看是否正常。操作為 # mysql -unagios -pnagios -e “show slavestutasG”  注意:這個操作是在 shell下操作的!!
3、在從伺服器安裝nrpe,然後加入一行”command[check_mysql_slave]=/usr/local/nrpe/libexec/chech_mysql_slave”.
4、編寫指令碼/usr/local/nrpe/libexec/check_mysql_slave(這是監控其作用的核心),其內容如下:

 代碼如下 複製代碼
#!/bin/sh
declare -a  slave_is
slave_is=($(/usr/local/mysql/bin/mysql -unagios-pnagios  -e “show slave statusG”|grep Running|awk ‘{print $2}’))
if [ "${slave_is[0]}” = “Yes” -a “${slave_is[1]}” = “Yes” ]
   then
   echo “OK -slave isrunning”
   exit 0
else
   echo “Critical -slave iserror”
   exit 2
fi

5、手動運行這個指令碼,觀察輸出。
6、執行 check_nrpe -H ip -c check_mysql_slave 觀察其輸出。
7、修改nagios,並測試。
8、功能測試(略)


方法三


首先定義主機: 
 

 代碼如下 複製代碼
vim /usr/local/nagios/etc/objects/localhost.cfg       
define host {
        host_name                    joker
        alias                              test
        address                        192.168.xx.xx
        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 service {
        host_name          joker
        service_description    nrpe
        check_period        24x7
        normal_check_interval  2
        retry_check_interval    1
        max_check_attempts      5
        notification_period    24x7
        notification_options    w,u,c,r
        check_command      check_nrpe!check_mysql
 }
 
定義command:
 
define command {
        command_name    check_nrpe
        command_line    /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
 }
 
在被監控主機上:
 
安裝nrpe
 tar fvxz nrpe*.tar.gz
 ./configure --prefix=/usr/local/nagios
 useradd nagios
 make all
 make install
 make install-daemon
 make install-daemon-config
 make install-xinetd
 
scp /usr/local/nagios/libexec/check_nrpe  root@192.168.18.105:/usr/local/nagios/libexec
 
vim /etc/xinetd.d/nrpe
 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.18.105 
}
 vim /etc/services
 
在最後添加nrpe 5666/tcp
 service xinetd restart
 
vim /usri/local/nagios/etc/nrpe.cfg
 command[check_mysql]=/usr/local/nagios/libexec/check_mysql
 
vim /usr/local/nagios/libexec/check_mysql
 #!/bin/bash
 master=`mysql -h 192.168.18.165 -u hello -p123 -e 'show master status' &> /tmp/123 | awk 'NR==2{print $2}' /tmp/123`
 slave=`mysql -e 'show slave status' &> /tmp/234 | awk =F: '{if($1~/Exec/)print $2}' /tmp/234`
 if [ $master -eq $slave ]; then
    echo "ok"
    rm -fr /tmp/123
    rm -fr /tmp/234
    exit 0
 else
    echo "error"
    mv /tmp/123 /tmp/err1
    mv /tmp/234 /tmp/err2
    exit 2
 


最後進行測試,本人親測OK~~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.