Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have modified an off-the-shelf Nagios plugin perl script to (in theory) return a one or zero according to the existence, or not, of a file on a remote linux server. The script runs a remote ssh session and logs in as the nagios user. The remote linux servers have private keys setup for that user, and on the bash command line the script works as expected, but when run as a plugin it always returns '1' (true) even if the file does not exist. Some help with the logic or a comment on why things are not working as expected within Nagios would be appreciated. I'd prefer to use this ssh login method rather than having to install nrpe on all the linux servers.

To run from a command line (assuming remote server has a user called nagios with a valid private key):

./check_reboot_required -e ssh -H remote-servers-ip-addr -p 'filename-to-check' -v

Ta.

#! /usr/bin/perl -w
#
#
# License Information:
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
############################################################################

use POSIX;
use strict;
use Getopt::Long;
use lib "/usr/lib/nagios/plugins" ;
use vars qw($host $opt_V $opt_h $opt_v $verbose $PROGNAME $pattern $opt_p $mmin $opt_e $opt_t $opt_H $status $state $msg $msg_q $MAILQ $SHELL $device $used $avail $percent $fs $blocks $CMD $RMTOS);
use utils qw(%ERRORS &print_revision &support &usage );

sub print_help ();
sub print_usage ();
sub process_arguments ();

$ENV{'PATH'}='';
$ENV{'BASH_ENV'}=''; 
$ENV{'ENV'}='';
$PROGNAME = "check_reboot_required";

Getopt::Long::Configure('bundling');
$status = process_arguments();

if ($status){
    print "ERROR: processing arguments\n";
    exit $ERRORS{'UNKNOWN'};
}

$SIG{'ALRM'} = sub {
    print ("ERROR: timed out waiting for $CMD on $host\n");
exit $ERRORS{'WARNING'};
};

$host = $opt_H;
$pattern = $opt_p;

print "Pattern >" . $pattern . "< " if $verbose;
alarm($opt_t);

#$CMD = "/usr/bin/find " . $pattern . " -type f 2>/dev/null| /usr/bin/wc -l";
$CMD = "[ -f " . $pattern . " ] && echo 1 || echo 0";

alarm($opt_t);

## get cmd output from remote system

if (! open (OUTPUT, "$SHELL $host $CMD|" ) ) {
    print "ERROR: could not open $CMD on $host\n";
    exit $ERRORS{'UNKNOWN'};
}

my $perfdata = "";
my $state = "3";
my $msg = "Indeterminate result";
# only first line is relevant in this iteration.
while (<OUTPUT>) {
                my $result = chomp($_);
                $msg = $result;
                print "Shell returned >" . $result . "< length is " . length($result)     . " " if $verbose;
                if ( $result == 1 ) {
                  $msg = "Reboot required (NB: Result still not accurate)" . $result ;
                  $state = $ERRORS{'WARNING'};
                  last;
                } elsif ( $result == 0 ) {
                  $msg = "No reboot required (NB: Result still not accurate) " .    $result ;
              $state = $ERRORS{'OK'};
              last;
            }
            else {
              $msg = "Output received, but it was neither a 1 nor a 0" ;
              last;
            }   

}

close (OUTPUT);
print "$msg | $perfdata\n";
exit $state;

#####################################
#### subs


sub process_arguments(){
    GetOptions
        ("V"   => \$opt_V, "version"    => \$opt_V,
         "v"   => \$opt_v, "verbose"    => \$opt_v,
         "h"   => \$opt_h, "help"   => \$opt_h,
         "e=s" => \$opt_e, "shell=s"    => \$opt_e,
             "p=s" => \$opt_p, "pattern=s"  => \$opt_p,
         "t=i" => \$opt_t, "timeout=i"  => \$opt_t,
         "H=s" => \$opt_H, "hostname=s" => \$opt_H
     );

if ($opt_V) {
    print_revision($PROGNAME,'$Revision: 1.0 $ ');
    exit $ERRORS{'OK'};
}

if ($opt_h) {
    print_help();
    exit $ERRORS{'OK'};
}

if (defined $opt_v ){
    $verbose = $opt_v;
}

    if (defined $opt_e ){
    if ( $opt_e eq "ssh" ) {
        if (-x "/usr/local/bin/ssh") {
        $SHELL = "/usr/local/bin/ssh";
        } elsif ( -x "/usr/bin/ssh" ) {
        $SHELL = "/usr/bin/ssh"; 
        } else {
                    print_usage();
                    exit $ERRORS{'UNKNOWN'};
                }

    } elsif ( $opt_e eq "rsh" ) {
        $SHELL = "/usr/bin/rsh";
    } else {
        print_usage();
                exit $ERRORS{'UNKNOWN'};
    }
    } else {
   print_usage();
       exit $ERRORS{'UNKNOWN'};
}

unless (defined $opt_t) {
    $opt_t = $utils::TIMEOUT ;  # default timeout
}

unless (defined $opt_H) {
    print_usage();
    exit $ERRORS{'UNKNOWN'};
}


return $ERRORS{'OK'};
}

sub print_usage () {
    print "Usage: $PROGNAME -e <shell> -H <hostname> -p <directory/file pattern> [-t <timeout>] [-v verbose]\n";
}

sub print_help () {
print_revision($PROGNAME,'$Revision: 0.1 $');
print "\n";
print_usage();
print "\n";
print "   Checks for the presence of a 'reboot-required' file on a remote host via SSH or RSH\n";
print "-e (--shell)     = ssh or rsh (required)\n";
print "-H (--hostname)  = remote server name (required)";
print "-p (--pattern)   = File pattern for find command (default = /var/run/reboot-required)\n";
print "-t (--timeout)   = Plugin timeout in seconds (default = $utils::TIMEOUT)\n";
print "-h (--help)\n";
print "-V (--version)\n";
print "-v (--verbose)   = debugging output\n";
print "\n\n";
support();
}
share|improve this question
    
Are you sure you are running the script in both cases as nagios user? –  Khaled May 9 '12 at 10:30
    
Yes - I've been monitoring auth.log on the target server: May 9 11:33:07 testserver sshd[29143]: Accepted publickey for nagios from 192.168.102.231 port 59138 ssh2 ... then ... May 9 11:33:07 testserver sshd[29143]: pam_unix(sshd:session): session opened for user nagios by (uid=0) –  Linker3000 May 9 '12 at 10:36
1  
can you not use nrpe? This is the reason nrpe was made so you can tell it to run local commands on remove servers from a nagios serve? –  Mike May 9 '12 at 11:32
1  
If you're not going to use NRPE or similar then I think you should try the check_by_ssh plugin which lets you run another plugin over ssh and handles all this for you. –  grifferz May 11 '12 at 19:32
1  
Another thing to be aware of is that Nagios Version 3 and above run the perl script check commands in a single perl process. You can turn this off by using a special comment code in the file. However, this should be avoided if at all possible as it creates a lot of extra work for the nagios server. nagios.sourceforge.net/docs/3_0/embeddedperl.html –  Fred the Magic Wonder Dog Oct 19 '13 at 15:32
show 2 more comments

1 Answer

Nagios runs plugins without any ENV, and therefore no $HOME... so it probably cannot find the ssh keys/identity info (you can simulate this by running your manual test through "env -i"). This is why check_by_ssh has options to explicitly specify this information.

As grifferz commented above, you should really be using check_by_ssh, since this perl script that you're using isn't much more than a (poor) re-implementation of it.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.