天天看點

搭建nagios監控

安裝

1.伺服器端

#rpm -Uvh http://repo.webtatic.com/yum/el6/latest.rpm

#yum install php54w php54w-cli php54w-common php54w-devel php54w-gd php54w-mbstring php54w-mcrypt php54w-mysql php54w-odbc php54w-pdo php54w-xml mysql55w mysql55w-devel mysql55w-server mysql55w-libs httpd memcached php-pecl-memcache gd gd-devel

# useradd nagios -s /sbin/nologin

# mkdir /usr/local/nagios

# chown nagios:nagios /usr/local/nagios/ -R

# tar -jxvf nagios-cn-3.2.3.tar.bz2

# ./configure --prefix=/usr/local/nagios/ --with-gd-lib=/usr/lib --with-gd-inc=/usr/include --with-nagios-user=nagios --with-nagios-group=nagios --with-gd-lib=/usr/lib --with-gd-inc=/usr/include/ --with-command-group=nagios

#make all

#make install

#make install-init

#make install-commandmode

#make install-config

******#make install-webconf                //如果是rpm包安裝的apache,可以執行,否則報錯,做如下操作即可

#vim /usr/local/apache/conf/httpd.conf

見nagios.conf

# /usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/.passwd.conf nagiosadmin            //生成nagios密碼密碼

或者

# /usr/local/apache/bin/htpasswd -bc /usr/local/nagios/etc/.passwd.conf nagiosadmin nagiosadmin    

#  /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg                    //檢查檔案是否出錯

#/etc/init.d/nagios start

#/etc/init.d/httpd start        

通路http://IP/nagios/                                        //最後的/一定加

# chmod o+w /usr/local/nagios/var/rw/nagios.cmd                            //更改事件通知時會用到

#/usr/local/nagios/bin/nagios -d  /usr/local/nagios/etc/nagios.cfg                //nagios手動重新開機

安裝插件

# tar -zxvf nagios-plugins-1.4.16.tar.gz

# ./configure --prefix=/usr/local/nagios/  --with-nagios-user=nagios --with-nagios-group=nagios

# make && make install

#tar -zxvf nrpe-2.12.tar.gz

#./configure --prefix=/usr/local/nagios/

#make all

#make install-plugin

#make install-daemon

#make install-daemon-config

#vim /usr/local/nagios/etc/objects/commands.cfg

在最後加上

define command{

        command_name check_nrpe

        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

        }

或者

define command{

        command_name check_nrpe

        command_line /usr/local/nagios/libexec/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

        }

# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d            //啟動nrpe

#/usr/local/nagios/bin/nagios -d  /usr/local/nagios/etc/nagios.cfg                //nagios手動重新開機

2.被監控端

#yum install openssl openssl-devel

# useradd nagios -s /sbin/nologin

#tar -zxvf nagios-plugins-1.4.16.tar.gz -C /usr/local/src/

#./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios

#make

#make install

#chown nagios:nagios /usr/local/nagios/ -R

#tar -zxvf nrpe-2.12.tar.gz -C /usr/local/src/

#./configure --prefix=/usr/local/nagios/

#make all

#make install-plugin

#make install-deamon

#make install-daemon-config

#make install-xinetd

#vim /usr/local/nagios/etc/nrpe.cfg

修改如下内容,根據自己需求修改    

allowed_hosts=127.0.0.1                                    //添加ip用逗号分隔                    

# vim /etc/services

在最後添加

nrpe            5666/tcp                        #nrpe

# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

配置

1.基本配置舉例(添加監控主機和監控服務)

定義主機(伺服器端)

# vim host.cfg

define host{

        use                     linux-server

        host_name               webserver

        alias                   web

#       parents                 

        address                 192.168.10.132

        icon_image              server.gif

        statusmap_image         server.gd2

        2d_coords               500,200

        3d_coords               500,200,100

        }

定義服務(伺服器端)

①定義host後通過本地check_tcp監控用戶端tcp端口

define service{

        use                             local-service

        host_name                       webserver

        service_description           1080

        check_command              check_tcp!1080     

        }

①定義host後通過本地check_http加上參數監控用戶端

define service{

        use                             local-service

        host_name                       webserver

        service_description           HTTP

        check_command              check_http!-I 192.168.10.132 -p 80 -e 403,202            //監控192.168.10.132主機的80端口,傳回值為403和                                                               202時,為ok狀态    

        }

③定義host後通過check_nrpe遠端調用用戶端插件來監控用戶端

define service{

        use                             local-service

        host_name                       webserver

        service_description           Login user

        check_command              check_nrpe!check_users  

        }

這裡check_nrpe!check_users指令的意思就是是通過check_nrpe插件遠端調用用戶端nrpe.cfg裡定義的check_user,是以用戶端nrpe.cfg裡需要定義一下check_users:

用戶端

#vim /usr/local/nagios/etc/nrpe.cfg

修改如下内容,根據自己需求修改               

command[check_users]=/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

2.監控網站首頁

定義指令

# vim commands.cfg

添加

define command{

        command_name    check_index

        command_line    $USER1$/check_http $ARG1$

        }

定義服務

# vim host.cfg

define service{

        use                             local-service         ; Name of service template to use

        host_name                       localhost

        service_description           Web_index

        check_command              check_index!-H 192.168.0.106 -u /index.php                //如果域名監控,格式是check_index!-H [url]www.testhost.test[/url] -u /index.php

       }

3.監控Mysql

檢視libexec目錄下有沒有check_mysql,如果沒有,yum installed mysql-devel,在重裝nagios-plugin,就會生成check_mysql

建立nagios專用資料庫

mysql> create database nagios;     

mysql> grant all on nagios.* to [email protected]'服務端IP' identified by 'nagios';

mysql> flush privileges;

測試

# /usr/local/nagios/libexec/check_mysql -H 用戶端IP -u nagios -d nagios -p nagios

Uptime: 44649  Threads: 10  Questions: 152675  Slow queries: 0  Opens: 292  Flush tables: 1  Open tables: 64  Queries per second avg: 3.419

# vim commands.cfg            //定義指令

添加

#check mysql

define command{

command_name check_mysql

command_line $USER1$/check_mysql -H $HOSTADDRESS$ -u nagiosuser -d nagios -p nagiospwd

}

# vim host.cfg                 //定義監控服務

define service{

        use                             local-service         ; Name of service template to use

        host_name                      webserver

        service_description          Mysql

        check_command             check_mysql

       }

PS:也可以在用戶端nrpe.cfg裡定義check_mysql指令(前提是用戶端的plugin裡生成了check_mysql),然後服務端定義服務時候通過check_nrpe!check_mysql來監控用戶端mysql,這樣就避免了資料庫的端口開放和使用者遠端登入帶來的風險

4.監控logfile

被監控端

#wget  http://labs.consol.de/wp-content/uploads/2012/02/check_logfiles-3.4.7.1.tar.gz

#tar xf check_logfiles-3.4.7.1.tar.gz

#./configure --prefix=/usr/local/nagios/ --with-nagios-user=nagios --with-nagios-group=nagios --with-seekfiles-dir=/usr/local/nagios/var/tmp/ --with-protocols-dir=/usr/local/nagios/var/tmp/ --with-perl=/usr/bin/perl --with-gzip=/bin/gzip

#make && make install

# vim /usr/local/nagios/etc/log.conf

@searches = (

{

tag => 'php_errors',

logfile => '/data/logs/php_errors.log',

rotation => 'CentOs',

criticalpatterns => [

'error',

],

},

);

#vim /usr/local/nagios/etc/nrpe.cfg

加入

command[check_logfiles]=/usr/local/nagios/libexec/check_logfiles --config=/usr/local/nagios/etc/log.conf

監控端

#vim etc/objects/commands.cfg

加入

define command{

command_name check_log

command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -t $ARG1$ -c $ARG2$

}

# vim /usr/local/nagios/etc/objects/host.cfg

加入

define service{

        use                             local-service

        host_name                      webserver

        service_description        PHP_Error

        check_command             check_log!2!check_logfiles                    //超過兩條報警

       }

5.監控程序

插件下載下傳位址

https://exchange.nagios.org/directory/Plugins/Operating-Systems/Linux/check_ps-2Esh/details

以監控一個java程式為例:

被監控端

把check_ps.sh放到被監控的的/usr/local/nagios/libexec/下

#vim /usr/local/nagios/etc/nrpe.cfg

command[check_UnZIP]=/usr/local/nagios/libexec/check_ps.sh -p "Linux_Sync_UnZIP.JAR"

ps:具體參數可以check_ps.sh -h檢視,設定一些具體伐值來報警,我這裡隻是監控程序是否存在

#killall nrpe

#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

監控伺服器

define service{

        use                             local-service         

        host_name                       10.10.10.10

        service_description             Linux_Sync_UnZIP

        check_command                   check_nrpe!check_UnZIP

        notifications_enabled           0

        }

#/etc/init.d/nagios restart

如果想配合pnp4nagios出圖的話,把下載下傳頁面的check_ps.php下載下傳到監控伺服器,放到/usr/local/pnp4nagios/share/templates目錄下即可

Error: Could not open command file '/usr/local/nagios/var/rw/nagios.cmd' for update!

解決方法

# usermod -a -G nagios www

# /etc/init.d/httpd restart

# /etc/init.d/nagios restart