LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Perl Script works from command line, but not when evoked by Nagios (https://www.linuxquestions.org/questions/linux-general-1/perl-script-works-from-command-line-but-not-when-evoked-by-nagios-643797/)

gfem 05-21-2008 10:03 AM

Perl Script works from command line, but not when evoked by Nagios
 
I am trying to evoke a perl mail script within Nagios. When it is executed with Nagios it throws errors, if I run the same command line within a terminal it works great. Does anyone have any helpful suggestions ? Below is the error log that I saved for debugging.

Wed May 21 07:55:05 PDT 2008 /usr/local/nagios/libexec/send_mail.pl -n 'SERVICE RECOVERY' -h 'SonicWall' -s 'OK' -a 'xxx.xxx.xxx.xxx' -i 'Bandwidth Usage Wan - Traffic OK - Avg. In = 16.1 KB/s, Avg. Out = 5.7 KB/s - check_local_mrtgtraf!/var/www/mrtg/sonicwall/xx.xx.xx.xx_1.log!AVG!1000000,1000000!5000000,5000000!10' -d 'Wed May 21 07:55:05 PDT 2008' -e email@email.com

Use of uninitialized value in concatenation (.) or string at /usr/local/nagios/libexec/send_mail.pl line 86. Use of uninitialized value in concatenation (.) or string at /usr/local/nagios/libexec/send_mail.pl line 86. Use of uninitialized value in concatenation (.) or string at /usr/local/nagios/libexec/send_mail.pl line 86. Use of uninitialized value in concatenation (.) or string at /usr/local/nagios/libexec/send_mail.pl line 117. Use of uninitialized value in string at /usr/local/nagios/libexec/send_mail.pl line 118. Use of uninitialized value in concatenation (.) or string at /usr/local/nagios/libexec/send_mail.pl line 122. Use of uninitialized value in concatenation (.) or string at /usr/local/nagios/libexec/send_mail.pl line 122. Use of uninitialized value in concatenation (.) or string at /usr/local/nagios/libexec/send_mail.pl line 122. Use of uninitialized value in concatenation (.) or string at /usr/local/nagios/libexec/send_mail.pl line 122. Net::SMTP>>> uninitialized value in concatenation (.) or string at /usr/local/nagios/libexec/send_mail.pl line 135. option -h not defined!
Exit code: 0


Thanks in advance.

eggixyz 05-21-2008 01:45 PM

Hey There,

Can you post the script that you're running, or (if it's a situation where you can't) can you post the lines that the error messages denote. If possible, also the lines above and below the line that's noted in the error messages. Sometimes the errors aren't on the line Perl says they are ;)

Thanks,

Mike

gfem 05-21-2008 03:42 PM

The borrowed Script....

#!/usr/bin/perl -w
#
# Wriiten by Rob Moss, 2005-10-09
# coding@mossko.com
#
# A replacement for Nagios's basic mail system which relies on /bin/mailx and local sendmail config
#
# To use this, just replace the entries in commands.cfg and update your hosts and services from the default "notify-by-email" to use the specific host-notify-by-email or service-notify-by-email
#
# I put this in the plugins directory /usr/local/nagios/libexec but you can put it anywhere. Just update the path

# commands.cfg
#define command{
# command_name host-notify-by-email
# command_line $USER1$/send_mail.pl -n "HOST $NOTIFICATIONTYPE$" -h "$HOSTNAME$" -s "$HOSTSTATE$" -a "$HOSTADDRESS$" -i "$HOSTOUTPUT$" -d "$LONGDATETIME$" -e "$CONTACTEMAIL$"
#}
#
#define command{
# command_name service-notify-by-email
# command_line $USER1$/send_mail.pl -n "SERVICE $NOTIFICATIONTYPE$" -h "$HOSTNAME$" -s "$SERVICESTATE$" -a "$HOSTADDRESS$" -i "$SERVICEDESC$ - $SERVICEOUTPUT$ - $SERVICECHECKCOMMAND$" -d "$LONGDATETIME$" -e "$CONTACTEMAIL$"
#}

# contacts.cfg
#define contact{
# contact_name contact
# host_notification_commands host-notify-by-email
# service_notification_commands service-notify-by-email
# email email@address.com
#}


use strict;
use Net::SMTP;
use Getopt::Std;

my $mailhost = 'xx.xx.xx.xx';
my $maildomain = 'architect.somesite.com';
my $mailfrom = 'nagios@somesite.com';
my $mailto = 'support@somesite.com';
my $timeout = 30;
my $mailsubject = ''; # Leave blank
my $mailbody = ''; # Leave blank
my $logfile = '/tmp/mail.log'; # Put somewhere better
my $debug = 1; # To enable SMTP session debugging to logfile


################################################################
# Email for nagios
#
# Subject: "Host $HOSTSTATE$ alert for $HOSTNAME$!" $CONTACTEMAIL$
#
# "***** Nagios *****
# Notification Type: $NOTIFICATIONTYPE$
# Host: $HOSTNAME$
# State: $HOSTSTATE$
# Address: $HOSTADDRESS$
# Info: $HOSTOUTPUT$
# Date/Time: $LONGDATETIME$
#
#################################################################
#
# Commandline options
#
# -n NOTIFICATIONTYPE
# -h HOSTNAME
# -s HOSTSTATE
# -a HOSTADDRESS
# -i HOSTOUTPUT
# -d LONGDATETIME
# -e CONTACTEMAIL
#
#################################################################


if (not open(LOG,">>$logfile") ) {
$mailsubject = "Nagios Monitoring Alert: Can't open $logfile for append: $!";
print STDERR "Nagios Monitoring Alert: Can't open $logfile for append: $!";
sendmail();
exit 1;
}


our ($opt_n, $opt_h, $opt_s , $opt_a , $opt_i , $opt_d , $opt_e);
# Get the cmdline options
getopt('nhsaide');
print LOG localtime() . " sending mail to $opt_e with host $opt_h state $opt_s\n";

if (not defined $opt_n or $opt_n eq "") {
print "option -n not defined!\n";
$opt_n = "UNDEFINED";
}
elsif (not defined $opt_h or $opt_h eq "") {
print "option -h not defined!\n";
$opt_h = "UNDEFINED";
}
elsif (not defined $opt_s or $opt_s eq "") {
print "option -s not defined!\n";
$opt_s = "UNDEFINED";
}
elsif (not defined $opt_a or $opt_a eq "") {
print "option -a not defined!\n";
$opt_a = "UNDEFINED";
}
elsif (not defined $opt_i or $opt_i eq "") {
print "option -i not defined!\n";
$opt_i = "UNDEFINED";
}
elsif (not defined $opt_d or $opt_d eq "") {
print "option -d not defined!\n";
$opt_d = "UNDEFINED";
}
elsif (not defined $opt_e or $opt_e eq "") {
die "CRITICAL: option -e not defined!. Host $opt_h, Notification: $opt_n, State: $opt_s, Info: $opt_i\n";
}

# You can change the subject here
$mailsubject = "Nagios Monitoring Alert: $opt_h is $opt_s";
$mailto = "$opt_e";



$mailbody = <<_END_;
***** Nagios *****

Notification Type: $opt_n
Host: $opt_h
State: $opt_s
Address: $opt_a
Info: $opt_i
Date/Time: $opt_d
_END_


sendmail();
print LOG localtime() . " sent mail to $opt_e successfully\n";

exit 0;


#########################################################
sub sendmail {
my $smtp = Net::SMTP->new(
$mailhost,
Hello => $maildomain,
Timeout => $timeout,
Debug => $debug,
);

$smtp->mail($mailfrom);
$smtp->to($mailto);

$smtp->data();
$smtp->datasend("To: $mailto\n");
$smtp->datasend("From: $mailfrom\n");
$smtp->datasend("Subject: $mailsubject\n");
$smtp->datasend("\n");
$smtp->datasend("$mailbody\n");
$smtp->dataend();

$smtp->quit;
}

sundialsvcs 05-21-2008 03:58 PM

Usually, in a situation like this you find that the library search path (e.g. PERL5LIB) is not initialized in the other environment in the same way as it is when using the command-line. This will come down to some configuration-parameter somewhere.

You can also use use lib pathname to explicitly specify the correct search-path, which will then be identically specified in all cases.

eggixyz 05-21-2008 07:57 PM

Hey There,

That's most likely correct. The only other thing I noticed is that all the error lines are referencing the options from getopts ($opt_e, etc) so that might be a place to look also.

Quote:

main::(./nagios:117): $mailsubject = "Nagios Monitoring Alert: $opt_h is $opt_s";
DB<1>
Use of uninitialized value in concatenation (.) or string at ./nagios line 117.
at ./nagios line 117
Use of uninitialized value in concatenation (.) or string at ./nagios line 117.
at ./nagios line 117
main::(./nagios:118): $mailto = "$opt_e";
For instance, above, on line 118 $mailto is initialized so the problem looks like -e is not set (or the flag wasn't set on the command line)

Quote:

my $mailto = 'support@somesite.com';
I also saw that it's supposed to complain if the -e flag isn't set. Just possible that when executed through nagios the option flags aren't being passed.

By any chance to you ever get this error (That would definitely point to the lib path, as mentioned in sundialvcs' response.

Quote:

Undefined subroutine &main::sendmail called at ./nagios line 134.
since that should be coming from Net::SMTP, I believe.

Hopefully some of this post helped out :)

Best wishes,

Mike

gfem 05-22-2008 11:51 AM

Quote:

Originally Posted by sundialsvcs (Post 3160716)
Usually, in a situation like this you find that the library search path (e.g. PERL5LIB) is not initialized in the other environment in the same way as it is when using the command-line. This will come down to some configuration-parameter somewhere.

Here is my path....
[nagios@Architect nagios]$ perl -e 'print "\@INC is @INC\n";' @INC is /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl/5.8.7 /usr/lib/perl5/site_perl/5.8.6 /usr/lib/perl5/site_perl/5.8.5 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.7/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.6/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.5/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl/5.8.7 /usr/lib/perl5/vendor_perl/5.8.6 /usr/lib/perl5/vendor_perl/5.8.5 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .

Quote:

Originally Posted by sundialsvcs (Post 3160716)
You can also use use lib pathname to explicitly specify the correct search-path, which will then be identically specified in all cases.

When you say specify search path how do I do that?

-Gregg

eggixyz 05-22-2008 01:39 PM

Hey There,

Just do

Quote:

use lib '/wherever/you/have/your/libdir;
Also, did you check to see if nagios was picking up the -e flag?

Best wishes,

Mike

chrism01 05-22-2008 08:24 PM

I notice it finds 4 different versions of Perl, 5.8. 5,6,7,8
That's not really a good idea.

gfem 05-23-2008 03:33 PM

yes nagios passes the -e option it appears in the log file noted in the first post. Thnaks for the directions with the use command, unfortunately it did not work. I think I will try the rpm instead of source and see if that makes a difference.

-Gregg

eggixyz 05-23-2008 05:11 PM

Cool,

Also, check out Chrism01's comment. I didn't notice it at first glance either, but there shouldn't be multiple versions of perl in your @INC array, unless you're sure that all those versions, except the one you want to use, are uninstalled or have been moved from their original locations.

Best of luck :)

, Mike

gfem 06-05-2008 03:59 PM

After a while of banging my head up against a wall I have figured out that if I set the command to send the -e option only it works through nagios, with any other option it fails. Does anyone have any experience with nagios? I will keep trying, but any help is appriciated.

-Gregg


All times are GMT -5. The time now is 01:40 AM.