Centos6.x Nagios安裝配置

1. Nagios安裝 - 服務端(192.168.0.11)
Centos6默認的yum源裏沒有nagios相關的rpm包,但是我們可以安裝一個epel的擴展源:
yum install -y epel-release

然後安裝nagios相關的包
yum install -y httpd nagios nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe
設置登錄nagios後臺的用戶和密碼:htpasswd -c /etc/nagios/passwd nagiosadmin
vim /etc/nagios/nagios.cfg
nagios -v /etc/nagios/nagios.cfg 檢測配置文件
啓動服務:service httpd start; service nagios start
瀏覽器訪問: http://ip/nagios

2. Nagios安裝 - 客戶端(192.168.0.12)
在客戶端機器上
yum install -y epel-release
yum install -y nagios-plugins nagios-plugins-all nrpe nagios-plugins-nrpe
vim /etc/nagios/nrpe.cfg  找到“allowed_hosts=127.0.0.1” 改爲 “allowed_hosts=127.0.0.1,192.168.0.11” 後面的ip爲服務端ip; 找到” dont_blame_nrpe=0” 改爲  “dont_blame_nrpe=1”
啓動客戶端 /etc/init.d/nrpe start

3. 監控中心(192.168.0.11)添加被監控主機(192.168.0.12)
cd /etc/nagios/conf.d/
vim 192.168.0.12.cfg                   //加入:
define host{
        use                     linux-server            
        host_name           192.168.0.12
        alias                       0.12
        address                 192.168.0.12
        }

define service{
        use                     generic-service
        host_name               192.168.0.12
        service_description     check_ping
        check_command           check_ping!100.0,20%!200.0,50%
        max_check_attempts 5
        normal_check_interval 1
}

define service{
        use                     generic-service
        host_name               192.168.0.12
        service_description     check_ssh
        check_command           check_ssh
        max_check_attempts      5    ;當nagios檢測到問題時,一共嘗試檢測5次都有問題纔會告警,如果該數值爲1,那麼檢測到問題立即告警
        normal_check_interval 1   ;重新檢測的時間間隔,單位是分鐘,默認是3分鐘
        notification_interval           60 ;在服務出現異常後,故障一直沒有解決,nagios再次對使用者發出通知的時間。單位是分鐘。如果你認爲,所有的事件只需要一次通知就夠了,可以把這裏的選項設爲0。
}

define service{
        use                     generic-service
        host_name               192.168.0.12
        service_description     check_http
        check_command           check_http
        max_check_attempts      5
        normal_check_interval 1
}


以上服務不依賴於客戶端nrpe服務,我們可以想象,我們在自己電腦上可以使用ping或者telnet探測遠程任何一臺機器是否存活、是否開啓某個端口或服務。 而當我們想要檢測客戶端上的某個具體服務的情況時,就需要藉助於nrpe了,比如想知道客戶端機器的負責或磁盤使用情況。

4.  繼續添加服務
服務端vim /etc/nagios/objects/commands.cfg
增加:define command{
        command_name    check_nrpe
        command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
        }
繼續編輯 vim /etc/nagios/conf.d/192.168.0.12.cfg
增加如下內容:define service{
        use     generic-service
        host_name       192.168.0.12
        service_description     check_load
        check_command           check_nrpe!check_load
        max_check_attempts 5
        normal_check_interval 1
}


define service{
        use     generic-service
        host_name       192.168.0.12
        service_description     check_disk_hda1
        check_command           check_nrpe!check_hda1
        max_check_attempts 5
        normal_check_interval 1
}

define service{
        use     generic-service
        host_name       192.168.0.12
        service_description     check_disk_hda2
        check_command           check_nrpe!check_hda2
        max_check_attempts 5
        normal_check_interval 1
}


說明:  check_nrpe!check_load :這裏的check_nrpe就是在commands.cfg剛剛定義的,check_load是遠程主機上的一個檢測腳本
在遠程主機上vim /etc/nagios/nrpe.cfg 搜索check_load,這行就是在服務端上要執行的腳本了,我們可以手動執行這個腳本
把check_hda1更改一下:/dev/hda1 改爲 /dev/sda1
再加一行command[check_hda2]=/usr/lib/nagios/plugins/check_disk -w 20% -c 10% -p /dev/sda2
客戶端上重啓一下nrpe服務: service nrpe restart
服務端也重啓一下nagios服務: service nagios restart

5.   配置告警
vim /etc/nagios/objects/contacts.cfg //增加:define contact{
        contact_name               123
        use                             generic-contact
        alias                           aming
        email              [email protected]
        }


define contact{
        contact_name               456
        use                             generic-contact
        alias                            aaa
        email              [email protected]
        }


define contactgroup{
        contactgroup_name           common
        alias                                  common
        members                          123,456
        }




然後在要需要告警的服務裏面加上contactgroup

define service{
        use     generic-service
        host_name       192.168.0.12
        service_description     check_load
        check_command           check_nrpe!check_load
        max_check_attempts 5
        normal_check_interval 1
        contact_groups        common
        notifications_enabled  1    ;是否開啓提醒功能。1爲開啓,0爲禁用。一般,這個選項會在主配置文件(nagios.cfg)中定義,效果相同。
        notification_period   24x7   ;發送提醒的時間段。非常重要的主機(服務)我定義爲7×24,一般的主機(服務)就定義爲上班時間。如果不在定義的時間段內,無論什麼問題發生,都不會發送提醒。        
        notification_options:w,u,c,r   ;這個是service的狀態。w爲waning, u爲unknown, c爲critical, r爲recover(恢復了),類似的還有一個  host對應的狀態:d,u,r   d = 狀態爲DOWN, u = 狀態爲UNREACHABLE , r = 狀態恢復爲OK,需要加入到host的定義配置裏。
}

參考:
調用短信接口   http://www.aminglinux.com/bbs/thread-7380-1-1.html
整合微信  http://www.aminglinux.com/bbs/thread-7917-1-1.html


6.   配置圖形顯示 pnp4nagios (請自行演示,課上不演示)
(1)安裝
yum install pnp4nagios rrdtool

(2)配置主配置文件
vim /etc/nagios/nagios.cfg  //修改如下配置

    process_performance_data=1   
    host_perfdata_command=process-host-perfdata
    service_perfdata_command=process-service-perfdata
    enable_environment_macros=1     

複製代碼


(3)修改commands.cfg
vim /etc/nagios/objects/commands.cfg  //註釋掉原有對process-host-perfdata和process-service-perfdata,重新定義

    define command {  
           command_name    process-service-perfdata  
           command_line    /usr/bin/perl /usr/libexec/pnp4nagios/process_perfdata.pl  
    }  
      
    define command {  
           command_name    process-host-perfdata  
           command_line    /usr/bin/perl /usr/libexec/pnp4nagios/process_perfdata.pl -d HOSTPERFDATA  
    }  

複製代碼



(4)修改配置文件templates.cfg
vim /etc/nagios/objects/templates.cfg define host {
        name       hosts-pnp
        register   0
        action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=_HOST_
        process_perf_data               1
}
define service {
        name       srv-pnp
        register   0
        action_url /pnp4nagios/index.php/graph?host=$HOSTNAME$&srv=$SERVICEDESC$
        process_perf_data               1
}


(5)修改host和service配置
vim /etc/nagios/conf.d/192.168.0.12.cfg
把 “define host{
        use                     linux-server”
改爲:
define host{
        use                     linux-server,hosts-pnp

修改對應的service,比如
把
define service{
        use     generic-service
        host_name       192.168.0.12
        service_description     check_disk_hda1
        check_command           check_nrpe!check_hda1
        max_check_attempts 5
        normal_check_interval 1
}
改爲:
define service{
        use     generic-service,srv-pnp
        host_name       192.168.0.12
        service_description     check_disk_hda1
        check_command           check_nrpe!check_hda1
        max_check_attempts 5
        normal_check_interval 1
}

(6) 重啓和啓動各個服務:
service nagios restart
service httpd restart
service npcd start

(7) 訪問測試
兩種訪問方法:
ip/nagios/
ip/pnp4nagios/

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