LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-03-2012, 10:58 AM   #1
andrest964
LQ Newbie
 
Registered: Oct 2012
Posts: 5

Rep: Reputation: Disabled
Unhappy perl script runs fine from command line but fails when invoked by nagios


Hi Folks,

this is the command line:
[root@monitor libexec]#./check_acc_bk.pl localhost 2
97 Files backed up in the last 2 days

this is the same command invoked by nagios:
[1349278228] EXTERNAL COMMAND: SCHEDULE_FORCED_SVC_CHECK;localhost;Check Account Backups;1349278227
[1349278238] SERVICE ALERT: localhost;Check Account Backups;CRITICAL;HARD;4;No files backed up in the previous 2 days


in nagios commands.cfg:
Code:
# 'check_acc_bk.pl' command definition
define command{
        command_name    check_acc_bk
        command_line    $USER1$/check_acc_bk.pl $HOSTADDRESS$ $ARG1$
        }
then in local.cfg:
Code:
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             Check Account Backups
        active_checks_enabled           1
        check_interval                  15
        check_command                   check_acc_bk
        notifications_enabled           0
        }
and in check_acc_bk.pl:
Code:
#!/usr/bin/perl
use strict;
use File::Copy;
$|=1;
my $hostname = $ARGV[0] || die "Usage: (hostname) (days previous)\n";
my $delay = $ARGV[1] || '2' ; 
my $status="3";
my $msg="warning failed to run properly \n";
my $retval="0";
$delay = $delay -1;
#my $newfiles = changedfiles($delay);
&changedfiles($delay);
#print "The number of files changed in $delay is $retval\n";
chomp($retval);
#print STDERR"***$retval****in $delay days ******";
        if ($retval ne '0') {
                $status=0;
                $msg="$retval Files backed up in the last $delay days \n";
        } else {
                $delay = $delay +1;
                &changedfiles($delay);
                chomp($retval);
                #print STDERR"***$retval****in $delay days ******";
                #print "The number of files changed in $delay is $newfiles1 \n";
                if ($retval ne '0') {
                        $status=1;
                        $msg="$retval files backed up in the last $delay days \n";
                } else {
                        $status=2;
                        $msg="No files backed up in the  previous $delay days \n";
                }
        }


#=================
# Status Processing
#=================
# 3 UNKNOWN
# 2 CRITICAL
# 1 WARNING
# 0 OK
print $msg;
exit ($status);

#print  "======= subs  =================\n";
sub changedfiles($delay)
{
#       print"delay is $delay\n";
        my $com  = "sudo /usr/bin/find /root/accounting/backup/ -type f -mtime -$delay -print  | wc -l";
        my @lines = `$com`;
        my $num;
        foreach $num (@lines)
        {
                #print "the num = $num\n"; #put in for debugging
                if ($num > 0) {
                $retval = $num;
                }
        }

        return $retval;
}
oh and nagios has sudo rights on /usr/bin/find with no password.


I have another almost identical perl script that checks database file backups and it works fine.

Please help me Obiwan you are my last chance

Last edited by andrest964; 10-04-2012 at 12:32 AM. Reason: better layout
 
Old 10-03-2012, 12:06 PM   #2
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
Much easier to read if you use [code][/code] tags.

For debugging purposes can you output the value of $PATH at the beginning of the script? Not that I can see it should make a difference.

What distro are you running this on?

I don't quite understand this subroutine:
Code:
sub changedfiles($delay)
{
#       print"delay is $delay\n";
        my $com  = "sudo /usr/bin/find /root/accounting/backup/ -type f -mtime -$delay -print  | wc -l";
        my @lines = `$com`;
        my $num;
        foreach $num (@lines)
        {
                #print "the num = $num\n"; #put in for debugging
                if ($num > 0) {
                $retval = $num;
                }
        }

        return $retval;
}
Why are you allowing for more than one line to be returned?
 
Old 10-04-2012, 12:38 AM   #3
andrest964
LQ Newbie
 
Registered: Oct 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Smile

cool thank you for the formatting tip - I was wondering how that was achieved - I'm new to forum participation but have been working with open source since 1994 (was my first Linux install). In those days we used Kermit via a dialup modem (9600 baud) to reach the internet and it was mostly text docs.

I program mostly in Java - Perl is something I use every occasionally (once a year maybe) so please excuse my ignorance. I am only trying to capture one value from the $com command - any suggestions will be appreciated.

The subroutine is to count the number of new and changed files in a directory.

(Centos 5.x) Linux monitor.jagmobile.co.za 2.6.18-308.11.1.el5PAE #1 SMP Tue Jul 10 09:29:33 EDT 2012 i686 i686 i386 GNU/Linux

Nagios Core 3.3.1

perl-5.8.8-38.el5

Last edited by andrest964; 10-04-2012 at 12:43 AM.
 
Old 10-04-2012, 01:06 AM   #4
andrest964
LQ Newbie
 
Registered: Oct 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
I believe the root of the problem is in the system call:
Code:
 my $com  = "sudo /usr/bin/find /root/accounting/backup/ -type f -mtime -$delay -print  | wc -l";
however the following is more Nagios friendly
Code:
my $com  = system("sudo /usr/bin/find /root/accounting/backup/ -type f -mtime -$delay -print  | wc -l");
but this causes the subroutine to return 0;
Ideas/examples on how to capture the system calls output to a returnable variable would be great.
 
Old 10-04-2012, 02:01 AM   #5
andrest964
LQ Newbie
 
Registered: Oct 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
FYI: I just found out that Perl at the command line is not the same as Nagios embedded Perl
 
Old 10-04-2012, 03:39 AM   #6
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
What are the versions?
Put this in your script as a quick and dirty check
Code:
print `perl -v`
Since you need to capture the output of the wc command, stick with backticks. Also I think system() requires a list not a string, and may not directly support piping.

But what I would suggest is some more error checking ...
Code:
sub changedfiles($delay)
{
#       print"delay is $delay\n";
        my $com  = "sudo /usr/bin/find /root/accounting/backup/ -type f -mtime -$delay -print  | wc -l";
        my @lines = `$com`;
        my $rc = $?;
        if ($rc != 0) { die "Non-zero return code $rc from command"; }
        if ($#lines != 0) { die "Unexpected number of lines ".($#lines+1)." from command"; }

        $retval = $lines[0];
        return $retval;
}

Another thought: is Nagios running in a bash shell? Or at least some shell that supports pipes (|)? If not, it might be necessary to explicitly call bash -c "$com" or something like that.
 
Old 10-04-2012, 04:05 AM   #7
andrest964
LQ Newbie
 
Registered: Oct 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
It was a permissions issue - solved by:
1) moving the test to a remote machine using nrpe.
2) enabling sudo in nrpe.cfg.
3) configuring sudo to let nagios run perl scripting in libexec without a password.
4) and changing the monitoring machines config to accommodate the remote call.

WOOOOHOOOOO
 
Old 10-04-2012, 09:04 AM   #8
SecretCode
Member
 
Registered: Apr 2011
Location: UK
Distribution: Kubuntu 11.10
Posts: 562

Rep: Reputation: 102Reputation: 102
OK, so none of my suggestions then
Glad you fixed it!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl script runs in terminal but not when called by nagios nicko18 Linux - Software 1 04-11-2012 11:37 AM
[SOLVED] AWK runs in command line, but not in script PaulVanLil Linux - Newbie 4 03-03-2011 07:07 AM
Script runs fine from command line, but not when attempted from Cron hwest Linux - Newbie 8 12-11-2008 06:48 PM
Perl Script works from command line, but not when evoked by Nagios gfem Linux - General 10 06-05-2008 03:59 PM
script runs fine from a command line, but doesn't work from cron? kleptophobiac Linux - Software 5 05-03-2004 04:14 PM


All times are GMT -5. The time now is 07:03 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration