Nagios 通知执行的脚本不会发送邮件

Nagios 通知执行的脚本不会发送邮件

我正在运行一个新的 nagios(服务器 3.5.1)系统。所有检查都正常

[1461932408] SERVICE NOTIFICATION: nagiosadmin;appserver;Swapfile;CRITICAL;notify-service-by-email;CHECK_NRPE: Socket timeout after 10 seconds.
[1461932418] SERVICE NOTIFICATION: nagiosadmin;adminserver;Disk;CRITICAL;notify-service-by-email;CHECK_NRPE: Socket timeout after 10 seconds.

我无法使用邮件,因为邮件服务器是 Exchange 服务器。所以我将命令更改为以下内容:

# 'notify-host-by-email' command definition 
define command { 
command_name notify-host-by-email 
command_line /usr/bin/printf "%b" "Notification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/bin/mailsend.rb "***** Nagios Alert*****"
}

# 'notify-service-by-email' command definition 
define command { 
command_name notify-service-by-email 
command_line /usr/bin/printf "%b" "Notification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/local/bin/mailsend.rb "***** Nagios Alert*****"
}

mailsend.rb是一个 ruby​​ 脚本,它接受参数并连接到服务器。如果我直接调用它,带上参数,我就会收到一封电子邮件。

我的联系方式如下:

define contact{
  contact_name                    nagiosadmin       ; Short name of user
  use               generic-contact     ; Inherit default values from generic-contact template (defined above)
  alias                           Nagios Admin      ; Full name of user
  service_notification_period     24x7
  host_notification_period        24x7
  service_notification_options    w,u,c,r
  host_notification_options       d,r
  service_notification_commands   notify-service-by-email
  host_notification_commands      notify-host-by-email
  email                           [email protected]   ; 
  }

define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 nagiosadmin
        }

看起来该命令没有被 nagios 调用。脚本的权限为 755。其他命令(例如“触摸文件”)也将不起作用。

调试模式的输出:

[1461935539.111737] [032.2] [pid=8391] ** Notifying contact 'nagiosadmin'
[1461935539.111762] [032.2] [pid=8391] Raw notification command: /usr/local/bin/mailsend.rb "***** Nagios Alert*****" "Notification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$"
[1461935539.111799] [032.2] [pid=8391] Processed notification command: /usr/local/bin/mailsend.rb "***** Nagios Alert*****" "Notification Type: PROBLEM\nHost: mysqlserver\nState: UP\nAddress: 10.130.1.22\nInfo: PING OK - Packet loss = 0%, RTA = 0.33 ms\n\nDate/Time: Fri Apr 29 15:12:19 CEST 2016"

更新:

如果我用原始行替换脚本,我将收到邮件/var/mail

用户nagios可以运行该脚本。我以nagios用户登录并手动执行该命令。

update2(来自评论的附加信息)

ruby 脚本正在使用,#!/usr/bin/env ruby并且可以通过从命令行调用它来正常工作。如果我使用默认的 /usr/bin/mail,那么我会收到一封发送到服务器上本地用户帐户的邮件。ruby 脚本正在连接到服务器,并使用不同的邮件凭据发送邮件。这适用于用户 nagios

有人有主意吗?

更新3:(ruby脚本发送邮件)

#!/usr/bin/env ruby
require 'net/smtp'
require 'logger'

require 'rubygems'
require 'net-ldap'
require '/home/user/lib/ntlm/smtp'
logger = Logger.new('/var/log/sendEmail')

server = '10.130.1.3'
port = 25      # or 25 - double check with your provider
username = 'user'
password = '****'

fromAddress = '[email protected]'
toAddress = '[email protected]'
subject = ARGV[0]
message = ARGV[1]

logger.info subject
logger.info message

message_body = <<END_OF_EMAIL
From: Nagios <#{fromAddress}>
To: Logs <#{toAddress}>
Subject: #{subject}

#{message}
END_OF_EMAIL

smtp = Net::SMTP.new(server, port)
smtp.start(server,username,password, :ntlm)
smtp.send_message(message_body, fromAddress, toAddress)

更新 4

nagios@background-server:/usr/local/bin$ whoami
nagios
nagios@background-server:/usr/local/bin$ ./mailsend.rb 
nagios@background-server:/usr/local/bin$
nagios@background-server:/usr/local/bin$ ls -la mailsend.rb 
-rwxr-xr-x 1 nagios nagios 811 May  2 09:05 mailsend.rb

答案1

因此,首先感谢所有帮助过我的人。

这不是 Nagios 的问题。

概括:

  1. nagios 检查每个服务是否工作正常,邮件脚本发送
  2. 通过控制台发送邮件(带或不带参数)
  3. nagios 触发了事件并执行了邮件脚本

这真是令人困惑。无奈之下,我在 mailscript 中添加了一个全局异常处理程序,将异常的结果写入文件。在那里我发现了一个错误条目,只有从 nagios 执行脚本时才会抛出该错误。

脚本现在找不到所需的文件,因为 LOAD_PATH 不正确。

解决方案:

因此在要求之前:

require 'ntlm/smtp'

我通过以下方式手动添加了文件路径:

$:.unshift File.dirname('/home/application/my_application/current/lib/ntlm')

现在,通过从 nagios 执行,mailscript 可以正常工作。

相关内容