Nagios3.2.0在CentOS5.3上安裝和配置

 一、安裝支持包

#yum -y install gcc gcc-c++ gd gd-devel php httpd openssl-devel 注:3.2的nagios,web首頁已經由html變爲php,必須安裝php支持
二、安裝nagios
創建用戶
#useradd  nagios
#passwd nagios
創建組
#groupadd nagcmd
#usermod -G nagcmd nagios
#usermod -G nagcmd apache
安裝
#tar xzf nagios-3.2.0.tar.gz
#cd nagios-3.2.0
#./configure --with-command-group=nagcmd --prefix=/usr/local/nagios
#make all
#make install        # 使用make install來安裝主程序,CGI和HTML文件
#make install-init        # 使用make install-init在/etc/rc.d/init.d安裝啓動腳本
#make install-config        # 使用make install-cofig來安裝示例配置文件,安裝的路徑是/usr/local/nagios/etc.
#make install-commandmode        # 使用make install-commandmode來配置目錄權限
nagios目錄功能的簡要說明:
bin    Nagios執行程序所在目錄,nagios文件即爲主程序
etc    Nagios配置文件位置
sbin    Nagios Cgi文件所在目錄,也就是執行外部命令所需文件所在的目錄
Share    Nagios網頁文件所在的目錄
var    Nagios日誌文件、spid 等文件所在的目錄
var/archives    日誌歸檔目錄
var/rw    用來存放外部命令文件
配置apache
#vi /etc/httpd/conf/httpd.conf        #加入末尾
ScriptAlias /nagios/cgi-bin "/usr/local/nagios/sbin"
#  SSLRequireSSL
   Options ExecCGI
   AllowOverride None
   Order allow,deny
   Allow from all
#  Order deny,allow
#  Deny from all
#  Allow from 127.0.0.1
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   Require valid-user
Alias /nagios "/usr/local/nagios/share"
#  SSLRequireSSL
   Options None
   AllowOverride None
   Order allow,deny
   Allow from all
#  Order deny,allow
#  Deny from all
#  Allow from 127.0.0.1
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /usr/local/nagios/etc/htpasswd.users
   Require valid-user
創建apache目錄驗證文件
#htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
New password: (輸入密碼)
Re-type new password: (再輸入一次密碼)
Adding password for user nagiosadmin
#service httpd restart
配置nagios配置文件
#vi /usr/local/nagios/etc/objects/contacts.cfg        # 將裏面的email地址改爲自己的email地址
 
三、安裝nagios插件
#tar xzf nagios-plugins-1.4.13.tar.gz
#cd nagios-plugins-1.4.13
#./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/usr/local/nagios
#make && make install
 
四、啓動nagios
添加服務
#chkconfig --add nagios
#chkconfig nagios on
檢查配置文件
#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
啓動nagios
#service nagios start
啓動apache
#service httpd start
訪問
http://localhost/nagios
輸入用戶名密碼
 
五、安裝nrpe
#tar xzvf nrpe-2.12.tar.gz
#cd nrpe-2.12
#./configure
#make all
#make install-plugin        #服務器端只要安裝nrpe監控插件就行
在/usr/local/nagios/etc/objects/commands.cfg中定義check_nrpe命令
#vi /usr/local/nagios/etc/objects/commands.cfg
# 'check_nrpe ' command definition
define command{
        command_name check_nrpe
        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
六、在被監控機上安裝nagios插件和nrpe
#useradd nagios
#tar xzvf nagios-plugins-1.4.13.tar.gz
#cd nagios-plugins-1.4.13
#./configure
#make
#make install
#chown nagios.nagios /usr/local/nagios/
#chown -R nagios.nagios /usr/local/nagios/libexec/
 
tar xzvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure
make all       
make install-plugin         #安裝nrpe插件,本監控端可以不裝      
make install-daemon        # 安裝nrpe守護進程       
make install-daemon-config         #安裝nrpe配置文件
#vi /usr/local/nagios/etc/nrpe.cfg        #修改nrpe配置文件,允許Nagios監控服務器(192.168.0.19)監控
allowed_hosts=127.0.0.1,192.168.0.19        #多臺機器用逗號隔開
#/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d        以獨立守護進程啓動nrpe,也可以使用xinetd啓動nrpe,具體清查看nrpe官方文檔。
#cat >> /etc/rc.d/rc.local #開機自動啓動nrpe
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
EOF
# /usr/local/nagios/libexec/check_nrpe -H localhost        #檢查nrpe是否安裝正常
NRPE v2.12
#netstat -tln        #查看端口
tcp        0      0 0.0.0.0:5666                0.0.0.0:*   
我們需要在/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
下面的命令是自己定義的:
#監控交換分區的使用情況,使用超過20%時爲警告狀態,超過10%時爲嚴重狀態
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10%
#監控根分區磁盤使用情況
command[check_disk_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
七、nagios監控流程
NRPE總共由兩部分組成:
(1).check_nrpe插件,運行在監控主機上。
(2).NRPE daemon,運行在遠程的linux主機上(通常就是被監控機)
按照上圖,整個的監控過程如下:
當Nagios需要監控某個遠程linux主機的服務或者資源情況時:
1).nagios會運行check_nrpe插件,我們要在nagios配置文件中告訴它要檢查什麼.
2).check_nrpe插件會通過SSL連接到遠程的NRPE daemon.
3).NRPE daemon會運行相應的nagios插件來執行檢查本地資源或服務.
4).NRPE daemon將檢查的結果返回給check_nrpe插件,插件將其遞交給nagios做處理.
注意:NRPE daemon需要nagios插件安裝在遠程被監控linux主機上,否則,daemon不能做任何的監控.
八、nagios配置文件
#控制cgi訪問的配置文件
cgi.cfg
#Nagios主配置文件
nagios.cfg
#resource.cfg定義了一些變量,以便被其他文件引用,如$USER1$
resource.cfg
# objects是一個目錄,用於定義Nagios對象
objects
# servers是自己創建的一個目錄,Nagios可以加載一個目錄下面的所有配置文件(需要在nagios.cfg中配置)
servers
./objects:
#命令定義配置文件,裏面定義的命令可以被其他文件引用
commands.cfg
#聯繫人和聯繫人組配置文件
contacts.cfg
#監控本地機器的配置文件
localhost.cfg
#監控打印機的一個事例配置文件(默認未啓用)
printer.cfg
#監控路由器的一個事例配置文件(默認未啓用)
switch.cfg
#模板配置文件,在此可以定義模板,在其他文件中引用
templates.cfg
#定義監控時間段的配置文件
timeperiods.cfg
#監控Windows的一個事例配置文件(默認未啓用)
windows.cfg
./servers:
#自己創建的主機羣組配置文件
hostgroup.cfg
#自己創建的監控遠程Linux主機的配置文件
sectop.cfg
配置文件是怎樣引用的?
 
nagios主要是監控一臺主機的各種信息,包括本機資源以及對外的服務等等.這些在nagios裏面都是被定義爲一個個的項目(nagios稱之爲服
務,爲了與主機提供的服務相區別,我這裏用項目這個詞),而實現每個監控項目,則需要通過commands.cfg文件中定義的命令。
爲了不必重複定義一些項目,Nagios引入了一個模板配置文件(templates.cfg),將一些共性的屬性定義成模板,以便於多次引用。
我們現在有一個監控項目是監控一臺機器的web服務是否正常, 我們需要哪些元素呢?最重要的有下面三點:首先是監控哪臺機器,然後是這個監控要用什麼命令實現,最後就是出了問題的時候要通知哪個聯繫人。
我們首先應該在commands.cfg中定義監控遠程服務和資源的命令,以及如何發送郵件的命令。大部分監控遠程服務和資源的命令的命令通過/usr/local/nagios/libexec下的腳本實現,如ping命令爲check_ping。
/usr/local/nagios/libexec下的腳本命令的使用發法可以通過-h參數查看
然後我們在contacts.cfg文件中定義聯繫人和聯繫人組,在timeperiods.cfg中定義監控時間段。
最後我們在服務器監控配置文件中引用前面定義的元素來監控服務器狀態。
下面引用配置文件中部分配置做說明:
#vi /usr/local/nagios/etc/resource.cfg
#定義$USER1$變量,設置插件路徑
$USER1$=/usr/local/nagios/libexec
#vi /usr/local/nagios/etc/objects/commands.cfg
#定義check-host-alive命令
define command{
        command_name    check-host-alive  #命令名稱
        command_line    $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
        }
#上面的$USER1$和$HOSTADDRESS$引用自已定義的配置文件。變量不需現定義才能被引用。
# 'check_nrpe ' command definition
define command{
        command_name check_nrpe
        command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
#vi /usr/local/nagios/etc/objects/contacts.cfg
#定義聯繫人
define contact{
        contact_name                    nagiosadmin             ; Short name of user
 
      use                             generic-contact         ; Inherit
default values from generic-contact template (defined above)
        alias                           Nagios Admin            ; Full name of user
        email                           
         ; #上面的generic-contact在templates.cfg中定義。
#定義聯繫人組
define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 nagiosadmin   #在此可以加入多個聯繫人,中間用逗號隔開
        }
#vi /usr/local/nagios/etc/objects/timeperiods.cfg
#定義監控的時間段
define timeperiod{
        timeperiod_name 24x7   #監控所有時間段(7*24小時)
        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
        }
#vi /usr/local/nagios/etc/objects/templates.cfg
#定義generic-contact聯繫人模板,並非真正的聯繫人,真正的聯繫人在contacts.cfg中定義
define contact{
        name                            generic-contact         ; The name of this contact template
        service_notification_period     24x7                    ; service notifications can be sent anytime
        host_notification_period        24x7                    ; host notifications can be sent anytime
 
      service_notification_options    w,u,c,r,f,s             ; send
notifications for all service states, flapping events, and scheduled
downtime events
        host_notification_options       d,u,r,f,s 
             ; send notifications for all host states, flapping events,
and scheduled downtime events
        service_notification_commands   notify-service-by-email ; send service notifications via email
        host_notification_commands      notify-host-by-email    ; send host notifications via email
 
      register                        0                       ; DONT
REGISTER THIS DEFINITION - ITS NOT A REAL CONTACT, JUST A TEMPLATE!
        }
------------------------------------------------------------------------------------------------------------------
service_notification_period     24x7
服務出了狀況通知的時間段,這個時間段就是上面在timeperiods.cfg中定義的.
host_notification_period        24x7
主機出了狀況通知的時間段, 這個時間段就是上面在timeperiods.cfg中定義的
service_notification_options    w,u,c,r
當服務出現w—報警(warning),u—未知(unkown),c—嚴重(critical),或者r—從異常情況恢復正常,在這四種情況下通知聯繫人.
host_notification_options       d,u,r
當主機出現d­­­­—當機(down),u—返回不可達(unreachable),r—從異常情況恢復正常,在這3種情況下通知聯繫人
service_notification_commands   notify-service-by-email
服務出問題通知採用的命令notify-service-by-email,這個命令是在commands.cfg中定義的,作用是給聯繫人發郵件.
host_notification_commands      notify-host-by-email
同上,主機出問題時採用的也是發郵件的方式通知聯繫人
------------------------------------------------------------------------------------------------------------------
#定義generic-host主機模板
define host{
        name                            generic-host    ; The name of this host template
        notifications_enabled           1               ; Host notifications are enabled
        event_handler_enabled           1               ; Host event handler is enabled
        flap_detection_enabled          1               ; Flap detection is enabled
        failure_prediction_enabled      1               ; Failure prediction is enabled
        process_perf_data               1               ; Process performance data
        retain_status_information       1               ; Retain status information across program restarts
        retain_nonstatus_information    1               ; Retain non-status information across program restarts
        notification_period             24x7            ; Send host notifications at any time
        register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
        }
#定義Linux主機模板
define host{
        name                            linux-server    ; The name of this host template
        use                             generic-host    ; This template inherits other values from the generic-host template
        check_period                    24x7            ; By default, Linux hosts are checked round the clock
        check_interval                  5               ; Actively check the host every 5 minutes
        retry_interval                  1               ; Schedule host check retries at 1 minute intervals
        max_check_attempts              10              ; Check each Linux host 10 times (max)
        check_command                   check-host-alive ; Default command to check Linux hosts
        notification_period             workhours       ; Linux admins hate to be woken up, so we only notify during the day
                                                        ; Note that the notification_period variable is being overridden from
                                                        ; the value that is inherited from the generic-host template!
        notification_interval           120             ; Resend notifications every 2 hours
        notification_options            d,u,r           ; Only send notifications for specific host states
        contact_groups                  admins          ; Notifications get sent to the admins by default
        register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
        }
#在nagios.cfg配置文件中開啓對/usr/local/nagios/etc/servers/中配置文件的引用。
cfg_dir=/usr/local/nagios/etc/servers
#遠程Linux主機監控文件,如果監控多臺主機只需簡單複製修改即可。
#我們應該牢記sectop.cfg用到的命令在commands.cfg中定義,在commands.cfg中定義的命令用到/usr/local/nagios/libexec下的插件(命令)。
#vi /usr/local/nagios/etc/servers/sectop.cfg
#定義主機
define host{
        use                     linux-server            ; Name of host template to use
                                                        ; This host definition will inherit all variables that are defined
                                                        ; in (or inherited by) the linux-server host template definition.
        host_name               sectop
        alias                   Docs
        address                 192.168.99.200
        }
#定義Ping遠程Linux主機
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       sectop
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%   ;check_ping命令在commands.cfg中定義,後跟兩個參數,命令及參數間用!分割。
        }
#檢查遠程Linux主機根分區使用情況,check_nrpe命令必須在/usr/local/nagios/etc/objects/commands.cfg中定義(默認未定義)
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       sectop
        service_description             Root Partition
        check_command                   check_nrpe!check_disk_root
        }
#檢查遠程Linux主機的登錄人數
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       sectop
        service_description             Current Users
        check_command                   check_nrpe!check_users
        }
#檢查遠程Linux的主機的負載
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       sectop
        service_description             Current Load
        check_command                   check_nrpe!check_load
        }
#檢查遠程Linux主機swap分區使用情況
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       sectop
        service_description             Swap Usage
        check_command                   check_nrpe!check_swap
        }
#檢查遠程Linux主機的SSH服務
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       sectop
        service_description             SSH
        check_command                   check_ssh
        notifications_enabled           0
        }
#檢查遠程Linux主機的HTTP服務
define service{
        use                             generic-service         ; Name of service template to use
        host_name                       sectop
        service_description             HTTP
        check_command                   check_http
        notifications_enabled           0
        }
#vi /usr/local/nagios/etc/servers/hostgroup.cfg
#定義主機組(localhost.cfg中有類似的主機組設置,我已將其註釋掉,否則可能會有衝突)
define hostgroup{
        hostgroup_name  linux-servers ; The name of the hostgroup
        alias           Linux Servers ; Long name of the group
        members         localhost,sectop     ; Comma separated list of hosts that belong to this group
        }
#define hostgroup{
#        hostgroup_name  windows-servers ; The name of the hostgroup
#        alias           Windows Servers ; Long name of the group
#        members         print     ; Comma separated list of hosts that belong to this group
#        }
===============================================================================
#完成監控主機配置文件的配置後使用下面命令檢查配置文件的正確性:
#/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#確定無誤後重啓Nagios:
#service nagios restart
        
文章來源:
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章