edimusrex has asked for the wisdom of the Perl Monks concerning the following question:

I updated my code and have come a little closer to figuring out the issue. When I run this script from the command prompt it returns the expected information, how ever, in Nagios it does not recognize anything sent to it by the array. I tested this by assigning the value of the array to a variable and then sending it to output. The command line prints 4, but nagios prints 0. I am at a loss here Here's the code
#!/usr/bin/perl use warnings; use strict; use POSIX qw(strftime); use Date::Calc qw(Delta_Days); my $domainList = 'domains.txt'; my (@display, $stat, $expDate, $daysLeft, $diff, $spec, $len); my $date = strftime "%Y-%m-%d", localtime; my $flag = 0; open (FILE, $domainList); my @dom = <FILE>; foreach my $dm (@dom) { chomp $dm; $stat = `whois $dm | egrep "Registrar Registration Expiration Date +|Registry Expiry Date"`; $stat =~ m/(\d{4}-\d{2}-\d{2})/; $expDate = $1; $diff = &dateDiff($expDate); $len = length($dm); if ($len <= 7) { $spec = $dm."\t\t\t".$diff; } if ($len > 7 && $len <=16) { $spec = $dm."\t\t".$diff; } if ($len > 16) { $spec = $dm."\t".$diff; } if ($diff < 28 && $diff > 14) { $flag = 1; } if ($diff <= 14) { $flag = 2; } push @display, "$spec"; } close FILE; if ($flag == 2) { my $status = "CRIT: There are Certificates Expiring Soon. Please +Resolve"; unshift @display, $status; print join("\n",@display); exit 2; } if ($flag == 1) { my $status = "WARN: There are Certificates Expiring within a month +. Please Resolve"; unshift @display, $status; print join("\n",@display); exit 1; } else { my $status = "OK: Certificates Look good"; unshift @display, $status; print join("\n",@display); exit 0; } sub dateDiff { my $ex = $_[0]; $ex =~ m/(\d{4})-(\d{2})-(\d{2})/; my $y = $1; my $m = $2; my $d = $3; my @exDate = ($y, $m, $d); $date =~ m/(\d{4})-(\d{2})-(\d{2})/; my $yLoc = $1; my $mLoc = $2; my $dLoc = $3; my @curDate = ($yLoc, $mLoc, $dLoc); my $diffSub = Delta_Days(@curDate, @exDate); return($diffSub); }

Replies are listed 'Best First'.
Re: Nagios custom Perl check
by atcroft (Abbot) on Jul 21, 2014 at 19:13 UTC

    I recently had some difficulty with a plugin I was writing as well, but a read through the "Plugin Output" section of the Nagios Plugin Development Guidelines proved helpful.

    Also, just a thought, but have you looked at any of the Nagios-related modules on CPAN (such as Nagios::Plugin) to help reduce your coding time/effort? (Just a thought.)

    Hope that helps.

    Update: 2014-07-21 - Added link to Nagios::Plugin module.

      Yea, I was looking into that plugin but chose not to use it. I have also read over the guidelines but they don't mention anything in relation to printing out data from an array. Thanks for your response.
Re: Nagios custom Perl check
by Old_Gray_Bear (Bishop) on Jul 22, 2014 at 00:05 UTC
    So, why is `whois $dm | egrep "Registrar Registration Expiration Date|Registry Expiry Date"` not working on the remote machine? There are several possibilities (I leave the determination of which one up to you).

    Please note: the Nagios plugin doesn't run in your /home directory on the remote. This is what is known in the trade as a 'Great Big Clue'.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: Nagios custom Perl check
by McA (Priest) on Jul 22, 2014 at 01:31 UTC

    Hi,

    an addition to what was said before. You don't check whether your domain list file could be opened. You could add something like:

    ... unless(open (FILE, $domainList)) { print "CRIT: Can't read file '$domainList': $!\n"; exit 1; } my @dom = <FILE>; ...

    Regards
    McA

      Ahh, looking back at the comments I think I now know the issue. I do not point to the full path of the domain list file in my script and I am willing to bet that's the issue. Thanks again.
        It was indeed the domains.txt file not having the absolute path. Error checking the file open made that clear. Guess I did not have enough coffee yesterday. Thanks again