当前位置: 首页 > 科技观察

Nagios使用NRPE监控Linux主机_0

时间:2023-03-20 02:17:33 科技观察

一、简介1、NRPE简介NRPE是Nagios的一个功能扩展,可以在远程Linux/Unix主机上执行插件。通过在远程服务器上安装NRPE插件和Nagios插件,Nagios监控平台可以提供服务器本地的情况,如CPU负载、内存占用、磁盘占用等,这里调用Nagios监控端Nagios服务器,远程监控主机称为Nagios客户端。Nagios监控远程主机的方式有很多种,包括SNMP、NRPE、SSH、NCSA等,下面介绍它是如何通过NRPE监控远程Linux主机的。NRPE(NagiosRemotePluginExecutor)是一个守护进程,用于在远程服务器上运行监控命令。用于让Nagios监控端根据安装方式触发远程主机的检测命令,并将检测结果返回给监控端。.其执行开销远低于基于SSH的检测方式,检测过程不需要远程主机的系统账户信息,安全性也高于SSH检测方式。2、NRPE的工作原理NRPE由两部分组成:check_nrpe插件:位于监控主机上nrpedaemon:运行在远程主机上,通常是被监控的agent注意:nrpedaemon需要Nagios-plugins插件的支持-中,否则守护进程无法做任何事情监控详细介绍了NRPE的工作原理。当Nagios需要监控远程Linux主机的服务或资源状态时:第一:Nagios会运行check_nrpe插件,告诉它要检查什么;第二:check_nrpe插件会连接远程的NRPEdaemon,使用的方式是SSL;然后:NRPE守护进程会运行相应的Nagios插件来执行检查;finally:NRPEdaemon会将校验结果返回给check_nrpe插件,插件再提交给nagios进行处理。2.在被监控端安装nagios-plugins插件和NRPE1,添加nagios用户[root@ClientNrpe~]#useradd-s/sbin/nologinnagios2,安装nagios-plugins,因为NRPE依赖这个插件[root@ClientNrpe~]#yum-yinstallgccgcc-c++makeopensslopenssl-devel[root@ClientNrpe~]#tarxfnagios-plugins-2.0.3.tar.gz[root@ClientNrpe~]#cdnagios-plugins-2.0.3[root@ClientNrpenagios-plugins-2.0.3]#./configure--with-nagios-user=nagios--with-nagios-group=nagios[root@ClientNrpenagios-plugins-2.0.3]#make&&makeinstall#注意:如何监控mysql需要添加--with-mysql3、安装NRPE[root@ClientNrpe~]#tarxfnrpe-2.15.tar.gz[root@ClientNrpe~]#cdnrpe-2.15[root@ClientNrpe-2.15]#./configure--with-nrpe-user=nagios\>--with-nrpe-group=nagios\>--with-nagios-user=nagios\>--with-nagios-group=nagios\>--enable-command-args\>--enable-ssl[root@ClientNrpenrpe-2.15]#makeall[root@ClientNrpenrpe-2.15]#makeinstall-plugin[root@ClientNrpenrpe-2.15]#makeinstall-daemon[root@ClientNrpenrpe-2.15]#makeinstall-daemon-config4,配置NRPE[root@ClientNrpe~]#grep-v'^#'/usr/local/nagios/etc/nrpe.cfg|sed'/^$/d'log_facility=daemonpid_file=/var/run/nrpe.pidserver_port=5666#监听端口nrpe_user=nagiosnrpe_group=nagiosallowed_hosts=192.168.0.105#Allowedaddress通常是Nagiosserverdont_blame_nrpe=0allow_bash_command_substitution=0debug=0command_timeout=60connection_timeout=300command[check_users]=/usr/userlocal/nagios/lib_exec/w5-c10command[check_load]=/usr/local/nagios/libexec/check_load-w15,10,5-c30,25,20命令[check_hda1]=/usr/local/nagios/libexec/check_disk-w20%-c10%-p/dev/hda1command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs-w5-c10-sZcommand[check_total_procs]=/usr/local/nagios/libexec/check_procs-w150-c2005,startNRPE#Startasadaemon[root@ClientNrpe~]#/usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe.cfg-d[root@ClientNrpe~]#netstat-tulpn|grepnrpetcp000.0.0.0:56660.0.0.0:*LISTEN22597/nrpetcp00:::5666:::*LISTEN22597/nrpe有两种管理nrpe服务的方式,nrpe有两种操作模式:-i#Runasaserviceunderrinetdorxinetd-d#Runasastandalonedaemon可以为nrpe编写启动脚本,让nrpe运行在standardalone模式下:[root@ClientNrpe~]#cat/etc/init.d/nrped#!/bin/bash#chkconfig:23458812#description:NRPEDAEMONNRPE=/usr/local/nagios/bin/nrpeNRPECONF=/usr/local/nagios/etc/nrpe.cfgcase"$1"instart)echo-n"StartingNRPEdaemon..."$NRPE-c$NRPECONF-decho"done.";;停止)echo-n“StoppingNRPEdaemon...”pkill-unagiosnrpeecho“完成。”;;重启)$0stopsleep2$0start;;*)echo"用法:$0start|stop|restart";;esacexit0[root@ClientNrpe~]#chmod+x/etc/init.d/nrped[root@ClientNrpe~]#chkconfig--addnrped[root@ClientNrpe~]#chkconfignrpedon[root@ClientNrpe~]#servicenrpedstartStartingNRPEdaemon...done.[root@ClientNrpe~]#netstat-tnlpActiveInternetconnections(onlyservers)ProtoRecv-QSend-QLocalAddressForeignAddressStatePID/Programnametcp000.0.0.0:220.0.0.0:*LISTEN1031/sshdtcp00127.0.0.1:250.0.0.0:*8/master11tcp000.0.0.0:56660.0.0.0:*LISTEN22597/nrpetcp00:::22:::*LISTEN1031/sshdtcp00::1:25:::*LISTEN1108/mastertcp00:::5666:::*LISTEN22597/nrpe3.在监控端安装NRPE1,安装NRPE[root@Nagios~]#tarxfnrpe-2.15.tar.gz[root@Nagios~]#cdnrpe-2.15[root@Nagiosnrpe-2.15]#./configure>--with-nrpe-user=nagios\>--with-nrpe-group=nagios\>--with-nagios-user=nagios\>--with-nagios-group=nagios\>--enable-command-args\>--enable-ssl[root@Nagiosnrpe-2.15]#makeall[root@Nagiosnrpe-2.15]#makeinstall-plugin#安装完成后会在Nagios安装目录libexec下生成check_nrpe插件[root@Nagios~]#cd/usr/local/nagios/libexec/[root@Nagioslibexec]#ll-dcheck_nrpe-rwxrwxr-x.1nagiosnagios767699月2808:07check_nrpe2、check_nrpe[root@Nagioslibexec]#./check_nrpe-hNRPEPluginforNagios的用法:09-06-2013许可证:GPLv2withexemptions(-lfororeinfo)SSL/TLSAavailable:AnonymousDHMode,OpenSSL0.9.6orhigherrequiredUsage:check_nrpe-H[-b][-4][-6][-n][-u][-p<端口>][-t<超时>][-c<命令>][-a]选项:-n=DonouseSSL-u=MakesockettimeoutsreturnanUNKNOWNstateinsteadofCRITICAL=TheaddressofthehostrunningtheNRPEdaemon=bindtolocaladdress-4=useripv4only-6=useripv6only[port]=Theportonwhichthedaemonisrunning(default=5666)[timeout]=Numberofsecondsbeforeconnectiontimesout(default=10)[command]=Thenameofthecommandthattheremotedaemonshouldrun[arglist]=Optionalargumentsthatshouldbepassedtothecommand.Multipleargumentsshouldbeseparatedbyaspace.如果提供,这必须是命令行上提供的最后一个选项。注意:此插件要求您在远程主机上运行NRPE守护程序。您还必须配置守护程序以将特定插件命令与您在此处指定的[命令]选项相关联。收到[命令]参数后,NRPE守护程序将运行适当的插件命令并将插件输出和返回代码返回给*此*插件。这允许您在远程主机上执行插件并“伪造”结果,使Nagios认为插件正在本地运行。要通过NRPE监控远程Linux主机,需要使用chech_nrpe插件。语法如下:check_nrpe-H[-n][-u][-p][-t][-c][-a][root@Nagioslibexec]#./check_nrpe-H192.168.0.81NRPEv2.153,定义命令[root@Nagios~]#cd/usr/local/nagios/etc/objects/[root@Nagiosobjects]#vimcommands.cfg#在最后一行添加definecommand{command_namecheck_nrpecommand_line$USER1$/check_nrpe-H"$HOSTADDRESS$"-c"$ARG1$"}#p#4.定义服务[root@Nagiosobjects]#cpwindows.cfglinhost.cfg[root@Nagiosobjects]#grep-v'^#'linhost.cfg|sed'/^$/d'definehost{uselinux-serverhost_namelinhostaliasMyLinuxServeraddress192.168.0.81}defineservice{usegeneric-servicehost_namelinhostservice_descriptionCHECKUSERcheck_commandcheck_nrpe!check_users}defineservice{usegeneric-servicehost_namelinhostservice_descriptionLoadcheck_commandcheck_nrpe!check_load}defineservice{我们egeneric-servicehost_namelinhostservice_descriptionSDA1check_commandcheck_nrpe!check_hda1}defineservice{usegeneric-servicehost_namelinhostservice_descriptionZombiecheck_commandcheck_nrpe!check_zombie_procs}defineservice{usegeneric-servicehost_namelinhostservice_descriptionTotalprocscheck_commandcheck_nrpe!check_total_procs}这里重点说下,Nagios服务端定义服务的命令完全是根据被监控端NRPEThemonitoringcommandbuiltin,asshowninthefigurebelow5.Startthedefinedcommandsandservices[root@Nagios~]#vim/usr/local/nagios/etc/nagios.cfg#Addalinecfg_file=/usr/local/nagios/etc/objects/linhost.cfg6,configurationfilesyntaxcheck[root@Nagios~]#servicenagiosconfigtestNagiosCore4.0.7Copyright(c)2009-presentNagiosCoreDevelopmentTeamandCommunityContributorsCopyright(c)1999-2009EthanGalstadLastModified:06-03-2014License::GPLWebsitehttp://www.nagios.orgReadingconfigurationdata...Readmainconfigfileokay...Readobjectconfigfilesokay...Runningpre-flightcheckonconfigurationdata...Checkingobjects...Checked20services.Checked3hosts.Checked2hostgroups.Checked0服务组。检查了1个联系人。已检查1个联系人组。检查了26个命令。检查了5个时间段。检查0主机升级。已检查0项服务升级。Checkingforcircularpaths...Checked3hostsChecked0servicedependenciesChecked0hostdependenciesChecked5timeperiodsCheckingglobaleventhandlers...Checkingobsessivecompulsiveprocessorcommands...Checkingmiscsettings...TotalWarnings:0TotalErrors:0Thingslookokay-Noseriousproblemsweredetectedduringthepre-flightcheckObjectprecachefilecreated:/usr/local/nagios/var/objects.重新启动themiscsettings...service[root@Nagios~]#servicenagiosrestart正在运行配置检查...停止nagios:完成。启动nagios:完成。8.打开Nagiosweb监控页面1)首先点击【Hosts】查看监控主机状态是否UP2)接下来点击【Services】查看各个监控服务状态是否OK注意:新添加的主机linhost正在被监控;状态为CRITICAL,表示不存在该文件或目录以下是解决方案。监控Linhost主机时出现CRITICAL警告,找到解决方法###被监控端修改NRPE配置文件,重启NRPE服务[root@ClientNrpeetc]#vimnrpe.cfgcommand[check_sda1]=/usr/local/nagios/libexec/check_disk-w20%-c10%-p/dev/sda1[root@ClientNrpeetc]#servicenrpedrestart###监控端修改linhost.cfg配置文件,重启nagios和httpd服务[root@Nagiosobjects]#vimlinhost。cfg#注意:原来这里是hda1,现在改为sda1defineservice{usegeneric-servicehost_namelinhostservice_descriptionSDA1check_commandcheck_nrpe!check_sda1}[root@Nagios~]#servicenagiosrestartRunningconfigurationcheck...Stoppingnagios:done.启动nagios:完成。[root@Nagios~]#servicehttpdrestartStoppinghttpd:[OK]Startinghttpd:[OK]再次点击[services]刷新页面,如下图:博文来自:http://467754239.blog.51cto.com/4878013/1558897