Nagios的安装与配置(5)
本帖最后由 monicazhang 于 2015-10-30 21:39 编辑20151030淡然续上
define command{ command_name notify-service-by-email nagios安装 command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$ }Ø 配置联系人# vi /usr/local/nagios/etc/objects/contacts.cfgdefine contact { contact_name test1 alias system administrator service_notification_period 24x7 host_notification_period 24x7 service_notification_options w,u,c,r host_notification_options d,u,r service_notification_commandsnotify-service-by-email host_notification_commands notify-host-by-email email test1@ }define contact { contact_name test2 alias test2 service_notification_period 24x7 host_notification_period 24x7 service_notification_options w,u,c,r host_notification_options d,u,r service_notification_commandsnotify-service-by-email host_notification_commands notify-host-by-email email test2@ }Ø 配置联系人组# vi /usr/local/nagios/etc/objects/contactgroups.cfgdefine contactgroup { contactgroup_name network alias network administrator group members test1}define contactgroup { contactgroup_name system alias system administrator group members test2}Ø 监控主机配置# vi /usr/local/nagios/etc/objects/hosts.cfg 开源监控软件define host { use host-pnp host_name nagios-test1 alias nagios test1 address 172.26.188.202 contact_groups network,system check_command check-host-alive max_check_attempts 5 notification_interval 10 notification_period 24x7 notification_options d,u,r }Ø 监控服务配置# vi /usr/local/nagios/etc/objects/services.cfgdefine service { use srv-pnp host_name nagios-test1 service_description check_tcp 80 check_period 24x7 max_check_attempts 4 normal_check_interval 3 retry_check_interval2 contact_groups system,network notification_interval 10 notification_period 24x7 notification_options w,u,c,r event_handler_enabled 1 event_handler restart-httpd check_command check_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短信报警配置 Ø 编写发送脚本#vi sendsms.sh#!/bin/bashfetionDir=/usr/local/fetioncd $fetionDirDIR=`pwd`user=138********pwd=yourpasswordecho "================" >> msg.txt nagios配置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 -R nagios.nagios /usr/local/fetion#chmod –R 755 /usr/local/fetion/Ø Nagios配置l 定义command,增加如下内容。# vi /usr/local/nagios/etc/objects/commands.cfg################################################################################## notify-host-by-sendmsg/notify-service-by-sendmsg //定义发送飞信报警的命令#################################################################################define command{ command_name notify-host-by-fetion command_line /usr/local/fetion/sendsms.sh $CONTACTPAGER$ "Host $HOSTSTATE$ alert for $HOSTNAME$($HOSTADDRESS$) on $TIME$." }define command{ command_name notify-service-by-fetion command_line /usr/local/fetion/sendsms.sh $CONTACTPAGER$ "$TIME$:$SERVICEDESC$($HOSTADDRESS$) is $SERVICESTATE$." }l 定义contact,增加短信通知,如要修改如下内容# vi /usr/local/nagios/etc/objects/contacts.cfgdefine contact { contact_name test1 alias system administrator 监控软件 service_notification_period 24x7 host_notification_period 24x7 service_notification_options w,u,c,r host_notification_options d,u,r service_notification_commandsnotify-service-by-email,notify-service-by-fetion host_notification_commands notify-host-by-email,notify-host-by-fetion email test1@ pager 138******** }故障排除 1、nagios无法发送短信报警。现象:(1)以web方式查看nagios监控状态,显示故障,却收不到报警短信。(2)系统日志出现“Warning: Attempting to execute the command "/usr/local/fetion/fetion --mobile=138******** --pwd=little228 --to=138******** --msg-utf8=" CHECK_NRPE: Socket timeout after 10 seconds. (20:51:12)"" resulted in a return code of 127. Make sure the script or binary you are trying to execute actually exists...”。测试fetion:用手工方式执行飞信发短信没问题,再切换成用户nagios,也没问题。处理过程:(1)登陆该服务器,查看系统日志和nagios日志,如想象(2)。(2)查看飞信所在的目录,检查权限设置。(3)先修改一下他的/etc/sudoers文件,新增一行“nagios ALL=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培训 define timeperiod{ timeperiod_name am alias am sunday 00:00-12:00
待续:http://ITIL-foundation.cn/thread-53047-1-1.html
本帖关键字:Nagios
页:
[1]