linuxperlnagiosnrpe

Why isn't /usr/local/bin/ checked in check_flexlm.pl ? Source included


Eventhough I have /usr/local/bin/lmstat the below script always fails with Cannot find "lmstat".

Can anyone see why that is the case?

use strict;
use Getopt::Long;
use vars qw($opt_V $opt_h $opt_F $opt_t $verbose $PROGNAME);
use FindBin;
use lib "$FindBin::Bin";
use lib '/usr/lib64/nagios/plugins';
use utils qw(%ERRORS &print_revision &support &usage);

$PROGNAME="check_flexlm";

sub print_help ();
sub print_usage ();

$ENV{'PATH'}='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin';
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';

Getopt::Long::Configure('bundling');
GetOptions
        ("V"   => \$opt_V,   "version"    => \$opt_V,
         "h"   => \$opt_h,   "help"       => \$opt_h,
         "v"   => \$verbose, "verbose"    => \$verbose,
         "F=s" => \$opt_F,   "filename=s" => \$opt_F,
         "t=i" => \$opt_t, "timeout=i"  => \$opt_t);

if ($opt_V) {
        print_revision($PROGNAME,'2.2.1');
        exit $ERRORS{'OK'};
}

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


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

unless (defined $opt_F) {
        $opt_F = $ENV{'LM_LICENSE_FILE'};
        unless (defined $opt_F) {
                print "Missing license.dat file\n";
                print_usage();
                exit $ERRORS{'UNKNOWN'};
        }
}
# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
        print "Timeout: No Answer from Client\n";
        exit $ERRORS{'UNKNOWN'};
};
alarm($opt_t);

my $lmstat = $utils::PATH_TO_LMSTAT ;
unless (-x $lmstat ) {
        print "Cannot find \"lmstat\"\n";
        exit $ERRORS{'UNKNOWN'};
}

Solution

  • Never assume you know what something is. Try printing the path to verify it is what you think it is:

    unless (-x $utils::PATH_TO_LMSTAT ) {
        print qq/Cannot find "lmstat" at <$utils::PATH_TO_LMSTAT>\n/;
        exit $ERRORS{'UNKNOWN'};
    }
    

    If $utils::PATH_TO_LMSTAT is a relative path (such as lmstat by itself) the -x is looking in the current directory. If it's a full path, maybe you have the string wrong.

    Note that your options handling can be a bit less unwieldy since you can specify multiple names for options in the same key:

    GetOptions(
            "V|version"    => \$opt_V,
            "h|help"       => \$opt_h,
            "v|verbose"    => \$verbose,
            "F|filename=s" => \$opt_F,
            "t|timeout=i"  => \$opt_t,
            );
    

    The "Secure Programming Techniques" chapter of Mastering Perl discusses many of the headaches of programs that call external programs.