×

微信扫一扫,快捷登录!

Nagios的安装与配置(3)

标签: 暂无标签
本帖最后由monicazhang于2015-10-3021:40编辑

20151030淡然
续上




definehost{
usehost-pnp
host_namenagios-test1
aliasnagiostest1
address172.26.188.202
contact_groupsadmins
check_commandcheck-host-alive
max_check_attempts5
notification_interval10
notification_period24x7
notification_optionsd,u,r
}
修改services.cfg
services.cfg定义被监控机的监控项,配置如下:
defineservice{
usesrv-pnp
host_namenagios-test1
service_descriptioncheck_tcp80
check_period24x7
max_check_attempts4
normal_check_interval3
retry_check_interval2
contact_groupsadmins
notification_interval10
notification_period24x7
notification_optionsw,u,c,r
event_handler_enabled1
event_handlerrestart-httpd
check_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/nrpe
servicenrpe
{
flags=REUSE
socket_type=stream
port=5666
wait=no
user=nagios
group=nagios
server=/usr/local/nagios/bin/nrpe
server_args=-c/usr/local/nagios/etc/nrpe.cfg--inetd开源监控软件
log_on_failure+=USERID
disable=no
only_from=127.0.0.1172.26.188.201
}

在/etc/services对应位置加入如下行:
nrpe5666/tcp#NRPE

启动nrpe
servicexinetdrestart
配置
被监控机主要修改了以下这几个文件
/usr/local/nagios/etc/nrpe.cfg
/etc/hosts.allow
/etc/rc.local
1)配置NRPE
首先查看/usr/local/nagios/libexec文件夹下面是否有监控脚本命令,这些脚本既为nrpe.cfg中定义命令需要调用的脚本
vi/usr/local/nagios/etc/nrpe.cfg
allowed_hosts=172.26.188.201#监控机IP
server_address=172.26.188.202#本机IP
在nrpe.cfg中添加自定义的命令
以下条命令为例
Command[check_sdb2]=/usr/local/nagios/libexec/check_disk–w20%-c10%-p/dev/sdb2
监测硬盘使用量,check_sdb2为自定义名,check_disk为调用的脚本,-w是警告,剩余20%容量时,-c,10%为严重值,-p指定要监控的硬盘
2)修改/etc/hosts.allow增加监控机ip
echo'nrpe:监控机ip'>>/etc/hosts.allow
3)启动NRPE守护进程:
/usr/local/nagios/bin/nrpe-c/usr/local/nagios/etc/nrpe.cfg–d
4)设置开机自动启动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_hander
nagios使用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-test1
2)在远程被监控机器上配置sudo
使nagios用户可以以root身份运行/usr/local/nagios/libexec/eventhandlers/restart-httpd脚本
#visudo
添加如下行:
nagiosALL=(root)NOPASSWD:/usr/local/nagios/libexec/eventhandlers/restart-httpd
注释如下行:
#Defaultsrequiretty
3)在远程机器上编写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"in
OK)
;;
WARNING)
;;
UNKNOWN)
;;
CRITICAL)
#Isthisa"soft"ora"hard"state?
case"$2"in
SOFT)
#Whatcheckattemptareweon?Wedon'twanttorestarttheApacheserveronthefirst
#check,becauseitmayjustbeafluke!nagios培训
case"$3"in
2)





待续:http://www.ITILxf.com/thread-53045-1-1.html
本帖关键字:Nagios




上一篇:Nagios的安装与配置(2)
下一篇:Nagios的安装与配置(4)
monicazhang

写了 2297 篇文章,拥有财富 12859,被 21 人关注

您需要登录后才可以回帖 登录 | 立即注册
B Color Link Quote Code Smilies

成为第一个吐槽的人

Powered by IT 运维管理
返回顶部