monicazhang 发表于 2015-10-30 21:40:57

Nagios的安装与配置(3)

本帖最后由monicazhang于2015-10-3021:40编辑

20151030淡然续上



definehost{usehost-pnphost_namenagios-test1aliasnagiostest1address172.26.188.202contact_groupsadminscheck_commandcheck-host-alivemax_check_attempts5notification_interval10notification_period24x7notification_optionsd,u,r}修改services.cfgservices.cfg定义被监控机的监控项,配置如下:defineservice{usesrv-pnphost_namenagios-test1service_descriptioncheck_tcp80check_period24x7max_check_attempts4normal_check_interval3retry_check_interval2contact_groupsadminsnotification_interval10notification_period24x7notification_optionsw,u,c,revent_handler_enabled1event_handlerrestart-httpdcheck_commandcheck_tcp!80nagios安装}被监控机配置被监控机安装plugin#groupaddnagios#useradd-gnagios-d/usr/local/nagiosnagios#tar–zxvfnagios-plugins-1.4.16.tar.gz#cdnagios-plugins-1.4.16#./configure--with-nagios-user=nagios--with-nagios-group=nagios#make#makeinstall被监控机安装nrpe#tar-zxvfnrpe-2.13.tar.gz#cdnrpe-2.13#./configure--prefix=/usr/local/nagios#makeall#makeinstall-plugin#makeinstall-daemon#makeinstall-daemon-config#chown-Rnagios:nagios/usr/local/nagios
将nrpe加入xinetd启动#vi/etc/xinetd.d/nrpeservicenrpe{flags=REUSEsocket_type=streamport=5666wait=nouser=nagiosgroup=nagiosserver=/usr/local/nagios/bin/nrpeserver_args=-c/usr/local/nagios/etc/nrpe.cfg--inetd开源监控软件log_on_failure+=USERIDdisable=noonly_from=127.0.0.1172.26.188.201}
在/etc/services对应位置加入如下行:nrpe5666/tcp#NRPE
启动nrpeservicexinetdrestart配置被监控机主要修改了以下这几个文件/usr/local/nagios/etc/nrpe.cfg/etc/hosts.allow/etc/rc.local1)配置NRPE首先查看/usr/local/nagios/libexec文件夹下面是否有监控脚本命令,这些脚本既为nrpe.cfg中定义命令需要调用的脚本vi/usr/local/nagios/etc/nrpe.cfgallowed_hosts=172.26.188.201#监控机IPserver_address=172.26.188.202#本机IP在nrpe.cfg中添加自定义的命令以下条命令为例Command=/usr/local/nagios/libexec/check_disk–w20%-c10%-p/dev/sdb2监测硬盘使用量,check_sdb2为自定义名,check_disk为调用的脚本,-w是警告,剩余20%容量时,-c,10%为严重值,-p指定要监控的硬盘2)修改/etc/hosts.allow增加监控机ipecho'nrpe:监控机ip'>>/etc/hosts.allow3)启动NRPE守护进程:/usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe.cfg–d4)设置开机自动启动NRPE。echo"/usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe.cfg-d">>/etc/rc.local查看相应的端口:netstat-an|grep5666你会发现5666端口已开启。
要点:在添加被监控机时,首先要确定被监控机nrpe.cfg文件里有添加此命令行,前提是在被监控机的/usr/local/nagios/libexec文件夹下面有相应的监控命令的脚本;其次在监控机的command.cfg文件配置里也要有相应的配置,既依照文件里面nrpe的定义文件来添加相应的监控命令;最后是监控机的services.cfg文件中定义具体的监控项。nagios事件自动处理nagios通过SSH执行event_handernagios使用eventhandlers来在任何人收到通知之前由Nagios做一些前期故障修复。nagios配置事件处理命令可以用shell或是perl脚本,脚本中应该处理以下宏:
对服务的:$SERVICESTATE$、$SERVICESTATETYPE$和$SERVICEATTEMPT$;
对主机的:$HOSTSTATE$、$HOSTSTATETYPE$和$HOSTATTEMPT$。
脚本须检测这些作为命令行参数传入的值,并采取必要动作来处理这些值。下面以nagios监控apache服务,当apache停止时自动启动apache。详细配置步骤如下:1)配置在Nagios监控机上无密码登录远程被监控机Ø生成ssh密钥文件#su-nagios$ssh-keygen-trsa#下面一直回车,不要设置密码Generatingpublic/privatersakeypair.Enterfileinwhichtosavethekey(/home/nagios/.ssh/id_rsa):Enterpassphrase(emptyfornopassphrase):Entersamepassphraseagain:Youridentificationhasbeensavedin/home/nagios/.ssh/id_rsa.Yourpublickeyhasbeensavedin/home/nagios/.ssh/id_rsa.pub.Thekeyfingerprintis:d2:82:61:12:53:f9:53:75:77:8d:32:c0:ca:c8:20:60nagios@nagios.itech.comØ将生成的密钥拷贝到要远程被监控主机上$scp.ssh/id_rsa.pubnagios-test1:/usr/local/nagios/Ø在要远程登录的被监控机器上配置公钥$sshnagios@nagios-test1$nagios@nagios-test1'spassword:$catid_rsa.pub>>.ssh/authorized_keys$chmod600.ssh/authorized_keys监控软件$exitØ测试无密码登录$sshnagios@nagios-test12)在远程被监控机器上配置sudo使nagios用户可以以root身份运行/usr/local/nagios/libexec/eventhandlers/restart-httpd脚本#visudo添加如下行:nagiosALL=(root)NOPASSWD:/usr/local/nagios/libexec/eventhandlers/restart-httpd注释如下行:#Defaultsrequiretty3)在远程机器上编写apache重启脚本
vi/usr/local/nagios/libexec/eventhandlers/restart-httpd内容如下:#!/bin/sh##EventhandlerscriptforrestartingtheApacheserverontheremotemachine##Note:ThisscriptwillonlyrestarttheApacheserveriftheserviceis#retried2times(ina"soft"state)orifthewebservicesomehow#managestofallintoa"hard"errorstate.###WhatstateistheApacheservicein?nagios实施case"$1"inOK);;WARNING);;UNKNOWN);;CRITICAL)#Isthisa"soft"ora"hard"state?case"$2"inSOFT)#Whatcheckattemptareweon?Wedon'twanttorestarttheApacheserveronthefirst#check,becauseitmayjustbeafluke!nagios培训case"$3"in2)




待续:http://www.ITILxf.com/thread-53045-1-1.html
本帖关键字:Nagios
页: [1]
查看完整版本: Nagios的安装与配置(3)