LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 05-21-2008, 11:03 AM   #1
gfem
Member
 
Registered: Apr 2003
Distribution: Fedora
Posts: 126

Rep: Reputation: 15
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.
 
Old 05-21-2008, 02:45 PM   #2
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
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
 
Old 05-21-2008, 04:42 PM   #3
gfem
Member
 
Registered: Apr 2003
Distribution: Fedora
Posts: 126

Original Poster
Rep: Reputation: 15
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;
}
 
Old 05-21-2008, 04:58 PM   #4
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,455

Rep: Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172Reputation: 1172
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.
 
Old 05-21-2008, 08:57 PM   #5
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
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
 
Old 05-22-2008, 12:51 PM   #6
gfem
Member
 
Registered: Apr 2003
Distribution: Fedora
Posts: 126

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by sundialsvcs View Post
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 View Post
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

Last edited by gfem; 05-22-2008 at 12:53 PM.
 
Old 05-22-2008, 02:39 PM   #7
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
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
 
Old 05-22-2008, 09:24 PM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
I notice it finds 4 different versions of Perl, 5.8. 5,6,7,8
That's not really a good idea.
 
Old 05-23-2008, 04:33 PM   #9
gfem
Member
 
Registered: Apr 2003
Distribution: Fedora
Posts: 126

Original Poster
Rep: Reputation: 15
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
 
Old 05-23-2008, 06:11 PM   #10
eggixyz
Member
 
Registered: Apr 2008
Posts: 310

Rep: Reputation: 30
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
 
Old 06-05-2008, 04:59 PM   #11
gfem
Member
 
Registered: Apr 2003
Distribution: Fedora
Posts: 126

Original Poster
Rep: Reputation: 15
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
works on command line but not in bash script tara Linux - General 7 02-09-2009 04:57 AM
Where to put the script to be evoked at booting satimis Linux From Scratch 15 10-10-2005 09:39 PM
PERL script OK at command line, not in browser alvo Programming 4 12-19-2004 09:28 AM
shell script works form command line but not form crontab saifee General 1 10-14-2004 11:27 AM
Perl shell-out to script dunna work. Works on command line. Why? jlangelier Linux - Software 1 08-28-2004 03:00 AM


All times are GMT -5. The time now is 10:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration