[Nagiosplug-devel] return code

Stanley Hopcroft Stanley.Hopcroft at IPAustralia.Gov.AU
Wed Apr 14 16:36:01 CEST 2004


Dear Sir,

I am writing to thank you for your letter and say,

On Wed, Apr 14, 2004 at 08:56:39PM +0200, Marco Barrera wrote:
> Hi All,
> 
> Hope I am starting a discussion in the correct forum, I think this is 
> more of a development issue rather than anything else.

  ... agreed.

> 
> I have been seeing a problem with a my plugin firebox
> 
> I am using some perl code to submit commands containing:
> 
> =========================================
> #!/usr/bin/perl -w
> 
> use lib "/usr/lib/nagios/plugins";
> use utils qw($TIMEOUT %ERRORS);
> 
> 
> my $state = 'OK';
> 
> my $memtotal = 0;
> my $memused = 0;
> my $memfree = 0;
> my $memshared = 0;
> my $membuffers = 0;
> my $memcached = 0;
> my $Mensaje = '';
> 
> 
> open(OUTPUT,"/usr/sbin/cbsh 372.168.0.154 DDDDedo9 -c 'status all' | grep 'Mem:'|");
> while (<OUTPUT>){
>      if (/^Mem:\s+(\d.*)/) { $Mensaje = $1; last; }
> }
> 
> ($memtotal, $memused, $memfree, $memshared, $membuffers, $memcached) = split /\s+/, $Mensaje, 6;
> 
> print "Stest $memfree $membuffers\n";
> exit $ERRORS{$state};
> =============================================
> 
> When I run the plugin
> manually, it consistently works,
> 
> [root at csm-server plugins]# ./check_wg-mem.pl
> Stest 18944000 14516224
> 
> but when it is run by
> nagios, the exit code is frequently wrong.

My comments are

1 I can't see where $status is set apart from the initialisation (= 
'OK') => the plugin will always return OK or something else if the 
plugin abends. There doesn't seem any way for it to return other than OK 
with a normal exit.

2 the /bin/sh pipeline is not checked for (fork) errors, so the result 
could be CRITICAL (code in base/checks.c IIRC traps no output from the 
plugin and sets the return code to CRITICAL).

I think you need to trap an open failure

 open(OUTPUT,"/usr/sbin/cbsh 372.168.0.154 DDDDedo9 -c 'status all' | 
grep 'Mem:'|") or
  do {
    print "open failed - fork() or other /bin/sh problem: $!" ;
    exit $ERRORS{'UNKNOWN'} ;
     } ;

and the close failure (in case the child processes forked by /bin/sh 
fail)

close OUTPUT or 
 do {

 } ;

3 You may want to use the 'strict' pragma to have Perl detect anything 
risky (at compile time).

4 If you are using embedded Perl, that may be causing problems. You 
could try without it.

5 You should always try the command from the CLI logged in as the Nagios 
user; this will reveal any permissions problems.

Yours sincerely.



-- 
------------------------------------------------------------------------
Stanley Hopcroft
------------------------------------------------------------------------

'...No man is an island, entire of itself; every man is a piece of the
continent, a part of the main. If a clod be washed away by the sea,
Europe is the less, as well as if a promontory were, as well as if a
manor of thy friend's or of thine own were. Any man's death diminishes
me, because I am involved in mankind; and therefore never send to know
for whom the bell tolls; it tolls for thee...'

from Meditation 17, J Donne.




More information about the Devel mailing list