添加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                           123456@qq.com       

        }

define contact{        ---》联系中可以加入手机登设参数

        contact_name                    huangsir            

        use                             generic-contact      ---》这里使用的模板就是模板文件中的contact定义      

        alias                           Nagios Admin           

        email                           123456@139.com       

        }

将定义好的联系加到联系组中:

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