Nagios+PNP+NRPE的安裝與配置

拓撲圖:

wKiom1PjPN_QarGaAACjOclFQAM459.jpg

一安裝Nagios

  1、安裝前準備

    a、創建Nagios用戶和用戶組,創建nagios主目錄

       [root@Server /]# useradd -s /sbin/nologin nagios

       [root@Server /]# passwd nagios

       [root@Server /]# mkdir /usr/local/nagios

       [root@Server /]# chown nagios.nagios /usr/local/nagios/


    b、開啓sendmail或者Postfix服務

      chkconfig sendmail on

      service sendmail start

      或者

      chkconfig postfix on

      service postfix start

  2、編譯安裝Nagios

      [root@Server Nagios主程序]# tar -zxvf nagios-3.2.3.tar.gz

      [root@Server Nagios主程序]# cd nagios-3.2.3

      [root@Server nagios-3.2.3]# ./configure --prefix=/usr/local/nagios/       指定Nagios的安裝目錄

      [root@Server nagios-3.2.3]#make all

      [root@Server nagios-3.2.3]#make install 安裝Nagios主程序CGI和HTML文件

      [root@Server nagios-3.2.3]#make install-init    在/etc/rc.d/init.d目錄下創建Nagios啓動腳本

      [root@Server nagios-3.2.3]#make install-commandmode   命令來配置目錄權限

      [root@Server nagios-3.2.3]#make install-config    安裝Nagios示例配置文件,這裏的安裝路徑是/usr/local/nagios/etc

    a、設置開機啓動

      [root@Server nagios-3.2.3]# chkconfig --add nagios

      [root@Server nagios-3.2.3]# chkconfig --level 35 nagios on

      [root@Server nagios-3.2.3]# chkconfig | grep nagios

       nagios          0:關閉  1:關閉  2:關閉  3:啓用  4:啓用  5:啓用  6:關閉

      [root@Server nagios-3.2.3]#


    b、Nagios各個目錄名稱及用途說明

      [root@Server nagios-3.2.3]# ls /usr/local/nagios/

       bin  etc  libexec  sbin  share  var

      [root@Server nagios]# ls var

       archives  rw  spool

      [root@Server nagios]#


      目錄名稱                  用途

      bin              Nagios可執行程序所在目錄

      etc              Nagios配置文件所在目錄

      sbin             NagiosCGI文件所在目錄,也就是執行外部命令所需文件所在目錄

      share            Nagios網頁文件所在的目錄

      libexec          Nagios外部插件所在目錄

      var              Nagios日誌文件、lock等文件所在的目錄

      var/archives     Nagios日誌自動歸檔目錄

      var/rw           用來存放外部命令文件的目錄


  3、安裝Nagios插件

      [root@Server Nagios插件]# tar -zxvf nagios-plugins-1.4.14.tar.gz

      [root@Server Nagios插件]# cd nagios-plugins-1.4.14

      [root@Server nagios-plugins-1.4.14]# ./configure

      [root@Server nagios-plugins-1.4.14]# make all

      [root@Server nagios-plugins-1.4.14]# make install


  4、安裝Nagios漢化插件

      [root@Server Nagios漢化插件]# tar -jxvf nagios-cn-3.2.3.tar.bz2

      [root@Server Nagios漢化插件]# cd nagios-cn-3.2.3

      [root@Server nagios-cn-3.2.3]# ./configure

      [root@Server nagios-cn-3.2.3]# make all

      [root@Server nagios-cn-3.2.3]# make install


  5、安裝Apache和PHP

      [root@Server Apache]# tar -jxvf httpd-2.2.13.tar.bz2

      [root@Server Apache]# cd httpd-2.2.13

      [root@Server httpd-2.2.13]# ./configure --prefix=/usr/local/apache2

      [root@Server httpd-2.2.13]# make

      [root@Server httpd-2.2.13]# make install


      [root@Server PHP]# tar -jxvf php-5.5.15.tar.bz2

      [root@Server php-5.5.15]# cd php-5.5.15

      [root@Server php-5.5.15]# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2/bin/apxs

      [root@Server php-5.5.15]# make

      [root@Server php-5.5.15]# make test

      [root@Server php-5.5.15]# make install



    a、配置Apache文件

      vim /usr/local/apache2/conf/httpd.conf


      242 User apache

      243 Group apache

      改成

      66 User nagios

      67 Group nagios


      167     DirectoryIndex index.html

      改成

      167     DirectoryIndex index.html index.php

      167     AddType application/x-httpd-php .php


      添加

      412 #setting for nagios

      413 ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"

      414 <Directory "/usr/local/nagios/sbin">

      415     Authtype Basic

      416     Options ExecCGI

      417     AllowOverride None

      418     Order allow,deny

      419     Allow from all

      420     AuthName "Nagios Access"

      421     AuthUserFile /usr/local/nagios/etc/htpasswd

      422     Require valid-user

      423 </Directory>

      424

      425 Alias /nagios "/usr/local/nagios/share"

      426 <Directory "/usr/local/nagios/share">

      427     AuthType Basic

      428     Options None

      429     AllowOverride None

      430     Order allow,deny

      431     Allow from all

      432     AuthName "nagios Access"

      433     AuthUserFile /usr/local/nagios/etc/htpasswd

      434     Require valid-user

      435 </Directory>


    b、創建Apache目錄驗證文件

      [root@Server /]# /usr/local/apache2/bin/htpasswd -cm /usr/local/nagios/etc/htpasswd xiaodong

      New password:

      Re-type new password:

      Adding password for user xiaodong

      [root@Server /]#

    c、啓動Apache服務

      [root@Server conf]# /usr/local/apache2/bin/apachectl start

      httpd: apr_sockaddr_info_get() failed for Server.shbztech.com

      httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

      [root@Server conf]#

                (FQDN的問題,修改文件“/etc/httpd/conf/httpd.conf”即可解決)

                 98 ServerName server.shbztech.com:80



        http:172.16.1.99/nagios   通過身份驗證“xiaodong/xiaodong”可以看到Nagios的主頁


  6、配置Nagios


      默認的配置文件在“/usr/local/nagios/etc”目錄下,


      文件名或目錄名                  用途

      cgi.cfg                      控制CGI訪問的配置文件

      nagios.cfg                   Nagios主配置文件

      resource.cfg                 變量定義文件,又稱爲資源文件,在此文件中定義變量,以便由其它配置文件引用。

      objects                      objects是一個目錄,在此目錄下有很多配置文件模板,用於定義Nagios對象

      objects/commands.cfg         命令定義配置文件,其中定義的命令可以被其它配置文件引用

      objects/contacts.cfg         定義聯繫人和聯繫人組的配置文件

      objects/localhost.cfg        定義監控本地主機的配置文件

      objects/printer.cfg          定義監控打印機的一個配置文件模板,默認沒有啓用此文件

      objects/switch.cfg           監控路由器的一個配置文件模板,默認沒有啓用此文件

      objects/templates.cfg        定義主機和服務的一個模板配置文件,可以在其它配置文件中引用

      objects/timeperiods.cfg      定義Nagios監控時間段的配置文件

      objects/windows.cfg          監控windows主機的一個配置文件模板,默認沒有啓用此文件


    a、配置文件之間的關係

      Nagios的配置過程設計的幾個定義有:主機、主機組、服務、服務組、聯繫人、聯繫人組、監控時間和監控命令等。

      第一:定義監控那些主機、主機組、服務和服務組。

      第二:定義這個監控通過什麼命令實現。

      第三:定義監控的時間段。

      第四:定義主機和服務出現問題時要通知的聯繫人和聯繫人組。


    b、templates.cfg文件


      define contact{                                                 ;聯繫人

              name                            generic-contact         ;聯繫人名稱

              service_notification_period     24x7                    ;當服務出現異常,發送通知的時間段(timeperiods.cfg文件中定義)

              host_notification_period        24x7                    ;當主機出現異常時,發送通知的時間段

              service_notification_options    w,u,c,r,f,s             ;定義服務“通知可以被髮送的情況”,w即warn,表示警告狀態;u即unknown,表示不明狀態;c即criticle,表示緊急狀態;r即recover,表示恢復狀態。

              host_notification_options       d,u,r,f,s               ;定義主機在什麼狀態下需要發送通知給使用者,d即down,表示宕機狀態;u即unreachable,表示不可到達狀態;r即recovery,表示重新恢復狀態

              service_notification_commands   notify-service-by-email ;服務故障發送通知的方式,“commands.cfg文件中定義”

              host_notification_commands      notify-host-by-email    ;主機故障發送通知的方式,“commands.cfg文件中定義”

              register                        0                       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!

              }


      define host{

              name                            generic-host    ;主機名稱(自定義)

              notifications_enabled           1               ;

              event_handler_enabled           1               ;

              flap_detection_enabled          1               ;

              failure_prediction_enabled      1               ;其值可以爲0或1,起作用爲是啓用Nagios的數據輸出功能。

              process_perf_data               1               ;Nagios收集數據寫入某個文件中,以備提取

              retain_status_information       1               ;

              retain_nonstatus_information    1               ;

              notification_period             24x7            ;

              register                        0               ;

              }



      define host{

              name                            linux-server    ;主機名稱(自定義)

              use                             generic-host    ;use表示引用,將generic-host的屬性應用到linux-server中

              check_period                    24x7            ;Nagios檢查主機的時間段

              check_interval                  5               ;Nagios對主機檢查時間間隔

              retry_interval                  1               ;重試檢查時間間隔,單位是分鐘

              max_check_attempts              10              ;Nagios對主機的最大檢查次數,當Nagios檢查到主機宕機時,會重複檢查該主機的次數

              check_command                   check-host-alive ;指定檢查主機狀態的命令

              notification_period             workhours       ;主機故障時,發送通知的時間範圍,其中“workhours”在timeperiods.cfg中進行定義

              notification_interval           120             ;當主機出現異常後,故障一直沒有解決,Nagios再次對使用者發送通知的時間。單位是分鐘。如果覺得所有的事件發送一次通知就夠了,可以吧這個選項設爲0

              notification_options            d,u,r           ;定義主機在什麼狀態下發送通知給使用者

              contact_groups                  admins          ;指定聯繫人組。“admins”在contacts.cfg文件中定義

              register                        0               ;

              }



      define service{

              name                            generic-service         ;定義一個服務名稱

              active_checks_enabled           1                       ;

              passive_checks_enabled          1                       ;

              parallelize_check               1                       ;

              obsess_over_service             1                       ;

              check_freshness                 0                       ;

              notifications_enabled           1                       ;

              event_handler_enabled           1                       ;

              flap_detection_enabled          1                       ;

              failure_prediction_enabled      1                       ;

              process_perf_data               1                       ;

              retain_status_information       1                       ;

              retain_nonstatus_information    1                       ;

              is_volatile                     0                       ;

              check_period                    24x7                    ;

              max_check_attempts              3                       ;Nagios對服務的最大檢查次數

              normal_check_interval           10                      ;服務檢查時間間隔,單位:分鐘

              retry_check_interval            2                       ;重試檢查事件間隔,單位:分鐘

              contact_groups                  admins                  ;指定聯繫人組

              notification_options            w,u,c,r                 ;

              notification_interval           60                      ;服務出現故障後,故障一直沒解決,Nagios在此向使用者發送通知的時間,單位:分鐘

              notification_period             24x7                    ;

               register                        0                      ;

              }


      define service{

              name                            local-service           ; The name of this service template

              use                             generic-service         ; Inherit default values from the generic-service definition

              max_check_attempts              4                       ; Re-check the service up to 4 times in order to determine its final (hard) state

              normal_check_interval           5                       ; Check the service every 5 minutes under normal conditions

              retry_check_interval            1                       ; Re-check the service every minute until a hard state can be determined

              register                        0                       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!

              }



    c、resource.cfg文件(只有一行)

      $USER1$=/usr/local/nagios/libexec        指定安裝Nagios插件的路徑

          

    d、commangs.cfg文件

      此文件默認情況下是存在的,無需任何修改即可使用。


    e、hosts.cfg

      此文件默認情況下不存在,需要手動創建。主要用來指定被監控的主機地址


      define host {

          use             linux-server                 ;引用主機linux-server的屬性

          host_name       www.shbztech.com             ;主機名

          alias           xiaodong-web                 ;主機別名

          address         172.16.1.100                 ;主機IP

      }


      define host {

          use             linux-server

          host_name       ftp.shbztech.com

          alias           xiaodong-ftp

          address         172.16.1.101

      }


      define hostgroup {                                     ;定義一個主機組

          hostgroup_name  linux-server                       ;主機組名稱,可以隨意指定

          alias           Linux Server                       ;主機組別名

          members         www.shbztech.com,ftp.shbztech.com  ;主機組成員

      }


    f、services.cfg文件

      此文件默認情況下不存在,需要手動創建。主要用來定義監控的服務和主機資源。

      例如:監控HTTP服務、FTP服務、主機磁盤空間、主機系統負載等。


      ##################################  xiaodong  Web  #####################################

      define service{

          use                  local-service

          host_name            www.shbztech.com

          service_description  PING

          check_command        check_ping!100.0,20%!500.0,60%

      }


      define service{

          use                  local-service

          host_name            www.shbztech.com

          service_description  SSH

          check_command        check_ssh

      }


      define service{

          use                  local-service

          host_name            www.shbztech.com

          service_description  http

          check_command        check_http

      }


      ##################################  xiaodong  FTP  #####################################

      define service{

          use                  local-service

          host_name            ftp.shbztech.com

          service_description  PING

          check_command        check_ping!100.0,20%!500.0,60%

      }


      define service{

          use                  local-service

          host_name            ftp.shbztech.com

          service_description  SSH

          check_command        check_ssh

      }


      define service{

          use                  local-service

          host_name            ftp.shbztech.com

          service_description  ftp

          check_command        check_tcp!21

      }



      在Nagios中,插件命令和參數組合格式爲:命令!參數!參數。如果有更多參數,以此通過歎號分割即可

      例如:

      check_ping!100.0,20%!500.0,60%

      此命令組合從左到右依次爲:命令!告警時延,丟包率!嚴重告警時延,丟包率。

      

      check_http!0.0020!0.0050!10

      此命令組合從左到右依次爲:命令!告警時延!嚴重告警時延!連接超時時間


      check_tcp!23!0.0020!0.0050!10

      此命令組合從左到右依次爲:命令!端口!告警時延!嚴重告警時延!連接超時時間


      check_ssh!22!10

      此命令組合從左到右依次爲:命令!端口!連接超時時間


      check_smtp!0.0020!0.0050!10

      此命令組合從左到右依次爲:命令!告警時延!嚴重告警時延!連接超時時間


      另外,在監控服務器端口時,很多命令都可以使用check_tcp來代替,

      例如:

      check_ssh=check_tcp!22

      check_imap=check_tcp!143

      check_ftp=check_tcp!21


    g、contacts.cfg文件

      contacts.cfg是一個定義聯繫人和聯繫人組的配置文件。

      

      define contact{

              contact_name                    xiaodong                ;聯繫人名稱

              use                             generic-contact         ;引用generic-contact的屬性信息

              alias                           Nagios Admin            ;聯繫人別名

              email                           [email protected]   ;聯繫人常用郵箱地址

              }



      define contactgroup{

              contactgroup_name       admins                          ;聯繫人組

              alias                   Nagios Administrators           ;聯繫人組描述

              members                 xiaodong                        ;聯繫人組成員

              }


    h、timeperiods.cfg文件

      此文件只用於定義監控的時間段


      define timeperiod{

              timeperiod_name 24x7

              alias           24 Hours A Day, 7 Days A Week

              sunday          00:00-24:00

              monday          00:00-24:00

              tuesday         00:00-24:00

              wednesday       00:00-24:00

              thursday        00:00-24:00

              friday          00:00-24:00

              saturday        00:00-24:00

              }


      define timeperiod{

              timeperiod_name workhours

              alias           Normal Work Hours

              monday          09:00-17:00

              tuesday         09:00-17:00

              wednesday       09:00-17:00

              thursday        09:00-17:00

              friday          09:00-17:00

              }


    i、cgi.cfg文件

      此文件用來控制相關CGI腳本,如果想在Nagios的Web監控界面執行CGI腳本,例如重啓Nagios進程、關閉Nagios通知

      停止Nagios主機檢測等,這時就需要配置cgi.cfg文件了

      由於Nagios的Web監控界面驗證用戶爲xiaodong,因此只需在cgi.cfg文件中添加此用戶的執行權限。


      107 default_user_name=xiaodong

      119 authorized_for_system_information=nagiosadmin,xiaodong

      131 authorized_for_configuration_information=nagiosadmin,xiaodong

      144 authorized_for_system_commands=nagiosadmin,xiaodong

      157 authorized_for_all_services=nagiosadmin,xiaodong

      158 authorized_for_all_hosts=nagiosadmin,xiaodong

      

      

    j、nagios.cfg文件

      Nagios的核心配置文件,所有對象配置文件必須在這個文件中進行定義才能發揮其作用


      “log_file”變量用來定義Nagios日誌文件的路徑

      19 log_file=/usr/local/nagios/var/nagios.log


      “cfg_file”變量用來引用對象配置文件

      30 cfg_file=/usr/local/nagios/etc/objects/hosts.cfg

      31 cfg_file=/usr/local/nagios/etc/objects/services.cfg

      32 cfg_file=/usr/local/nagios/etc/objects/commands.cfg

      33 cfg_file=/usr/local/nagios/etc/objects/contacts.cfg

      34 cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg

      35 cfg_file=/usr/local/nagios/etc/objects/templates.cfg


      “object_cache_file”變量用於指定一個“所有對象配置文件”的副本文件,又稱爲對象緩衝文件,這個設定可以加快CGI的配置緩衝,並且在編輯對象配置文件時,可以讓正在進行的Nagios不影響CGI的顯示輸出

      69 object_cache_file=/usr/local/nagios/var/objects.cache


      “resource_file”變量用於指定Nagios資源文件的路徑

      98 resource_file=/usr/local/nagios/etc/resource.cfg


      “status_file”變量用於定義一個狀態文件

      108 status_file=/usr/local/nagios/var/status.dat


      “status_update_interval”變量用於定義狀態文件的更新時間間隔,單位是秒,最先更新間隔是1秒

      117 status_update_interval=10


      “nagios”進程由那個用戶和組運行

      125 nagios_user=nagios

      133 nagios_group=nagios


      “check_external_commands”變量用於設置是否允許Nagios在Web監控界面上運行CGI命令,也就是是否允許Nagios在Web界面下執行重啓Nagios、停止主機/服務檢查等操作,“1”爲運行,“0”爲不允許運行

      145 check_external_commands=1


      “command_check_interval”變量用於設置Nagios對外部命令檢測的時間間隔,如果指定了一個數字加一個“s”(如10s),那麼外部檢測命令的間隔是這個數值以秒爲單位的時間間隔。如果沒有“s”,那麼外部檢測的命令的間隔是以這個數值爲“時間單位”的時間間隔

      163 command_check_interval=2


      “interval_length”變量指定了Nagios的時間單位,默認是60秒,也就是1分鐘,即在Nagios配置中所有的“時間單位”都是分鐘

      720 interval_length=60



  7、Nagios的運行和維護


    a、驗證nagios配置文件的正確性

      [root@Server etc]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg


      Nagios Core 3.2.3

      Copyright (c) 2009-2010 Nagios Core Development Team and Community Contributors

      Copyright (c) 1999-2009 Ethan Galstad

      Last Modified: 10-03-2010

      License: GPL


      Website: http://www.nagios.org

      Reading configuration data...

         Read main config file okay...

      Processing object config file '/usr/local/nagios/etc/objects/hosts.cfg'...

      Processing object config file '/usr/local/nagios/etc/objects/services.cfg'...

      Processing object config file '/usr/local/nagios/etc/objects/commands.cfg'...

      Processing object config file '/usr/local/nagios/etc/objects/contacts.cfg'...

      Processing object config file '/usr/local/nagios/etc/objects/timeperiods.cfg'...

      Processing object config file '/usr/local/nagios/etc/objects/templates.cfg'...

      Processing object config file '/usr/local/nagios/etc/objects/localhost.cfg'...

         Read object config files okay...


      Running pre-flight check on configuration data...


      Checking services...

              Checked 14 services.

      Checking hosts...

              Checked 3 hosts.

      Checking host groups...

              Checked 2 host groups.

      Checking service groups...

              Checked 0 service groups.

      Checking contacts...

              Checked 1 contacts.

      Checking contact groups...

              Checked 1 contact groups.

      Checking service escalations...

              Checked 0 service escalations.

      Checking service dependencies...

              Checked 0 service dependencies.

      Checking host escalations...

              Checked 0 host escalations.

      Checking host dependencies...

              Checked 0 host dependencies.

      Checking commands...

              Checked 24 commands.

      Checking time periods...

              Checked 5 time periods.

      Checking for circular paths between hosts...

      Checking for circular host and service dependencies...

      Checking global event handlers...

      Checking obsessive compulsive processor commands...

      Checking misc settings...


      Total Warnings: 0

      Total Errors:   0


      Things look okay - No serious problems were detected during the pre-flight check

      [root@Server etc]#


    b、啓動與停止Nagios

      啓動Nagios


      1>通過初始化腳本啓動Nagios

      /etc/init.d/nagios start

      或者

      service nagios start

      

      2>手工方式啓動Nagios

      通過Nagios命令的“-d”參數來啓動Nagios守護進程

      /usr/local/nagios/bin/nagios -d /usr/local/nagios/etc/nagios.cfg

      

      關閉Nagios


      1>通過初始化腳本關閉Nagios服務

      /etc/init.c/nagios stop

      或者

      service nagios stop

      

      2>通過kill方式關閉Nagios

      kill <nagios_pid>

      

      重啓Nagios      

      

      1>通過初始化腳本來重啓Nagios

      /etc/init.d/nagios reload

      /etc/init.d/nagios restart

      或者

      service nagios restart


      2>通過Web監控頁重啓Nagios


      3>手工方式平滑重啓

      kill -HUP <nagios_pid>

 


  8、Nagios性能分析圖表的實現

    a、安裝rrdtool

      [root@Server RRDtool]# tar -zxvf rrdtool-1.4.5.tar.gz

      [root@Server RRDtool]# cd rrdtool-1.4.5

      [root@Server rrdtool-1.4.5]# ./configure --prefix=/usr/local/rrdtool

      [root@Server rrdtool-1.4.5]# make

      [root@Server rrdtool-1.4.5]# make install

      [root@Server rrdtool-1.4.5]#


      configure: error: Please fix the library issues listed above and try again.

      解決方案:

        第一:

            tar -zxvf cgilib-0.5.tar.gz

            cd cgilib-0.5

            make

            cp libcgi.a /usr/local/lib

            cp cgi.h /usr/include/

            不能解決看下面

        第二:

            yum -y install libart_lgpl-devel

            不能解決看下面

        第三:

            yum -y install pango-devel* cairo-devel* libxml2-devel


            解決!!

         

            

    b、安裝pnp

      [root@Server PNP]# tar -zxvf pnp-0.4.13.tar.gz

      [root@Server PNP]# cd pnp-0.4.13

      [root@Server pnp-0.4.13]# ./configure --with-nagios-user=nagios --with-nagios-group-nagios --with-rrdtool=/usr/local/rrdtool/bin/rrdtool --with-perfdata-dir=/usr/local/nagios/share/perfdata

      [root@Server pnp-0.4.13]# make all

      [root@Server pnp-0.4.13]# make install

      [root@Server pnp-0.4.13]# make install-config

      [root@Server pnp-0.4.13]# make install-init

      

    c、配置pnp

      創建默認配置文件

      [root@Server /]# cd /usr/local/nagios/etc/pnp/

      [root@Server pnp]# cp process_perfdata.cfg-sample process_perfdata.cfg

      [root@Server pnp]# cp npcd.cfg-sample npcd.cfg

      [root@Server pnp]# cp rra.cfg-sample rra.cfg

      [root@Server pnp]# chown -R nagios.nagios /usr/local/nagios/etc/pnp/

      

      修改process_perfdata.cfg文件

      [root@Server pnp]# vim process_perfdata.cfg

      44 LOG_LEVEL = 0

      改成

      44 LOG_LEVEL = 2


    d、修改Nagios配置文件


      1>增加小太陽圖標

      修改templates.cfg文件

      define host {

          name                hosts-pnp

          register            0

          action_url          /nagios/pnp/index.php?host=$HOSTNAME$

          process_perf_data   1

      }


      define service {

          name                services-pnp

          register            0

          action_url          /nagios/pnp/index.php?host=$HOSTNAME$&srv=$SERVICEDESC$

          process_perf_data   1

      }


      2>修改nagios.cfg文件

      833 process_performance_data=0

      改成

      833 process_performance_data=1           ###開啓數據輸出功能


      去掉註釋

      845 host_perfdata_command=process-host-perfdata

      846 service_perfdata_command=process-service-perfdata


      3>修改commands.cfg文件

      228 define command{

      229         command_name    process-host-perfdata

      230         command_line    /usr/local/nagios/libexec/process_perfdata.pl

      231         }


      235 define command{

      236         command_name    process-service-perfdata

      237         command_line    /usr/local/nagios/libexec/process_perfdata.pl

      238         }


      4>修改hosts.cfg文件和services.cfg文件

      hosts.cfg

      define host {

          use             linux-server,hosts-pnp         ;“添加hosts-pnp”

          host_name       www.shbztech.com

          alias           xiaodong-web

          address         172.16.1.100

      }


      services.cfg

      define service{

          use                  local-service,services-pnp

          host_name            www.shbztech.com

          service_description  PING

          check_command        check_ping!100.0,20%!500.0,60%

      }



      檢測Nagios配置正確性

      /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg


      service nagios restart


      打開小太陽出現故障


      故障一:PHP zlib Support not found

      解決方案:需要重新編譯安裝PHP。

      [root@Server php-5.5.15]# cd php-5.5.15

      [root@Server php-5.5.15]# ./configure --prefix=/usr/local/php5 --with-gd --with-zlib --with-apxs2=/usr/local/apache2/bin/apxs

      [root@Server php-5.5.15]# make

      [root@Server php-5.5.15]# make test

      [root@Server php-5.5.15]# make install

      

      故障二: /usr/local/nagios/share/pnp/include/function.inc.php on line 557

      解決方案:vim /usr/local/nagios/share/pnp/include/function.inc.php

      556         date_default_timezone_set('UTC');                添加一行


      故障三: /usr/local/nagios/share/pnp/include/function.inc.php on line 1027

      1027         $pdf =& new PDF('P', 'mm', 'A4');

      改成

      1027         $pdf = new PDF('P', 'mm', 'A4');


      1503             if($level == 2 && $type == "complete" && eregi("^NAGIOS_",$tag)){

      改成

      1503             if($level == 2 && $type == "complete" && preg_match("/^NAGIOS_/",$tag)){




      再重新重啓nagios,應該就能看到圖表了


wKioL1PjPwWS0h2fAAeys52j5Sg692.jpg

wKiom1PjPezAHADeAAY-0C-04rY276.jpg


  9、利用插件擴展Nagios的監控功能 - 利用NRPE外部構件監控遠程主機


    配置Nagios客戶端(遠程主機)

    a、安裝Nagios插件

      [root@www nagios]# useradd -s /sbin/nologin nagios

      [root@www nagios]# passwd nagios

      [root@www nagios]# tar -zxvf nagios-plugins-1.4.14.tar.gz

      [root@www nagios]# cd nagios-plugins-1.4.14

      [root@www nagios-plugins-1.4.14]# ./configure

      [root@www nagios-plugins-1.4.14]# make install


      [root@www nagios-plugins-1.4.14]# chown nagios.nagios /usr/local/nagios/

      [root@www nagios-plugins-1.4.14]# chown nagios.nagios /usr/local/nagios/libexec/


    b、安裝NRPE插件

      [root@www nagios]# tar -zxvf nrpe-2.14.tar.gz

      [root@www nagios]# cd nrpe-2.14


      故障:checking for SSL headers... configure: error: Cannot find ssl headers

      解決方案:

      yum -y install openssl-devel


      [root@www nrpe-2.14]# make all

      [root@www nrpe-2.14]# make install-plugin

      [root@www nrpe-2.14]# make install-daemon

      [root@www nrpe-2.14]# make install-daemon-config


    c、配置NRPE

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


      81 allowed_hosts=127.0.0.1

      改成

      81 allowed_hosts=127.0.0.1,172.16.1.99        ;nagios監控服務器IP


    d、啓動NRPE守護進程

      

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


      vim /etc/rc.local      加入開機啓動腳本

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

      


      [root@www /]# netstat -antl | grep 5666

      tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN    


      關閉NRPE進程

      ps -aux | grep nagios

      kill pid


    e、測試NRPE功能

      在客戶端本機上測試

      [root@www /]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1

      NRPE v2.14 --看到“NRPE v2.14”就說明成功了      


    在服務器上安裝NRPE和配置Nagios服務

    a、安裝NRPE插件


      [root@NagiosServer NRPE]# tar -zxvf nrpe-2.14.tar.gz

      [root@NagiosServer NRPE]# cd nrpe-2.14

      [root@NagiosServer NRPE]# ./configure

    

      故障:checking for SSL headers... configure: error: Cannot find ssl headers

      解決方案:

      yum -y install openssl-devel


      [root@NagiosServer NRPE]# make all      

      [root@NagiosServer nrpe-2.14]# make install-plugin    ;通過make install-plugin 命令默認將check_nrpe插件安裝到/usr/local/nagios/libexec目錄下

      

    b、測試插件與客戶端是否能正常通信

      [root@NagiosServer /]# /usr/local/nagios/libexec/check_nrpe -H 172.16.1.100

      NRPE v2.14 --看到“NRPE v2.14”就說明成功了 

      [root@NagiosServer /]# /usr/local/nagios/libexec/check_nrpe -H 172.16.1.101

      NRPE v2.14


    c、定義一個check_nrpe監控命令

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

      添加如下命令

      define command{

              command_name check_nrpe

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

      }


    d、添加遠程主機監控

      vim /usr/local/nagios/etc/objects/services.cfg

      #################################  xiaodong  www  #####################################

      define service{

          use                  local-service,services-pnp

          host_name            www.shbztech.com

          service_description  PING

          check_command        check_ping!100.0,20%!500.0,60%

      }


      define service{

          use                  local-service,services-pnp

          host_name            www.shbztech.com

          service_description  SSH

          check_command        check_ssh

      }


      define service{

          use                  local-service,services-pnp

          host_name            www.shbztech.com

          service_description  http

          check_command        check_http

      }

      

      define service{

          use                  local-service,services-pnp

          host_name            www.shbztech.com

          service_description  users

          check_command        check_nrpe!check_users

      }


      define service{

          use                  local-service,services-pnp

          host_name            www.shbztech.com

          service_description  load

          check_command        check_nrpe!check_load

      }



      #################################  xiaodong  ftp  #####################################

      define service{

          use                  local-service,services-pnp

          host_name            ftp.shbztech.com

          service_description  PING

          check_command        check_ping!100.0,20%!500.0,60%

      }

      

      define service{

          use                  local-service,services-pnp

          host_name            ftp.shbztech.com

          service_description  SSH

          check_command        check_ssh

      }

      

      define service{

          use                  local-service,services-pnp

          host_name            ftp.shbztech.com

          service_description  ftp

          check_command        check_tcp!21

      }


      define service{

          use                  local-service,services-pnp

          host_name            ftp.shbztech.com

          service_description  users

          check_command        check_nrpe!check_users

      }


      define service{

          use                  local-service,services-pnp

          host_name            ftp.shbztech.com

          service_description  load

          check_command        check_nrpe!check_load

      }



      增加的配置是對www.shbztech.com和ftp.shbztech.com主機的當前用戶數、系統負載、磁盤空間利用率、swap內存使用4個方面進行監控

      監控遠程主機的命令方式:check_nrpe!遠程主機下定義的監控變量 而check_users、check_load等變量已經在Nagios客戶端nrpe.cfg文件中進行了定義。




    e、測試和啓動Nagios服務

      

      [root@NagiosServer /]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg


      [root@NagiosServer /]# service nagios restart


wKiom1PjPhXgj5UuAAgSHZDgnFI518.jpg

wKioL1PjPy7h0eIvAAV51n1rJaI252.jpg


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