LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 06-05-2012, 06:30 AM   #1
sburnay
LQ Newbie
 
Registered: Sep 2011
Location: Lisbon, Portugal
Distribution: Ubuntu, CentOS & SUSE
Posts: 26

Rep: Reputation: Disabled
Nagios - interprets perl plugin output as (null)


Hi,

I developed my first Nagios plugin from scratch and I'm getting some issues regarding the way nagios interprets its output.

Execution from CLI returns good output (even $? is as 0), but nagios interface recognizes it as CRITICAL witn an output of '(null)'.

From command line:
Quote:
nagios@my_Server:/usr/local/nagios/l...consumo_ups.pl <my_UPS_IP> <my_Community>
CONSUMO OK - Consumo: 10.252kW; | 'Consumo[kW]'=10.252kW;11;14;
The command definition:
Quote:
define command{
command_name ups_consumos
command_line /usr/local/nagios/libexec/my_get_consumo_ups.pl <my_UPS_IP> <my_Community>
}
The plugin code
Code:
#!/usr/bin/perl -w

use Net::SNMP;
use Getopt::Long;
use warnings;
use strict;
use utils qw(%ERRORS $TIMEOUT);
use lib "/usr/local/nagios/libexec";

my $session = undef;
my $error = undef;
my $ups_current_I = undef;
my $ups_current_II = undef;
my $ups_current_III = undef;
my $ups_consumption_I = undef;
my $ups_consumption_II = undef;
my $ups_consumption_III = undef;
my $total_consumption = undef;
my $status = undef;

# requires a hostname and a community string as its arguments
($session,$error) = Net::SNMP->session(Hostname => $ARGV[0],
                                       Community => $ARGV[1]);

die "SNMP - Erro de Sessao: $error" unless ($session);

$ups_current_I = $session->get_request("1.3.6.1.2.1.33.1.4.4.1.3.1");
$ups_current_II = $session->get_request("1.3.6.1.2.1.33.1.4.4.1.3.2");
$ups_current_III = $session->get_request("1.3.6.1.2.1.33.1.4.4.1.3.3");

die "request error: ".$session->error unless (defined $ups_current_I);

$ups_consumption_I = $ups_current_I->{"1.3.6.1.2.1.33.1.4.4.1.3.1"} * 22 * 0.001;
$ups_consumption_II = $ups_current_II->{"1.3.6.1.2.1.33.1.4.4.1.3.2"} * 22 * 0.001;
$ups_consumption_III = $ups_current_III->{"1.3.6.1.2.1.33.1.4.4.1.3.3"} * 22 * 0.001;

$total_consumption = $ups_consumption_I + $ups_consumption_II + $ups_consumption_III;
$total_consumption=sprintf("%.0f" , $total_consumption);
$status="OK";
#check if output is critical
if($total_consumption > 14){
  $status="CRITICAL";
  print $status . ": Consumo " . $total_consumption . "kW\n";
  exit $ERRORS{"CRITICAL"};
  } else {
     if($total_consumption > 11) {
	$status="WARNING";
	print $status . ": Consumo " . $total_consumption . "kW\n";
        exit $ERRORS{"WARNING"};
        }
    } 
#close SNMP session
$session->close;
#print output and return exit code
print "CONSUMO " . $status . " -  Consumo: " . $total_consumption . "kW; | 'Consumo[kW]'=" .$total_consumption . "kW;11;14;\n";
exit $ERRORS{"OK"};

Well, I'm hoping this is a very simple issue.

All similar issues I've seen either end up related to perl 'use' clause (libraries or paths) or with permissions.

Best regards,
sburnay
 
Old 06-05-2012, 07:23 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,981
Blog Entries: 5

Rep: Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778
What is your services.cfg entry for this? Are <my_UPS_IP> <my_Community> hard coded values in the command.cfg or are you passing them as variables from services.cfg? If so how have you defined the variables in the command.cfg? How have you separated them in the services.cfg?
 
Old 06-05-2012, 08:44 AM   #3
sburnay
LQ Newbie
 
Registered: Sep 2011
Location: Lisbon, Portugal
Distribution: Ubuntu, CentOS & SUSE
Posts: 26

Original Poster
Rep: Reputation: Disabled
Well, I give the args directly in the command definition (commands.cfg),

which means I pass directly the host IP address (lets say 192.168.1.20) and SNMP community surrounded by ''s (as in 'myCommunity'), I don't use the $HOSTADDRESS$ nor a predefined $USERx$ for SNMP community.

In my service definition (services.cfg) I only call the command which is defined in commands.cfg (no arguments given on ervice definition)

Code:
# 4 C-Consumo_Energia
define service{
	use			PROIT-generic-service,srv-pnp
	host_name		PROIT-UPS
	service_description	C-Consumo_Energia
	is_volatile		0
	check_period		24x7
	max_check_attempts	3
	normal_check_interval	5
	retry_check_interval	1
	servicegroups		PROIT-Datacenter
	contact_groups		PROIT-admins
	notification_interval	240
	notification_period	24x7
	notification_options	c,r
	check_command		ups_consumos
	}
 
Old 06-05-2012, 06:15 PM   #4
serosensteinq
LQ Newbie
 
Registered: Jun 2012
Distribution: fedora
Posts: 5

Rep: Reputation: Disabled
I may be wrong about this, but from the scripts I've written in Bash for Nagios, the exit status number (0,1,2) is what tells Nagios whether it is OK, Warning or Critical (optionally 3 for Unknown). The output text is arbitrary as far as Nagios is concerned and is just for the user's information.
 
Old 06-06-2012, 06:25 AM   #5
sburnay
LQ Newbie
 
Registered: Sep 2011
Location: Lisbon, Portugal
Distribution: Ubuntu, CentOS & SUSE
Posts: 26

Original Poster
Rep: Reputation: Disabled
Yes serosensteinq,

And I've checked the '$?' system variable's value after execution of the script (either wuth the nagios user and root), it always came with the suposely correct value of 0 (zero = OK).

I even followed a sugestion I found on another forum of disabling the Nagios' perl interpreter, setting the 'enable_embedded_perl=0', but without results.
 
Old 06-06-2012, 07:03 AM   #6
serosensteinq
LQ Newbie
 
Registered: Jun 2012
Distribution: fedora
Posts: 5

Rep: Reputation: Disabled
Just to make sure, nagios is running under the "nagios" user, right?

I know you said you didn't need to include the $USER$ variable in the commands.cfg, but have you tried adding it? Also, Have you tried the full path of perl? (Although it shouldn't be needed either)

command_line /usr/bin/perl /usr/local/nagios/libexec/my_get_consumo_ups.pl <my_UPS_IP> <my_Community>
 
Old 06-06-2012, 07:12 AM   #7
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,981
Blog Entries: 5

Rep: Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778
Quote:
Originally Posted by serosensteinq View Post
Have you tried the full path of perl? (Although it shouldn't be needed either)

command_line /usr/bin/perl /usr/local/nagios/libexec/my_get_consumo_ups.pl <my_UPS_IP> <my_Community>
That wouldn't be necessary because the OP has the path of perl as the interpreter line in his perl script.

The prompt of his original command line above shows he is running as the nagios user when testing from command line. Hopefully the nagios process is also running as that user when he starts it.
 
Old 06-06-2012, 07:18 AM   #8
serosensteinq
LQ Newbie
 
Registered: Jun 2012
Distribution: fedora
Posts: 5

Rep: Reputation: Disabled
Yes, I saw that the command from the command line was being run as Nagios, and I assumed that the process was as well, but it never hurts to check.

Also, I know that the script defines the interpreter and that it shouldn't be necessary, just as adding the $USER$, but those are just some of the steps I would personally go through.
 
Old 06-06-2012, 11:21 AM   #9
hash_tag
LQ Newbie
 
Registered: Jun 2012
Posts: 3

Rep: Reputation: Disabled
I'm having the same issue. Please see the relevant config info from Nagios:

define service{
use unix-linux-service ; Name of service template to use
host_name RandomBox
service_description Database Partition
check_command check_nix_disk!/dev/mapper/z-db!10!85
}

# 'check_nix_disk'
define command{
command_name check_nix_disk
command_line /usr/bin/perl $USER1$/NagiosPlugin/check_disk.pl -H $HOSTADDRESS$ -F $ARG1$ -w $ARG2$ -c $ARG3$
}

I've disabled nagios embedded perl, and as you see above, I'm using the full path to my perl to run this script. From the NAGIOS Gui, this will get expanded to:

To expand: check_nix_disk!/dev/mapper/z-db!10!85
check_nix_disk /usr/bin/perl $USER1$/NagiosPlugin/check_disk.pl -H $HOSTADDRESS$ -F $ARG1$ -w $ARG2$ -c $ARG3$
-> /usr/bin/perl $USER1$/NagiosPlugin/check_disk.pl -H $HOSTADDRESS$ -F /dev/mapper/z-db -w 10 -c 85

Running the check_disk.pl from the command line (as root) gives this:
# /usr/bin/perl /usr/lib/nagios/plugins/NagiosPlugin/check_disk.pl -H 172.22.24.196 -F /dev/mapper/z-db -w 10 -c 85
DISK WARNING: Used=38% (Size: 20G, Available: 12G)

Running as nagios gave me problems due to permissions. After fixing these, I now get this (as nagios):
nagios@myhost:/usr/lib/nagios/plugins/NagiosPlugin> /usr/lib/nagios/plugins/NagiosPlugin/check_disk.pl -H 172.22.24.196 -F /dev/mapper/z-db -w 10 -c 85
DISK WARNING: Used=38% (Size: 20G, Available: 12G)

For testing I have printed the input from command line to check_disk.pl; When this script runs from Nagios, it does not create the file.

I use m// for checking in my perl script. It seems that the special variables come back empty when the script is called from Nagios. When it is called from the command line, they work fine.

Any further ideas?
 
Old 06-06-2012, 12:01 PM   #10
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,981
Blog Entries: 5

Rep: Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778
According to this page:
http://nagios.sourceforge.net/docs/3...eddedperl.html

You can tell the perl script itself not to use the Nagios embedded perl (starting with Nagios v3.x) even if it is used for other scripts by adding:
nagios: -epn
within the first 10 lines of the perl script for Nagios to detect it.

The page suggests that is a good debugging step to rule out embedded perl.

It also notes that embedded perl directives in nagios.cfg only have effect if you originally compiled with embedded perl options explicitly set. Reviewing your config log in the directory where you original ran the configure command and subsequent makes should show if you have it enabled. You should see a line like:
configure:8260: result: Embedded Perl: no
 
Old 06-06-2012, 12:14 PM   #11
sburnay
LQ Newbie
 
Registered: Sep 2011
Location: Lisbon, Portugal
Distribution: Ubuntu, CentOS & SUSE
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Yes, I saw that the command from the command line was being run as Nagios, and I assumed that the process was as well, but it never hurts to check.
Yes, Nagios sure is run by the nagios user.

Quote:
It also notes that embedded perl directives in nagios.cfg only have effect if you originally compiled with embedded perl options explicitly set.
Yes, I was suspecting that because I tried changing the enable_embedded_perl directive and noted nothing different after a service restart.

I'm in a hurry right now, but tomorow I will try to give the '/usr/bin/perl' as prefix of command_line in the command definition.

I will also explore the link sugested (http://nagios.sourceforge.net/docs/3...eddedperl.html)
 
Old 06-06-2012, 12:47 PM   #12
hash_tag
LQ Newbie
 
Registered: Jun 2012
Posts: 3

Rep: Reputation: Disabled
Hi again; I'm using Nagios® Core™ 3.3.1

I tried placing the below line early in my scripts.

# nagios: -epn

Also I set the below in nagios.cfg

enable_embedded_perl=0
use_embedded_perl_implicitly=0

Where before I had only set enable_embedded_perl=0. This didn't help either after restarting Nagios.

I'm wondering is there a way to tell if Nagios embedded perl is running these scripts?

Or better, can I somehow run my check_disk.pl script from Linux command line using Nagios embedded Perl? I tried changing the first line of my script to:

#!/usr/lib/nagios/p1.pl

But got no output.

Last edited by hash_tag; 06-06-2012 at 01:00 PM.
 
Old 06-06-2012, 01:19 PM   #13
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,981
Blog Entries: 5

Rep: Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778Reputation: 778
I didn't find a way to tell which options the compiled Nagios was compiled with. However as noted above if you did the compile yourself and have the original directory in which you ran the configure and make steps then you should have a config.log that shows what options were used. If it wasn't configured with embedded perl (which is default) you should see a line like:

configure:8260: result: Embedded Perl: no

The subsequent make would use that to NOT include embedded perl support in the compile.

Presumably that "no" would be "yes" if it WAS configured with embedded perl.

On my 3.x installation we have this set to no and haven't had any issues running perl scripts in Nagios.
 
Old 06-07-2012, 12:17 PM   #14
hash_tag
LQ Newbie
 
Registered: Jun 2012
Posts: 3

Rep: Reputation: Disabled
My issue is resolved. My plugin does file IO and wasn't opening a file for reading. Works from the command line possibly because I ran the script from the same directory as the script. Nagios runs the script from absolute path in another working directory.

sburnay: I would recommend you try running your script as nagios, from a different working directory, using an absolute path to run the script. See if it still works for you. Otherwise, ensure that you are using absolute paths where possible.
 
1 members found this post helpful.
Old 06-12-2012, 05:22 AM   #15
sburnay
LQ Newbie
 
Registered: Sep 2011
Location: Lisbon, Portugal
Distribution: Ubuntu, CentOS & SUSE
Posts: 26

Original Poster
Rep: Reputation: Disabled
Thumbs up Solved

Well, sorry for the long time to reply,

In the meanwhile I had devoleped a shell script to do the same thing as my original perl script.

In thr Nagios Support Forum someone sugested changing
Quote:
use utils qw(%ERRORS $TIMEOUT);
use lib "/usr/local/nagios/libexec";
by
Quote:
use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS $TIMEOUT);
And the perl script began giving a good output for Nagios to interpret.

Thank you for the brainstorming.
 
2 members found this post helpful.
  


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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nagios Check_nrpe returns no output in Nagios but works in terminal dave0821 Linux - Software 1 04-18-2012 06:26 PM
[SOLVED] Nagios Returning (null) 4Paul4 Linux - Software 3 07-12-2011 10:53 PM
if output to /dev/null how can i still get some output ?? naveen76 Linux - Software 2 11-23-2009 10:12 AM
Nagios - No output returned from plugin nishith Linux - Software 16 07-12-2009 03:07 PM
LXer: Installing Eclipse, the Epic Perl plugin and my first Perl GUI program LXer Syndicated Linux News 0 05-08-2009 06:41 PM


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

Main Menu
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