Custom plugin output (null)

Support forum for Nagios Core, Nagios Plugins, NCPA, NRPE, NSCA, NDOUtils and more. Engage with the community of users including those using the open source solutions.
Locked
User avatar
sebastiaopburnay
Posts: 105
Joined: Sun Oct 31, 2010 1:40 pm
Location: Lisbon, Portugal

Custom plugin output (null)

Post by sebastiaopburnay »

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:
nagios@my_Server:/usr/local/nagios/libexec#./my_get_consumo_ups.pl <my_UPS_IP> <my_Community>
CONSUMO OK - Consumo: 10.252kW; | 'Consumo[kW]'=10.252kW;11;14;
The command definition:
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: Select all

#!/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,
sebastiaopburnay
Last edited by sebastiaopburnay on Tue Jun 12, 2012 5:56 am, edited 1 time in total.
User avatar
jsmurphy
Posts: 989
Joined: Wed Aug 18, 2010 9:46 pm

Re: Custom plugin output (null)

Post by jsmurphy »

Interestingly enough there was another user a month back with the same issue, but I don't think we ever actually managed to work out what was causing it. As you already mentioned usually this is caused by a permissions issue or an execution failure related to the libs.

Two things you can try:
1. su - nagios and run the script... ensure that even as the Nagios user it is able to execute from the command line.
2. Disable the Nagios embedded perl handler in nagios/etc/nagios.cfg and try again.

If nothing else it might give us something we can work with to try and further debug the issue.
User avatar
sebastiaopburnay
Posts: 105
Joined: Sun Oct 31, 2010 1:40 pm
Location: Lisbon, Portugal

Re: Custom plugin output (null)

Post by sebastiaopburnay »

1. su - nagios and run the script... ensure that even as the Nagios user it is able to execute from the command line.
Yes, I have done that and still returns OK
2. Disable the Nagios embedded perl handler in nagios/etc/nagios.cfg and try again.
I've set the 'enable_embedded_perl=0' and restarted nagios, still dame rresult.

PS. I have already checked the '$?' system variable after execution and it correctly returns 0 (zero), so I'm guessing return code is not the problem.
agriffin
Posts: 876
Joined: Mon May 09, 2011 9:36 am

Re: Custom plugin output (null)

Post by agriffin »

Try changing

Code: Select all

use utils qw(%ERRORS $TIMEOUT);
use lib "/usr/local/nagios/libexec";
to

Code: Select all

use lib "/usr/local/nagios/libexec";
use utils qw(%ERRORS $TIMEOUT);
User avatar
sebastiaopburnay
Posts: 105
Joined: Sun Oct 31, 2010 1:40 pm
Location: Lisbon, Portugal

Re: Custom plugin output (null)

Post by sebastiaopburnay »

Thank you very much,

That change did the trick,

I will pass that information to the linuxforums where I posted the same doubt.
agriffin
Posts: 876
Joined: Mon May 09, 2011 9:36 am

Re: Custom plugin output (null)

Post by agriffin »

Glad I could help!
Locked