NRPE监控远程主机——nagios扩展

NRPE简介:

NRPE是监控软件nagios的一个扩展,它被用于被监控的服务器上,向nagios监控平台提供该服务器的一些本地的情况。例如,cpu负载、内存使用、硬盘使用等等。NRPE可以称为nagios的for linux 客户端。

073433677.png

NRPE的基本机构:

  • check_nrpe插件:运行在nagios监控主机上

  • NRPE daemon:运行在被监控机如远程的linux主机上

nagios监控主机设置:

1.编译安装nrpe软件包

072524720.png

072524884.png

执行/usr/local/nagios/libexec/check_nrpe -H 192.168.10.2检查连接是否正常:

072524862.png

/usr/local/nagios/etc/objects/commands.cfg 模版中添加如下内容:

define    command {

    command_name check_nrpe

    command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$

}

072524170.png

  • command_name    check_nrpe  ——定义命令名称为check_nrpe,services.cfg必须使用

  • command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$  ——$USER1$代替/usr/local/nagios/libexec

这是定义实际运行的插件程序.这个命令行的书写要完全按照check_nrpe这个命令的用法.不知道用法的就用check_nrpe –h查看; -c后面带的$ARG1$参数是传给nrpe daemon执行的检测命令,之前说过了它必须是nrpe.cfg中所定义的5条命令中的其中一条。

eg:要监控mysql主机192.168.10.2的SWAP分区,需要在mysql主机的nrpe.cfg添加如下内容:

072543307.png

072543213.png

重启xinetd服务:

072543211.png

在监控主机上执行:

072543408.png

如果这里没有定义好,在服务器端监测时会报错误:NRPE command ‘check_swap’ not defined。

在nagios监控主机的/usr/local/nagios/etc/objects/services.cfg文件中添加对应的所要监控的资源:

define    service {

    use    generic-service

    host_name    mysqlhost

    service_description    Swap

    check_command    check_nrpe!check_swap

}

072642922.png

等被监控端nrpe安装完成后使用此命令检查配置文件是否有错:

072913214.png

 

远程被控机主机设置:

这里以http://wenzhongxiang.blog.51cto.com/6370734/1259339实验前提为基础进行操作:

在被监控端安装nagios-pluginsnrpe插件:

1.创建nagios用户:

072714938.png

2.编译安装nagios-plugins插件上:

[root@mysql ~]#cd  nagios-plugins-1.4.15

072714920.png

3.安装xinetd软件包:

072714724.png

4.编译安装nrpe软件包:

072714666.png

执行过程需要按照先后顺序:

072726884.png

注解信息:

  • make install-daemon         ——安装守护进程

  • make install-daemon-config ——安装配置文件

  • make install-xinetd                  ——安装xinetd脚本

5.配置nrpe,添加nrpe 服务:

在/etc/xinetd.d/nrpe配置文件的 

only_from = 127.0.0.1 后添加nagios监控主机的地址192.168.10.1,中间以空格分隔:

修改内容如下:

072726849.png

修改后的配置文件总览:

072726850.png

6.编辑/etc/services文件,增加NRPE服务模块:

vi /etc/services 

nrpe 5666/tcp       #nrpe  Service  jiankong(nrpe的监控端口

072726719.png

7.在配置文件/usr/local/nagios/etc/nrpe.cfg 的第214行取消其注解的“#”号:

command[check_disk]=/usr/local/nagios/libexec/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$

072745744.png

8.重启xinetd服务查看NRPE是否已经启动(显示5666端口已在监听表示服务正常,并确保防火墙允许此端口进入:

072745261.png

072745271.png

/usr/local/nagios/libexec/check_nrpe –h的使用方法

  • check_nrpe  –H :表示被监控的主机;

  •  -c  :需要执行的监控命令(监控命令必须是nrpe.cfg文件中定义的):

NRPE daemon只运行nrpe.cfg中所定义的命令!

072745102.png

9.重新加载nagios.cfg配置文件:

通过浏览器访问nagios的web页面,查看新添加的swap资源的监控情况:

072913998.png

注解信息:

services.cfg中使用check_nrpe的时候要带上“!”参数:

在/usr/local/nagios/etc/objects/services.cfg 添加如下内容:

072913309.png

define service {

    use    local-service

    host_name    mysqlhost

    service_groups    mysqlgroup

    service_description    SWAP

    check_command    check_nrpe!check_swap

    contact_groups    admins

    notification_interval    10

    notification_period    24x7

    notification_options    w,u,r,c

}

define service {

    use    local-service

    host_name    mysqlhost

    service_groups    mysqlgroup

    service_description    CPUload

    check_command    check_nrpe!check_load

    contact_groups    admins

    notification_interval    10

    notification_period    24x7

    notification_options    w,u,r,c

}

define service {

    use    local-service

    host_name    mysqlhost

    service_groups    mysqlgroup

    service_description    Free Space

    check_command    check_nrpe!check_disk

    contact_groups    admins

    notification_interval    10

    notification_period    24x7

    notification_options    w,u,r,c

}

define service {

    use    local-service

    host_name    mysqlhost

    service_groups    mysqlgroup

    service_description    TotalProcesses

    check_command    check_nrpe!check_total_procs

    contact_groups    admins

    notification_interval    10

    notification_period    24x7

    notification_options    w,u,r,c

}

 

define service {

    use    local-service        ——使用的模板

    host_name    mysqlhost        ——被监控主机名,该主机名必须是linux且运行着nrpe,而且必须是hosts.cfg中定义的

    service_groups    mysqlgroup

    service_description    ZombieProcesses        ——监控项目的描述

    check_command    check_nrpe!check_zombie_procs    ——监控命令是check_nrpe,commands.cfg中定义,check_disk参数nrpe.cfg中定义的

    contact_groups    admins

    notification_interval    10            ——通知间隔

    notification_period    24x7            ——通知周期设置

    notification_options    w,u,r,c        ——定义状态报警设置

}

142544645.png

072913380.png

通过访问http://192.168.10.1/nagios来查看监控信息,显示信息如下:

072938436.png

072938224.png

072938434.png

 

nagios通过邮件发送报警信息:

nagios除了丰富的监控功能外,报警功能相当的强大(常见的报警方法有:邮件和短信):

邮件报警的配置
/usr/local/nagios/etc/object/contact.cfg的配置文件中的email后添加管理员的邮箱:

define    contact {

    contact_name    nagiosadmin

    alias    Nagios Admin

    service_notification_period    24x7

    host_notification_period    24x7

    service_notification_options    w,u,c,r

    host_notification_options    d,u,r

    service_notification_commands    notify-service-by-email

    host_notification_commands        notify-host-by-email

    email        root@localhost.localdomain     #报警邮箱,以root为例。

}

截图如下:

072938908.png

注解:

072938418.png

  • 在commands.cfg文件中要定义发邮件的命令默认就存在。

  • hosts.cfgservices.cfg文件添加有关发通知的联系人信息和间隔等。

  • nagios利用本地的sendmail或postfix邮件服务器发送邮件,只要启动本地服务器的sendmail或postfix就可以了。

整个nagios部署到此告一段落,不足之处,请谅解。

欢迎关注微信公众号:小温研习社

欢迎关注yanxishe.png