monicazhang 发表于 2015-10-30 21:39:27

Nagios的安装与配置(5)

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

20151030淡然续上



definecommand{command_namenotify-service-by-emailnagios安装command_line/usr/bin/printf"%b""*****Nagios*****NotificationType:$NOTIFICATIONTYPE$Service:$SERVICEDESC$Host:$HOSTALIAS$Address:$HOSTADDRESS$State:$SERVICESTATE$Date/Time:$LONGDATETIME$AdditionalInfo:$SERVICEOUTPUT$"|/bin/mail-s"**$NOTIFICATIONTYPE$ServiceAlert:$HOSTALIAS$/$SERVICEDESC$is$SERVICESTATE$**"$CONTACTEMAIL$}Ø配置联系人#vi/usr/local/nagios/etc/objects/contacts.cfgdefinecontact{contact_nametest1aliassystemadministratorservice_notification_period24x7host_notification_period24x7service_notification_optionsw,u,c,rhost_notification_optionsd,u,rservice_notification_commandsnotify-service-by-emailhost_notification_commandsnotify-host-by-emailemailtest1@}definecontact{contact_nametest2aliastest2service_notification_period24x7host_notification_period24x7service_notification_optionsw,u,c,rhost_notification_optionsd,u,rservice_notification_commandsnotify-service-by-emailhost_notification_commandsnotify-host-by-emailemailtest2@}Ø配置联系人组#vi/usr/local/nagios/etc/objects/contactgroups.cfgdefinecontactgroup{contactgroup_namenetworkaliasnetworkadministratorgroupmemberstest1}definecontactgroup{contactgroup_namesystemaliassystemadministratorgroupmemberstest2}Ø监控主机配置#vi/usr/local/nagios/etc/objects/hosts.cfg开源监控软件definehost{usehost-pnphost_namenagios-test1aliasnagiostest1address172.26.188.202contact_groupsnetwork,systemcheck_commandcheck-host-alivemax_check_attempts5notification_interval10notification_period24x7notification_optionsd,u,r}Ø监控服务配置#vi/usr/local/nagios/etc/objects/services.cfgdefineservice{usesrv-pnphost_namenagios-test1service_descriptioncheck_tcp80check_period24x7max_check_attempts4normal_check_interval3retry_check_interval2contact_groupssystem,networknotification_interval10notification_period24x7notification_optionsw,u,c,revent_handler_enabled1event_handlerrestart-httpdcheck_commandcheck_tcp!80}

短信报警通知短信报警可以通过139邮箱或qq邮箱绑定手机实现,设置相对比较简单,这里不做介绍。本文采用飞信机器人实现nagios短信报警。安装飞信机器人下载飞信机器人及机器人支持库,下载地址:该软件为绿色版,解压后即可使用。发送短信命令:fetion--mobile=13711123456--pwd=mypwd--to=137xxxxxxxx--msg-gb=测试提示:
1.使用复杂的密码(数字+字母+符号),将不会弹出图形验证码(我测试时是这种表现)。
2.第一次使用机器人时,即使是复杂密码,也可能会弹出图形验证码,以后将不再提示。
3.如果您是使用其它程序调用本程序,请在运行时,指定--exit-on-verifycode=1,否则,程序将无限期等待。当您以该参数运行机器人时,当弹出图形验证码时,程序将以退出码29结束(在linux中,通过$?获取,在windows中,通过%ERRORLEVEL%获取)
4.请确保目录权限可写。Nagios短信报警配置Ø编写发送脚本#visendsms.sh#!/bin/bashfetionDir=/usr/local/fetioncd$fetionDirDIR=`pwd`user=138********pwd=yourpasswordecho"================">>msg.txtnagios配置echo"sms$1$2">>$DIR/msg.txtecho"quit">>$DIR/msg.txtecho"================">>msg.txt$fetionDir/fetion--exit-on-verifycode=1--mobile=$user--pwd=$pwd--to=$1--msg-utf8="$2"Ø修改飞信目录权限(否则运行nagios的用户无法调用fetion发送短信)#chown-Rnagios.nagios/usr/local/fetion#chmod–R755/usr/local/fetion/ØNagios配置l定义command,增加如下内容。#vi/usr/local/nagios/etc/objects/commands.cfg##################################################################################notify-host-by-sendmsg/notify-service-by-sendmsg//定义发送飞信报警的命令#################################################################################definecommand{command_namenotify-host-by-fetioncommand_line/usr/local/fetion/sendsms.sh$CONTACTPAGER$"Host$HOSTSTATE$alertfor$HOSTNAME$($HOSTADDRESS$)on$TIME$."}definecommand{command_namenotify-service-by-fetioncommand_line/usr/local/fetion/sendsms.sh$CONTACTPAGER$"$TIME$:$SERVICEDESC$($HOSTADDRESS$)is$SERVICESTATE$."}l定义contact,增加短信通知,如要修改如下内容#vi/usr/local/nagios/etc/objects/contacts.cfgdefinecontact{contact_nametest1aliassystemadministrator监控软件service_notification_period24x7host_notification_period24x7service_notification_optionsw,u,c,rhost_notification_optionsd,u,rservice_notification_commandsnotify-service-by-email,notify-service-by-fetionhost_notification_commandsnotify-host-by-email,notify-host-by-fetionemailtest1@pager138********}故障排除1、nagios无法发送短信报警。现象:(1)以web方式查看nagios监控状态,显示故障,却收不到报警短信。(2)系统日志出现“Warning:Attemptingtoexecutethecommand"/usr/local/fetion/fetion--mobile=138********--pwd=little228--to=138********--msg-utf8="CHECK_NRPE:Sockettimeoutafter10seconds.(20:51:12)""resultedinareturncodeof127.Makesurethescriptorbinaryyouaretryingtoexecuteactuallyexists...”。测试fetion:用手工方式执行飞信发短信没问题,再切换成用户nagios,也没问题。处理过程:(1)登陆该服务器,查看系统日志和nagios日志,如想象(2)。(2)查看飞信所在的目录,检查权限设置。(3)先修改一下他的/etc/sudoers文件,新增一行“nagiosALL=NOPASSWD:/usr/local/feiton/feiton”保存。nagios实施(4)切换到nagios用户:su-nagios(5)执行sudo/usr/local/feiton/feiton,报无lib*.so文件。(6)这些文件在/usr/local/feiton里呆着呢,先看看/lib里有没有这些。(7)切换回root,查一下/lib目录,果然没有这些lib*.so。(8)把这些个lib*.so拷贝到/lib目录。(9)再切换到nagios用户执行sudo/usr/local/feiton/feiton哈哈,没报错了。(10)停止一个监控机上的服务,片刻,就发短信了。对于监控短信报警,还是建议使用短信接口,不行该死的飞信,不能发其他运营商的手机短信。邮件和短信分时报警如我们需要每天上午让nagios通过邮件来报警,每天下午通过短信报警。实现方法如下:定义2个timeperiod,如am、pm,定义contact时分邮件联系人和短信联系人,分别应用am、pm,最后在host、service中通知到相应的联系人。配置示例如下。Ø定义timeperiod,增加如下内容nagios培训definetimeperiod{timeperiod_nameamaliasamsunday00:00-12:00



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