Script executed by Nagios notification not send mail

nagios

i've running a new nagios (server 3.5.1) system. all checks works fine

[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.

i can't use mail because, the mailserver is a exchange server. so i changed the command to the following:

# '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 is a ruby script which takes the arguments and connects to the server. if i call this directly, with parameters so i get an email.

my contact looks like the following:

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                           idontcare@examnple.org   ; 
  }

define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 nagiosadmin
        }

it looks like the command is not getting called by nagios.
script has rights 755. other commands like "touching a file" will also not work.

output from debug mode:

[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"

update:

if i replace the script with the original lines, i will get mails in /var/mail.

the user nagios can run the script. i logged in with user nagios and execute the command manually.

update2 (additional information from the comments)

the ruby script is using #!/usr/bin/env ruby and works fine by calling it from the commandline. if i use the default /usr/bin/mail than i got a mail to the local user account on the server. the ruby script is connecting to the server, and send the mail with different mail credentials. this works finde with the user nagios

somebody had an idea?

update 3: (ruby script to send the mail)

#!/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 = 'existingmail@existingdomain.de'
toAddress = 'me@example.com'
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)

update 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

Best Answer

so, first thanks to all who helped me.

it wasn't a nagios problem.

summary:

  1. nagios check for each service worked fine the mailscript sends the
  2. mail via console (with or without parameter)
  3. nagios fired the event and executed the mailscript fine

that was really confusing. in my desperation i added a global exception handler in the mailscript, which writes the result of the exception into a file. and there i found an entry for an error which throwed only when the script was executed from nagios.

the script now didn't find the required files, because the LOAD_PATH was not correct.

solution:

so before the requirements:

require 'ntlm/smtp'

i added the path to the files manually by:

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

now the mailscript works fine by executing from nagios.

Related Topic