q9989 发表于 2019-1-14 14:40:59

nagios报警通知

  1.下载邮件客户端,测试邮件发送是否正常
# wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
# tar -zxvfsendEmail-v1.56.tar.gz
# cp sendEmail /usr/local/bin
# chmod0755 /usr/local/bin/sendEmail
# /usr/local/bin/sendEmail-f zzz@qq.com -t xxxx@xxxxxx.com -s mail.xxxx.com -u "test"-m"error" ;此邮箱发邮件无需添加smtp身份验证,139邮箱需要


-f 表示发送者的邮箱
-t 表示接收者的邮箱
-s 表示SMTP服务器的域名或者ip
-u 表示邮件的主题
-xu 表示SMTP验证的用户名
-xp 表示SMTP验证的密码(注意,这个密码貌似有限制,例如我用d!5neyland就不能被正确识别)
-m 表示邮件的内容
-cc 表示抄送
-bcc 表示暗抄送


--------------------------------------------------------------------------
2. 修改commands.cfg, 替换掉原来发送邮件的两个命令
默认的输出宏$HOSTOUTPUT$和$SERVICEOUTPUT$只会输出第一行信息, 我们使用$LONGHOSTOUTPUT$和$LONGSERVICEOUTPUT$输出完整信息.
注意: 为了能接受到格式化良好, 且中文无乱码的超文本邮件, 可以对sendEmail设置 "-o message-content-type=html -o message-charset=utf8" 选项.
# vi/usr/local/nagios/etc/objects/commands.cfg   

define command{
      command_name    notify-host-by-email
      command_line    /usr/local/bin/sendEmail -f 手机号@139.com -t $CONTACTEMAIL$ -s smtp.139.com:25-u "** Host Alert:$HOSTALIAS$ **" -m "$HOSTNAME$($HOSTALIAS$)的状>态异常,请及时处理!" -o message-content-type=html -o message-charset=utf8 -xu 手机号@139.com -xp 邮箱密码
            }

# 'notify-service-by-email' command definition

define command{
      command_name    notify-service-by-email
      command_line/usr/local/bin/sendEmail -f 手机号@139.com -t $CONTACTEMAIL$ -s smtp.139.com:25-u "** Service Alert:$HOSTALIAS$ **" -m "$HOSTNAME$($HOSTALIAS$)的监控项 $SERVICEDESC$ 状态为$SERVICESTATE$,请及时处理!" -o message-content-type=html -o message-charset=utf8-xu 手机号@139.com-xp 邮箱密码
            }


-------------------------------------------------------------------
3.配置接收nagios告警的邮件地址
# vi /usr/local/nagios/etc/objects/contacts.cfg

define contact{
      contact_name                     联系人名称
      use                              generic-contact
      alias                            联系人别名
      service_notification_period      24x7
      host_notification_period         24x7
      service_notification_options   w,u,c,r,f,s
      host_notification_options      d,u,r,f,s
      service_notification_commands    notify-service-by-email
      host_notification_commands       notify-host-by-email
      email                            手机号@139.com
             }


define contact{
      contact_name                     联系人名称
      use                              generic-contact
      alias                            联系人别名
      service_notification_period      24x7
      host_notification_period         24x7
      service_notification_options   w,u,c,r,f,s
      host_notification_options      d,u,r,f,s
      service_notification_commands    notify-service-by-email
      host_notification_commands       notify-host-by-email
      email                            手机号@139.com
             }


define contactgroup{
      contactgroup_name            system
      alias                        system
      members                        联系人1,联系人2
                   }

-------------------------------------------------------------------------

4.自定义需要监控的主机和相关服务
检测主机是否宕机
例如:
define host{
      use             windows-server; Inherit default values from a template
      host_name       XX服务器      ; The name we're giving to this host
      alias         192.168.12.216; A longer name associated with the host
      address         192.168.12.216; IP address of the host
添加如下语句
      check_command   check-host-alive
      max_check_attempts1;最大重复检测失败次数
      contact_groupssystem   ;通知发送组
      normal_check_interval2   ;正常命令检测频率 2分钟
      notification_interval0   ;警报发送频率,0为只发送一次,10为每隔10分钟发送一次
      }

检测相关服务
例如:
define service{
      use                     generic-service
      host_name               XX服务器
      service_description   XX进程
      check_command         check_nt!PROCSTATE!-d SHOWALL -lOAAdmin.exe
      contact_groups          system;选择需要发送警告的联系组
      notification_interval    0   ;选择重复发送警告的频率,0为发送一次,10为10分钟发送一次!
}
-----------------------------------------------------------------------------
5. 修改完主配置文件后, 都需要重启Nagios服务, 重启前先验证配置文件是否有误
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
# service nagios reload
如果有问题则查询Nagios运行日志, 进行进一步诊断:
#tail -50f /usr/local/nagios/var/nagios.log

------------------------------------------------------------------------------
6.短信通知
本文主要通过139邮箱免费的短信提示实现,只用于移动手机
参考:http://storysky.blog.运维网.com/628458/274416








页: [1]
查看完整版本: nagios报警通知