Nagios 監控服務器安裝及配置文檔

一:需求軟件
       RHEL 5.6
Nagios
Nagios-plugins
Nrpe
下載地址:
二:安裝準備工作
1:搭建yum服務器
       [root@loc ~]# vim /etc/yum.repos.d/rhel-source.repo
              [Server]
name=Server
baseurl=file:///mnt/Server
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
2:安裝環境所需要的安裝軟件包
       [root@loc ~]# yum –y install httpd php gcc glibc glibc-common gd gd-devel openssl openssl-devel
三:開始安裝nagios軟件
1:解壓軟件包
       [root@loc ~]# tar xf nagios-3.2.3.tar.gz -C /usr/src/
       [root@loc ~]# tar xf nagios-plugins-1.4.10.tar.gz -C /usr/src/
       [root@loc ~]# tar xf nrpe-1.8.tar.gz -C /usr/src/

 

2:創建用戶
       [root@loc ~]# groupadd nagcmd
       [root@loc ~]# useradd –G nagcmd nagios
       [root@loc ~]# usermod –G nagcmd apache

 

3:編譯安裝nagios
       [root@loc ~]# cd /usr/src/nagios-3.2.3
       [root@loc nagios-3.2.3]# ./configure --prefix=/usr/local/nagios --with-nagios-user=nagios --with-nagios-group=nagios --with-command-group=nagcmd
       [root@loc nagios-3.2.3]# make all                                         
       [root@loc nagios-3.2.3]# make install                              編譯安裝
       [root@loc nagios-3.2.3]# make install-init                        生成啓動腳本
       [root@loc nagios-3.2.3]# make install-commandmode              爲外部命令文件配置目錄權限
       [root@loc nagios-3.2.3]# make install-config                    生成配置文件
       [root@loc nagios-3.2.3]# make install-webconf                 生成一個與apache接口的配置文件

 

4:編譯安裝nagios-plugins
       [root@loc nagios-3.2.3]# cd ../nagios-plugins-1.4.10/
       [root@loc nagios-plugins-1.4.15]# ./configure –with-nagios-user=nagios –with-nagios-group=nagios –prefix=/usr/local/nagios
       [root@loc nagios-plugins-1.4.15]# make;make install
      
5:添加爲開機自啓動服務
       [root@loc ~]# chkconfig --add nagios
       [root@loc ~]# chkconfig nagios on

 

6:添加web用戶
       [root@loc ~]# htpasswd -c /usr/local/nagios/etc/passwrd.users nagiosadmin

 

7:啓動服務,檢查配置
       [root@loc ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg    檢查下nagios 配置文件的正確性
              Total Warnings: 0          查看這兩個爲“0”,說明配置文件沒問題,可以啓動服務了。
Total Errors:   0
       [root@loc ~]# /etc/init.d/httpd start
       [root@loc ~]# /etc/init.d/nagios start

 

8:登錄nagios
              Http://127.0.0.1/nagios   出現下面這個登錄界面:

登錄後的界面:


注意
       如果是開啓了selinux服務,就要執行下面兩步:
       [root@loc ~]# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin/
       [root@loc ~]# chcon -R -t httpd_sys_content_t /usr/local/nagios/share/

 

到此,安裝部分也就完成了!接下來我們就做下添加監控服務器配置
先觀看下監控原理圖:

四:配置nagios監控linux主機
1:主配置文件修改(改動部分內容)
       [root@loc ~]# vim /usr/local/nagios/etc/nagios.cfg
       # Definitions for monitoring the local (Linux) host
cfg_file=/usr/local/nagios/etc/objects/localhost.cfg                    定義監控本機服務(如不需要監控本機,將註釋這行)
cfg_file=/usr/local/nagios/etc/objects/hosts.cfg               添加此行,定義被監控的主機名和地址
cfg_file=/usr/local/nagios/etc/objects/hostgroups.cfg          添加此行,定義被監控主機的分組管理
cfg_file=/usr/local/nagios/etc/objects/contactgroups.cfg        添加此行,定義聯繫人組
cfg_dir/usr/local/nagios/etc/servers                                打開此行,定義需要監控的服務項

 

check_external_commands=1                                    定義在web界面下重啓nagios服務

command_check_interval=10s                                  定義命令檢查服務的間隔時間

 

[root@loc ~]# vim /usr/local/nagios/cfi.cfg
authorized_for_system_information=nagiosadmin,test
authorized_for_configuration_information=nagiosadmin,test
authorized_for_system_commands=nagiosadmin,test
authorized_for_all_services=nagiosadmin,test
authorized_for_all_hosts=nagiosadmin,test
authorized_for_all_service_commands=nagiosadmin,test
authorized_for_all_host_commands=nagiosadmin,test
可以定義多個用戶,添加到後面用逗號隔開就好。

 

2object文件配置(在此目錄下添加在.1中所講到的文件)
       創建聯繫人和聯繫人組配置文件
[root@loc objects]# vi contacts.cfg
define contact {
     contact_name                 admin                                          #聯繫人名
     alias                         system administrator               #別名
     service_notification_period    24x7                                     #服務通知的時間段
     host_notification_period       24x7                                    #主機通知的時間段
     service_notification_options    w,u,c,r                                 #當服務出現w—報警,u—未知,c—嚴重,r—從異常恢復到正常,在這四種情況下通知聯繫人
     host_notification_options       d,u,r                                   #當主機出現d----—當機,u—返回不可達,r—從異常情況恢復正常,在這3種情況下通知聯繫人
     service_notification_commands  notify-service-by-email        #服務出問題通知採用的命令
     host_notification_commands     notify-host-by-email           #同上
     email                        [email protected]     #指定聯繫的人email地址
     pager                                          13800138000                   #定義通過手機短信的方式發送警報的手機號碼
        pager                                          13810255206
}

 

[root@loc objects]# vi contactgroups.cfg
define contactgroup{
        contactgroup_name       sagroup                                 #定義組名
        alias                   system administrator group   #組別名
        members                admin                   #定義聯繫人名(contacts.cfg中的聯繫人名)
        }
創建被監控的主機和主機組文件
[root@loc objects]# vi hosts.cfg
       define host {
       host_name             web                      #被監控的主機名
       alias                      tomas                    #別名
       address                  192.168.2.6           #被監控主機地址
       contact_groups       sagroup                 #聯繫人組
       check_command     check-host-alive     #檢查主機狀態的名字
       check_period  24x7                            #提醒週期
       max_check_attempts     5                   #檢查失敗後重試的次數
       notification_interval      5                   #提醒的間隔時間
       notification_options       d,u,r                     #在什麼情況提醒
       }

 

       define host {
       host_name             nagios-server               
       alias                      tomas1                        
       address                  192.168.2.7          
       contact_groups       sagroup                
       check_command     check-host-alive    
       check_period  24x7                           
       max_check_attempts     5                  
       notification_interval      5                  
       notification_options       d,u,r                    
       }
       define host {
       host_name             linux                    
       alias                      tomas2                        
       address                  192.168.2.8          
       contact_groups       sagroup                
       check_command     check-host-alive    
       check_period  24x7                           
       max_check_attempts     5                  
       notification_interval      5                  
       notification_options       d,u,r                    
       }
      
       [root@loc objects]# vi hostgroups.cfg
       define hostgroup{
        hostgroup_name  sa-servers
        alias           sa servers
        members         web,nagios-server,linux
        }

 

       配置監控主機服務項
       /usr/local/nagios/etc/servers/web.cfg                                 #在這下面依次再建nagios-server.cfglinux.cfg
       define service{
        host_name               web                              #必須是hosts.cfg中定義的主機
        service_description           check-host-alive            #
        check_command           check-host-alive           #commands.cfg文件中定義或在nrpe.cfg裏面定義的命令
        max_check_attempts        5                                 #最大重試次數
        normal_check_interval        5                                 #檢查間隔的單位是分鐘
        retry_check_interval          2                                 #檢查間隔的單位是分鐘
        check_period              24x7                           
        notification_interval           10                                #探測到故障後,每隔多長時間發送一次報警信息,單位是分鐘
        notification_period           24x7                            #通知選項跟聯繫人配置文件相同
        notification_options           w,u,c,r                        
        contact_groups             sagroup                        #配置文件contactgroup.cfg定義的組名稱
        }

 

define service{
        host_name                 web
        service_description           check_tcp 80
              check_command           check_tcp!80
        check_period              24x7
        max_check_attempts        4
        normal_check_interval        3
        retry_check_interval          2
        contact_groups             sagroup
        notification_interval           10
        notification_period           24x7
        notification_options           w,u,c,r
        }

 

define service{
        host_name               web
        service_description           cpu load
        check_command             check_nrpe!check_load[A1]  [A1] 
        check_period              24x7
        max_check_attempts        4
        normal_check_interval        3
        retry_check_interval          2
        contact_groups             sagroup
        notification_interval           10
        notification_period           24x7
        notification_options           w,u,c,r
        }

 

define service{
        host_name               web
        service_description           total-procs
        check_command          check_nrpe!check_total_procs
        check_period              24x7
        max_check_attempts        4
        normal_check_interval        3
        retry_check_interval          2
        contact_groups             sagroup
        notification_interval           10
        notification_period           24x7
        notification_options           w,u,c,r
        }

 

監控主機文件配置完成了,我們可以重啓下服務,查看下nagios的界面。
       驗證下配置文件:
       [root@loc objects]# /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/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'...
Processing object config file '/usr/local/nagios/etc/objects/hosts.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/hostgroups.cfg'...
Processing object config file '/usr/local/nagios/etc/objects/contactgroups.cfg'...
Processing object config directory '/usr/local/nagios/etc/servers'...
Processing object config file '/usr/local/nagios/etc/servers/linux.cfg'...
Processing object config file '/usr/local/nagios/etc/servers/web.cfg'...
Processing object config file '/usr/local/nagios/etc/servers/nagios-server.cfg'....
     Read object config files okay...
Running pre-flight check on configuration data...
Checking services...
        Checked 12 services.
Checking hosts...
        Checked 2 hosts.
Checking host groups...
        Checked 2 host groups.
Checking service groups...
        Checked 0 service groups.
Checking contacts...
        Checked 3 contacts.
Checking contact groups...
        Checked 2 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                 #表示文件沒有任何錯誤

 

重新啓動服務:
       [root@loc objects]# /etc/init.d/nagios restart
Running configuration check...done.
Stopping nagios: done.
Starting nagios: done.

 

[root@loc objects]# /etc/init.d/httpd restart
登錄界面:
Http://IP/nagios


可以清晰的看到前面所做的監控配置了。

 

遠程監控插件NRPE的安裝就不再記錄了,在用NRPE監控的時候記得去修改services.cfgcommand.cfg配置文檔,網絡上文檔都很詳細。

 

 

在被監控的服務器上只要安裝以下這兩個軟件包就好:
Nagios-plugins
Nrpe









































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