nagios部署監控多臺client

添加client節點

####添加一臺client:新加入的是一臺開啓了防火牆的阿里雲服務器

首先搭建client環境

#監控Linux、Unix主機,安裝客戶端

添加nagios用戶

useradd -m nagios -s /sbin/nologin

#安裝插件nagios-plugins

cd /home/huang/tools/

wget http://www.nagios-plugins.org/download/nagios-plugins-2.1.1.tar.gz

tar xfz nagios-plugins-2.1.1.tar.gz

cd nagios-plugins-2.1.1

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

make

make install

chown -R nagios.nagios /usr/local/nagios/

#安裝NRPE

cd /home/huang/tools/

wget https://github.com/NagiosEnterprises/nrpe/archive/3.0.tar.gz

tar xf 3.0.tar.gz

cd nrpe-3.0/

./configure

make all

make install

make install-plugin

make install-daemon

make install-config

#修改配置文件,添加nagios主機

sed -i 's#allowed_hosts=127.0.0.1#allowed_hosts=127.0.0.1,192.168.1.155#g' /usr/local/nagios/etc/nrpe.cfg


##編輯hosts.cfg配置文件,添加阿里雲服務器的信息

vim ../objects/hosts.cfg

1、define host{

        use                     linux-server          

        host_name               client01

        alias                   client01

        address                 120.26.68.152

        }

2、加入組

define hostgroup{

        hostgroup_name  linux-servers 

        alias           Linux Servers 

        members         nagios_server,client01(client01新加入的)   

        }

3、定義主動模式(不需要nrpe服務)   此時是主動拉取數據,所以並沒有用到nrpe,也就是不使用check_nrpe,也不需要在客戶端開啓nrpe服務

cd /usr/local/nagios/etc/

mkdir services

chown -R nagios.nagios services

cd services

vim port-client.cfg(nagios服務的services目錄下)

define service{

        use     generic-service

        host_name       client01

        service_description     ntp_port

        check_period 24x7

        check_interval 5

        retry_interval 1

        max_check_attempts 3

        check_command      check_http!-p 123

        notification_period 24x7

        notification_interval 30

        notification_options  w,u,c,r

        contact_groups admins

}

3、手動測試數據是否有收集

[root@RS1 services]# /usr/local/nagios/libexec/check_tcp -H 120.26.68.152 -p 123

CRITICAL - Socket timeout after 10 seconds

報錯如上

解決辦法:防火牆的123端口沒有向外開放(由於不想開放這個端口,所以不理會這個報錯,重新監控其他端口)

這裏選擇監控sshd服務的端口22389

當監控sshd的端口22389

[root@RS1 services]# /usr/local/nagios/libexec/check_tcp -H 120.26.68.152 -p 22389

TCP OK - 0.092 second response time on 120.26.68.152 port 22389|time=0.092055s;;;0.000000;10.000000

修改配置文件的123端口爲22389

重啓檢測語法以及重啓服務,在web頁面觀察到如下錯誤:

check_tcp: Port must be a positive integer

手動能夠收集數據,但是nagios的web界面報錯:check_tcp: Port must be a positive integer


解決辦法:修改service配置文件:

check_command      check_tcp!22389(不需要加-p了)


#####主動模式小結

一般客戶端對外開啓的服務,用主動模式監控,例如:port,uri

主動模式的監控配置過程:

1、在服務端的命令行把要監控的命令先調試好

2、在commands.cfg裏定義好命令,同時調用命令行的插件,先手動收集一下數據

3、在服務的配置文件裏定義要監控的服務,調用commands.cfg裏定義的監控命令


創建第二臺client服務器,ip爲192.168.1.11

首先還是搭建nrpe環境,這裏省略

這次選擇客戶端被動模式收集數據

在client02上操作

[root@node2 ~]# /usr/local/nagios/libexec/check_tcp -H 192.168.1.11 -p 80

TCP OK - 0.001 second response time on 192.168.1.11 port 80|time=0.000810s;;;0.000000;10.000000

手動抓取數據正常,於是配置服務器端:

define service{

        use                     generic-service

        host_name               client02   ----》已經定義了hosts文件(192.168.1.11爲client02)

        service_description     web_port

        check_command           check_nrpe!check_tcp 

}

然後重啓服務,出現如下錯誤:

NRPE: Command 'check_tcp' not defined 


解決辦法:這是因爲客戶端配置文件nrpe.cfg並沒有定義check_tcp這個命令,編輯配置文件,添加下面一行:

command[check_tcp]=/usr/local/nagios/libexec/check_tcp -H 192.168.1.11 -p 80


然後在服務端進行手動測試抓取數據:

[root@RS1 etc]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.11 -c check_tcp

TCP OK - 0.001 second response time on 192.168.1.11 port 80|time=0.000670s;;;0.000000;10.000000

成功解決

然後被動模式成功

wKiom1e-54OSs625AACuhXzo46g138.jpg


#########對每個服務進行分組(好幾臺服務器監控的服務都是load負載)

新建:vim /usr/local/nagios/etc/services/servicegroup.cfg

define servicegroup{

        servicegroup_name  load    ----》這裏對應service配置文件的service_description

        alias           Linux Servers 

        members         nagios_server,load,client01,load,client02,load 

        }

將三個主機(nagios_server、client01、client02)的監控的load服務定義到一個服務組中,

修改server.cfg中的description如下:

vim ../objects/services.cfg

define service{

        use                     generic-service

        host_name               nagios_server    由於這裏只定義了一個主機,所以還是顯示了一個

        service_description     load      -----》這裏改成和服務組定義的名稱一致

        check_command           check_nrpe!check_load

}

wKioL1e-50OSqPt2AABUJqHJxAg678.jpg


#####模板選擇與修改

編輯的services.cfg

define service{

        use     generic-service      ----》這裏使用的是generic-service這個模板,在配置文件templates.cfg中有定義,自己可以在模板中定義一個別的service模板,然後這裏修改就行

        host_name       client01

        service_description     ntp_port

        check_period 24x7

        check_interval 5

        retry_interval 1

        max_check_attempts 3

        check_command      check_http!22389

        notification_period 24x7

        notification_interval 30

        notification_options  w,u,c,r

        contact_groups admins

}    ----》後面這些和模板中相同的參數配置都可以省略掉,因爲和模板配置文件一樣,剛好就調用了模板文件

模板定義:vim templates.cfg

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                       

        process_perf_data               1                       

        retain_status_information       1                       

        retain_nonstatus_information    1                       

        is_volatile                     0                       

        check_period                    24x7                    

        max_check_attempts              3                       

        normal_check_interval           10          這個可以稍微改小一點              

        retry_check_interval            2                       

        contact_groups                  admins                 

        notification_options            w,u,c,r        warning unknown critical recovery            

        notification_interval           60                      

        notification_period             24x7                    

         register                        0                     

        }


########定義分組報警

vim contacts.cfg

以下是默認聯繫人

define contact{

        contact_name                    nagiosadmin            

        use                             generic-contact         

        alias                           Nagios Admin           

        email                           nagios@localhost       

        }


自定義聯繫人:

define contact{

        contact_name                    huang            

        use                             generic-contact         

        alias                           Nagios Admin           

        email                           [email protected]       

        }

define contact{        ---》聯繫中可以加入手機登設參數

        contact_name                    huangsir            

        use                             generic-contact      ---》這裏使用的模板就是模板文件中的contact定義      

        alias                           Nagios Admin           

        email                           [email protected]       

        }

將定義好的聯繫加到聯繫組中:

define contactgroup{   -----------》默認聯繫組

        contactgroup_name       admins

        alias                   Nagios Administrators

        members                 nagiosadmin

        }


新增聯繫組:

define contactgroup{

        contactgroup_name       sas

        alias                   Nagios Administrators

        members                 huang,huangsir

        }

然後修改模板中service的定義,將

contact_groups                  admins改爲

contact_groups                  admins,sas  

提示:根據不同的監控內容將報警設置分配不同,這時候模板就可以刪掉這個參數,依次在監控的內容中定義需要報警的組即可,如下:

define service{

        use                     generic-service

        host_name               nagios_server    

        service_description     load      

        check_command           check_nrpe!check_load

        contact_groups          admins,sas    ---》單獨報警一個組也行,根據報警的重要性來分配

}


#######聯繫人的模板

vim templates.cfg

# Generic contact definition template - This is NOT a real contact, just a template!


define contact{

        name                            generic-contact         

        service_notification_period     24x7                    

        host_notification_period        24x7                    

        service_notification_options    w,u,c,r,f,s             

        host_notification_options       d,u,r,f,s               

        service_notification_commands   notify-service-by-email     這個命令在commands.cfg配置文件中有定義

        host_notification_commands      notify-host-by-email    

        register                        0                       

        }


vim  commands.cfg

定義了notify-service-by-email、notify-host-by-email這兩個命令,如果要使用短信報警

這裏就需要在後面用逗號隔開,再者是在commands配置文件中定義命令

新建菜鳥學習交流羣:584498750


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