Solved

Argument "" isn't numeric in numeric eq (==)  and some code fix

Posted on 2009-04-01
18
1,521 Views
Last Modified: 2012-06-27
Hi,

I am working in perl

My perl file checks the file in a folder with max age

I pass the directory and directory masks by parameters

like

perl rsda-check-file-age2.pl -d /shouse/in -m *.{xml,eot}

user can pass the masks in parameter like * or *.{xml,eot}

Then I add directory path and mask together by
<$d_opt.$m_opt>

in line

my ($opt_f, $age_in_secs) = @{(sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0]};

Is this fine the way I did.

Also, if I get any file that should pass in my variable $opt_f

I am declaring my ( $opt_f,..

on the top of perl file then I am passing in condition

if (! $d_opt) {
      my ($opt_f, $age_in_secs) = @{(sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0]};
      print "file name=$name, age=$age_in_secs\n";

}

Can I use 2 time "my" for the same varible like u can see in my code below.

my ($opt_f

If not how should I pass the file name in $opt_f in condition
if (! $d_opt) {
      my ($opt_f
}


My above

aicimcc@rsda-pbhi5:/usr/local/nagios/libexec$ perl rsda-check-file-age2.pl -d /shouse/in -m *
Argument "" isn't numeric in numeric eq (==) at rsda-check-file-age2.pl line 63.
Argument "check_apt" isn't numeric in numeric eq (==) at rsda-check-file-age2.pl line 63.
aicimcc@rsda-pbhi5:/usr/local/nagios/libexec$


line 63 if ($m_opt == '') {
Some part of code

 

use List::Util  qw(reduce);

 

sub print_help ();

sub print_usage ();

 

my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V, $name, $age_in_secs,$d_opt,$m_opt);

my ($result, $message, $age, $size, $st);

 

$PROGNAME="check_file_age";

 

$opt_w = 240;

$opt_c = 600;

$opt_W = 0;

$opt_C = 0;

$opt_f = "";

$d_opt = "";

$m_opt = "";

 

Getopt::Long::Configure('bundling');

GetOptions(

        "V"   => \$opt_V, "version"     => \$opt_V,

        "h"   => \$opt_h, "help"        => \$opt_h,

        "f=s" => \$opt_f, "file"        => \$opt_f,

        "w=f" => \$opt_w, "warning-age=f" => \$opt_w,

        "W=f" => \$opt_W, "warning-size=f" => \$opt_W,

        "c=f" => \$opt_c, "critical-age=f" => \$opt_c,

        "C=f" => \$opt_C, "critical-size=f" => \$opt_C,

        "d=s"    => \$d_opt,

    	"m=s"    => \$m_opt);

    	

    	

if ($m_opt == '') {

	$m_opt = "*";

}

 

 

if (! $d_opt) {

	my ($opt_f, $age_in_secs) = @{(sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0]};

	print "file name=$name, age=$age_in_secs\n";

 

}

Open in new window

0
Comment
Question by:tia_kamakshi
  • 8
  • 7
  • 2
  • +1
18 Comments
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
If you use my twice like you have (the second my is inside a block), perl will create a new variable named $opt_f and $age_in_secs that will only exist inside that block.  Any value you assign to it will be lost once that block is finished.  Remove the my keyword.

Also note that -M returns age in days, not seconds.
0
 

Author Comment

by:tia_kamakshi
Comment Utility
Many thanks for your reply,

Can you please help me in fixing this error

aicimcc@rsda-pbhi5:/usr/local/nagios/libexec$ perl rsda-check-file-age2.pl -d /shouse/in -m *
Argument "" isn't numeric in numeric eq (==) at rsda-check-file-age2.pl line 63.
Argument "check_apt" isn't numeric in numeric eq (==) at rsda-check-file-age2.pl line 63.
aicimcc@rsda-pbhi5:/usr/local/nagios/libexec$


Line 63 is

line 63 if ($m_opt == '') {


I am doing

if ($m_opt == '') {
      $m_opt = "*";
}


If $m_opt is blank or undef then put the

value "*" in $m_opt

Thanks then I will test my code

Kind Regards
0
 

Author Comment

by:tia_kamakshi
Comment Utility
Above error is fixed.
I have used if ($m_opt eq '')

and it works

Now I am getting error at line 69 saying
perl pbms-check-file-age2.pl -d /smarthouse/in
Can't use an undefined value as an ARRAY reference at pbms-check-file-age2.pl line 69.

Line 69 is
      ($opt_f, $age_in_secs) = @{(sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0]};

I have removed my from inside if condition.

if (! $d_opt eq '') {
      ($opt_f, $age_in_secs) = @{(sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0]};
      print "file name=$name, age=$age_in_secs\n";
}

If you wish to see more code. I have pasted in my opening question.

Please guide

Many thanks
0
 
LVL 39

Expert Comment

by:Adam314
Comment Utility
With this code:
    if (! $d_opt) {
        my ($opt_f, $age_in_secs) = @{(sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0]};
        print "file name=$name, age=$age_in_secs\n";
    }


The code in the if block is only executed when you don't have $d_opt, but then you use $d_opt in the globbing (<$d_opt.$m_opt>).
0
 
LVL 3

Expert Comment

by:WeiXi
Comment Utility
1)
Yes,
> if (!$d_opt)
wouldn't work. However, the author already changed that to
> if (! $d_opt eq '')
in the latest code snippet. This works as intended, but in a rather screwed way :)

!$d_opt boolifies $d_opt and negates it, thus yielding false. Then, due to the eq-operator, the false is stringified and compared to the empty string, which just happens to be the stringified version of false. In the end, every string in $d_opt that's not empty and not equal to "0" passes the if.
I guess you intended a if ($d_opt ne '') using perls ne-operator (not equal in string context).

In perl, the most straight-forward way for such conditionals is:
> if ($d_opt) {
That simply boolifies $d_opt, which passes as true for any non-empty string not equal to "0".


2)
Your usage of the globbing operator
> <$d_opt.$m_opt>
does not work. The globbing operator < > is an interpolating quote-like operator. Think of it as a string literal. Thus, the . is not interpreted as a concatination operator but a literal '.'

Additionally, using your command line example
> perl rsda-check-file-age2.pl -d /shouse/in -m *.{xml,eot}
the concatination of $d_opt and $m_opt will miss a slash between them.

So, you are actually globbing for "/shouse/in.*.{xml,eot}" instead of "/shouse/in/*.{xml,eot}" (which I guess you intended).

This is the reason, that your globbing <$d_opt.$m_opt> doesn't return anything, i.e. an empty list. The first element of an emptly list is undefined and thus
> (sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0]
is undefined. With @{ } you try to dereference the undefined value as an array, which causes the error message.

To fix the missing slash simply use <$d_opt/$m_opt> for the globbing.

However, you should not rely on finding a file. The directory might be empty after all. Always ending up in error in such cases is not very elegant. Something like this would be more reliable:
>      my $found_ref = (sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0];
>      if ($found_ref) {
>          ($opt_f, $age_in_secs) = @$found_ref;
>      }
>      else {
>          # issue file not found message, or wathever
>      }


3)
What are you intending to achieve with $opt_f?
Any -f parameter from the user is never used but overwritten with the name of the oldest file. Is this really what you try to achieve? If yes, then why do you read it in via GetOptions()? Simply leave out the line
>         "f=s" => \$opt_f, "file"        => \$opt_f,
And maybe it would also be wise to use a different name, otherwise you could get confused :)
0
 

Author Comment

by:tia_kamakshi
Comment Utility
Many Thanks for your great explanation.

There is a nagios system, which has the capability to check the file age , file size etc.
It alerts via mail or sms if the file age is more than defined time

The parameters are already passed in the script
like -f filename - w 25 -c 35

here it checks the the file name and we passed the warning and critical age of file in sec

I need to add so that it should check the files in the directory and warns us if file age is greater than passed warning time or critical warning time or age of file

To achive this I have added 2 options -d and -M

Where I am passing directory path in -d and in -M I am passing masking of file

I mean -d directoryPath -M *.{xml,eot}

I am doing If I get the file with max age I am passing to nagios file variable $opt_f

Like

if (! $d_opt eq '') {
      my($name, $age_in_days) = @{(sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0]};
      $opt_f = $name;
}

Then nagios does its own bits with the file

If I write
my($name, $age_in_days) = @{(sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (</home/actimcc/archive/*.{xml,eot}>))[0]};

Then it works greate for me

But If I add directory path and mask variable here like

my($name, $age_in_days) = @{(sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0]};

Then I get the error saying that
Can't use an undefined value as an ARRAY reference at pbms-check-file-age2.pl line 74.

writting again Line 74 is
my($name, $age_in_days) = @{(sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0]};

I am executing my program

perl abms-check-file-age2.pl -d /home/shouse/in -w 25 -m *.{xml,eot}

How should I fix this so that I can pass $d_opt, $m_opt from the option and I should be able to find file with max age for me

@{(sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0]};

I am doing something like

if (! $d_opt eq '') {
      
      my($name, $age_in_days) = @{(sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0]};
      
      $opt_f = $name;
      print "name=$name, age=$age_in_days\n";
      print "name=$name, age in seconds=",$age_in_days*60*60*24,"\n";
}

Please guide

Many Thanks
#! /usr/bin/perl -w

# $Id: check_file_age.pl 1750 2007-07-07 11:54:29Z psychotrahe $
 

# check_file_age.pl Copyright (C) 2003 Steven Grimm <koreth-nagios@midwinter.com>

#

# Checks a file's size and modification time to make sure it's not empty

# and that it's sufficiently recent.

#

#

# 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 (or with Nagios);  if not, write to the

# Free Software Foundation, Inc., 59 Temple Place - Suite 330,

# Boston, MA 02111-1307, USA
 

use strict;

use English;

use Getopt::Long;

use File::stat;

use vars qw($PROGNAME);

use lib "/usr/local/nagios/libexec" ;

use utils qw (%ERRORS &print_revision &support);

use List::Util  qw(reduce);
 

sub print_help ();

sub print_usage ();
 

my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V, $d_opt,$m_opt);

my ($result, $message, $age, $size, $st);
 

$PROGNAME="check_file_age";
 

$opt_w = 240;

$opt_c = 600;

$opt_W = 0;

$opt_C = 0;

$opt_f = "";

$d_opt = "";

$m_opt = "";
 

Getopt::Long::Configure('bundling');

GetOptions(

        "V"   => \$opt_V, "version"     => \$opt_V,

        "h"   => \$opt_h, "help"        => \$opt_h,

        "f=s" => \$opt_f, "file"        => \$opt_f,

        "w=f" => \$opt_w, "warning-age=f" => \$opt_w,

        "W=f" => \$opt_W, "warning-size=f" => \$opt_W,

        "c=f" => \$opt_c, "critical-age=f" => \$opt_c,

        "C=f" => \$opt_C, "critical-size=f" => \$opt_C,

        "d=s"    => \$d_opt,

    	"m=s"    => \$m_opt);

    	

    	

if ($m_opt eq '') {

	$m_opt = "*";

}
 

	print "mask=$m_opt\n";

	print "dir=$d_opt\n";

	print "comb=$d_opt.$m_opt\n";
 

if (! $d_opt eq '') {

	

	

	my($name, $age_in_days) = @{(sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt.$m_opt>))[0]};

	

	$opt_f = $name;

	

	print "name=$name, age=$age_in_days\n";

		

	print "name=$name, age in seconds=",$age_in_days*60*60*24,"\n";

	

	

	

}
 

print "optf is=$opt_f\n";
 

if ($opt_V) {

        print_revision($PROGNAME, '$Revision: 1750 $');

        exit $ERRORS{'OK'};

}else

{

	print "3\n";

}
 

print "4\n";
 

if ($opt_h) {

        print_help();

        exit $ERRORS{'OK'};

}else

{

	print "2\n";

}
 

$opt_f = shift unless ($opt_f);

print "optf2 is=$opt_f\n";

if (! $opt_f) {

        print "FILE_AGE UNKNOWN: No file specified\n";

        exit $ERRORS{'UNKNOWN'};

}else

{

	print "1\n";

}
 

# Check that file exists (can be directory or link)

unless (-e $opt_f) {

        print "FILE_AGE CRITICAL: File not found - $opt_f\n";

        exit $ERRORS{'CRITICAL'};

}
 

$st = File::stat::stat($opt_f);

$age = time - $st->mtime;

$size = $st->size;
 
 

$result = 'OK';
 

if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {

	$result = 'CRITICAL';

}

elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {

	$result = 'WARNING';

}
 

print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes\n";

exit $ERRORS{$result};
 

sub print_usage () {

	print "Usage:\n";

	print "  $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] -f <file>\n";

	print "  $PROGNAME [-h | --help]\n";

	print "  $PROGNAME [-V | --version]\n";

}
 

sub print_help () {

	print_revision($PROGNAME, '$Revision: 1750 $');

	print "Copyright (c) 2003 Steven Grimm\n\n";

	print_usage();

	print "\n";

	print "  <secs>  File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";

	print "  <size>  File must be at least this many bytes long (default: crit 0 bytes)\n";

	print "\n";

	support();

}

Open in new window

0
 

Author Comment

by:tia_kamakshi
Comment Utility
I have changed my code as below

if (! $d_opt eq '') {      
      
      my $found_ref = (sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} (<$d_opt$m_opt>))[0];
            if ($found_ref) {
                my ($name, $age_in_days) = @$found_ref;
                $opt_f = $name;
                print "Found\n";
            }
            else {
                # file not found
                print "Not Found\n";
      }
     
     
and when I am passing parameters like

 perl pbms-check-file-age2.pl -d /home/abtimcc/archive/ -w 25 -M *.{xml}
 
 It works but it is not looking only xml files it is looking other files and bring result for eot file
 
 Please help me in fixing it
 
 Many Thanks again
0
 
LVL 3

Expert Comment

by:WeiXi
Comment Utility
Because you pass parameter -M (upper case M) on the command line
> perl pbms-check-file-age2.pl -d /home/abtimcc/archive/ -w 25 -M *.{xml}
while using the -m (lower case m) parameter for feeding $m_opt
>          "m=s"    => \$m_opt);
$m_opt stays empty and then defaults to '*':
> if ($m_opt eq '') {
>       $m_opt = "*";
> }

Thus, you are globbing </home/abtimcc/archive/*> which, quite naturally, yields all files in that directory.
0
 
LVL 3

Expert Comment

by:WeiXi
Comment Utility
Just a thought for easing your debugging. As also explained in
http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators
I would strongly suggest you use the glob() function instead of the <> operator. It is doing the same thing but is much less prone to mishandling due to interpolation or its ambiguities.

Also, for the sake of debugging, print the string that is globbed and cross-check it.

if (! $d_opt eq '') {      

!>      my $glob_pattern = $d_opt . $m_opt;
!>      print "globbing for: $glob_pattern\n";
!>      my $found_ref = (sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} glob $glob_pattern)[0];
            if ($found_ref) {
                my ($name, $age_in_days) = @$found_ref;
                $opt_f = $name;
                print "Found\n";
            }
            else {
                # file not found
                print "Not Found\n";
      }
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
splain
/usr/bin/splain: Reading from STDIN
Argument "check_apt" isn't numeric in numeric eq (==) at rsda-check-file-age2.pl line 63.
Argument "check_apt" isn't numeric in numeric eq (==) at
        rsda-check-file-age2.pl line 63 (#1)
    (W numeric) The indicated string was fed as an argument to an operator
    that expected a numeric value instead.  If you're fortunate the message
    will identify which operator was so unfortunate.


You can also get this with -Mdiagniostics
0
 

Author Comment

by:tia_kamakshi
Comment Utility
Hi,

I have done the changes as you have suggested

if (! $d_opt eq '') {      

      my $glob_pattern = $d_opt . $m_opt;
      print "globbing for: $glob_pattern\n";
      my $found_ref = (sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} glob $glob_pattern)[0];
            if ($found_ref) {
                my ($name, $age_in_days) = @$found_ref;
                $opt_f = $name;
                print "Found\n";
            }
            else {
                # file not found
                print "Not Found\n";
      }
     
     
Now I am getting an error saying

abtimcc@cdbms-pbhi5:/usr/local/nagios/libexec$ perl cdbms-check-file-age2.pl -d /home/abtimcc/archive/ -w 25 -M *.{xml}
Missing right curly or square bracket at cdbms-check-file-age2.pl line 155, at end of line
syntax error at cdbms-check-file-age2.pl line 155, at EOF
Execution of cdbms-check-file-age2.pl aborted due to compilation errors.
abtimcc@cdbms-pbhi5:/usr/local/nagios/libexec$ perl cdbms-check-file-age2.pl -d /home/abtimcc/archive/ -w 25 -M *.{xml}


Please help me in fixing this

Many Thanks for your co-operation
#! /usr/bin/perl -w

# $Id: check_file_age.pl 1750 2007-07-07 11:54:29Z psychotrahe $
 

# check_file_age.pl Copyright (C) 2003 Steven Grimm <koreth-nagios@midwinter.com>

#

# Checks a file's size and modification time to make sure it's not empty

# and that it's sufficiently recent.

#

#

# 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 (or with Nagios);  if not, write to the

# Free Software Foundation, Inc., 59 Temple Place - Suite 330,

# Boston, MA 02111-1307, USA
 

use strict;

use English;

use Getopt::Long;

use File::stat;

use vars qw($PROGNAME);

use lib "/usr/local/nagios/libexec" ;

use utils qw (%ERRORS &print_revision &support);

use List::Util  qw(reduce);
 

sub print_help ();

sub print_usage ();
 

my ($opt_c, $opt_f, $opt_w, $opt_C, $opt_W, $opt_h, $opt_V, $d_opt,$m_opt);

my ($result, $message, $age, $size, $st);
 

$PROGNAME="check_file_age";
 

$opt_w = 240;

$opt_c = 600;

$opt_W = 0;

$opt_C = 0;

$opt_f = "";

$d_opt = "";

$m_opt = "";
 

Getopt::Long::Configure('bundling');

GetOptions(

        "V"   => \$opt_V, "version"     => \$opt_V,

        "h"   => \$opt_h, "help"        => \$opt_h,

        "f=s" => \$opt_f, "file"        => \$opt_f,

        "w=f" => \$opt_w, "warning-age=f" => \$opt_w,

        "W=f" => \$opt_W, "warning-size=f" => \$opt_W,

        "c=f" => \$opt_c, "critical-age=f" => \$opt_c,

        "C=f" => \$opt_C, "critical-size=f" => \$opt_C,

        "d=s"    => \$d_opt,

    	"m=s"    => \$m_opt);

    	

    	

if ($m_opt eq '') {

	$m_opt = "*";

}
 

	print "mask=$m_opt\n";

	print "dir=$d_opt\n";

	print "comb=$d_opt.$m_opt\n";
 

if (! $d_opt eq '') {      
 

      my $glob_pattern = $d_opt . $m_opt;

      print "globbing for: $glob_pattern\n";

      my $found_ref = (sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} glob $glob_pattern)[0];

            if ($found_ref) {

                my ($name, $age_in_days) = @$found_ref;

                $opt_f = $name;

                print "Found\n";

            }

            else {

                # file not found

                print "Not Found\n";

      }
 

print "optf is=$opt_f\n";
 

if ($opt_V) {

        print_revision($PROGNAME, '$Revision: 1750 $');

        exit $ERRORS{'OK'};

}else

{

	print "3\n";

}
 

print "4\n";
 

if ($opt_h) {

        print_help();

        exit $ERRORS{'OK'};

}else

{

	print "2\n";

}
 

$opt_f = shift unless ($opt_f);

print "optf2 is=$opt_f\n";

if (! $opt_f) {

        print "FILE_AGE UNKNOWN: No file specified\n";

        exit $ERRORS{'UNKNOWN'};

}else

{

	print "1\n";

}
 

# Check that file exists (can be directory or link)

unless (-e $opt_f) {

        print "FILE_AGE CRITICAL: File not found - $opt_f\n";

        exit $ERRORS{'CRITICAL'};

}
 

$st = File::stat::stat($opt_f);

$age = time - $st->mtime;

$size = $st->size;
 
 

$result = 'OK';
 

if (($opt_c and $age > $opt_c) or ($opt_C and $size < $opt_C)) {

	$result = 'CRITICAL';

}

elsif (($opt_w and $age > $opt_w) or ($opt_W and $size < $opt_W)) {

	$result = 'WARNING';

}
 

print "FILE_AGE $result: $opt_f is $age seconds old and $size bytes\n";

exit $ERRORS{$result};
 

sub print_usage () {

	print "Usage:\n";

	print "  $PROGNAME [-w <secs>] [-c <secs>] [-W <size>] [-C <size>] -f <file>\n";

	print "  $PROGNAME [-h | --help]\n";

	print "  $PROGNAME [-V | --version]\n";

}
 

sub print_help () {

	print_revision($PROGNAME, '$Revision: 1750 $');

	print "Copyright (c) 2003 Steven Grimm\n\n";

	print_usage();

	print "\n";

	print "  <secs>  File must be no more than this many seconds old (default: warn 240 secs, crit 600)\n";

	print "  <size>  File must be at least this many bytes long (default: crit 0 bytes)\n";

	print "\n";

	support();

}

Open in new window

0
 
LVL 3

Expert Comment

by:WeiXi
Comment Utility
     print "mask=$m_opt\n";
      print "dir=$d_opt\n";
      print "comb=$d_opt.$m_opt\n";
 
if (! $d_opt eq '') {      
 
      my $glob_pattern = $d_opt . $m_opt;
      print "globbing for: $glob_pattern\n";
      my $found_ref = (sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} glob $glob_pattern)[0];
            if ($found_ref) {
                my ($name, $age_in_days) = @$found_ref;
                $opt_f = $name;
                print "Found\n";
            }
            else {
                # file not found
                print "Not Found\n";
            }

#  THIS BRACKET IS MISSING
}
#  THIS BRACKET IS MISSING

print "optf is=$opt_f\n";
0
 

Author Comment

by:tia_kamakshi
Comment Utility
Hi,

Now it works great.

But it is not doing the file mask

I asked to look files with extension {xml,err}

It is still looking from all files.

Here is the resut when I execute this perl file


perl cdbms-check-file-age2.pl -d /home/abtimcc/archive/ -w 25 -M *.{xml,err}
Unknown option: M
mask=*
dir=/home/abtimcc/archive/
comb=/home/abtimcc/archive/.*
mask=*
dir=/home/abtimcc/archive/
comb=/home/abtimcc/archive/.*
globbing for: /home/abtimcc/archive/*
Found
optf is=/home/abtimcc/archive/MESSAGE-0000000214.eot
3
4
2
optf2 is=/home/abtimcc/archive/MESSAGE-0000000214.eot
1
FILE_AGE CRITICAL: /home/abtimcc/archive/MESSAGE-0000000214.eot is 29053989 seconds old and 57 bytes



Also, If I have to check from all files then what parameter i should pass like -M *

I mean -M * will work for all or not

Many Thanks for your great help

Kind Regards
0
 
LVL 3

Expert Comment

by:WeiXi
Comment Utility
1)
My I, very politely, suggest that you read the advice given to you more carefully.

I already wrote, that you use an UPPER CASE M for the command line parameter
> perl cdbms-check-file-age2.pl -d /home/abtimcc/archive/ -w 25 -M *.{xml,err}
but you need a LOWER CASE m:
> perl cdbms-check-file-age2.pl -d /home/abtimcc/archive/ -w 25 -m *.{xml,err}

You should also pay more attention to the error and warning messages, as they are telling you:
> Unknown option: M
The m-option (lower case m!) would habe been known.

The funny thing about this is, that in your original question you started off with -m but changed to -M during the thread :)

2)
If you want to check for all files you can pass -m *, as you suggested, or just don't pass any -m parameter
> perl cdbms-check-file-age2.pl -d /home/abtimcc/archive/ -w 25
because it defaults to *.
0
 

Author Comment

by:tia_kamakshi
Comment Utility
Many Many Thanks WeiXi for great explanation and now my code is working good.

I was confused with first reply from Adam314. Where he says that
"Also note that -M returns age in days, not seconds."

So, from that time I was sometime trying with -m and sometime -M.  I thought to ask this question in new.

Anyway -m works for me

 My code is working now and giving results now
 
 But I have a question that in mask I am passing
 -m *.{eot,xml} in parameter
 
globbing prints

globbing for: /home/abtimcc/archive/*.eot

and also giving result for .eot file

Looks it is not looking xml file now.
I was expecting it to print

globbing for: /home/abtimcc/archive/*.{eot,xml}

Is it not looking xml files in above case. How can we fix this
 
 
 perl cdbms-check-file-age2.pl -d /home/abtimcc/archive/ -w 25 -m *.{eot,xml}
mask=*.eot
dir=/home/abtimcc/archive/
comb=/home/abtimcc/archive/.*.eot
mask=*.eot
dir=/home/abtimcc/archive/
comb=/home/abtimcc/archive/.*.eot
globbing for: /home/abtimcc/archive/*.eot
name=/home/abtimcc/archive/MESSAGE-0000000214.eot, age=336.559849537037
name=/home/abtimcc/archive/MESSAGE-0000000214.eot, age in seconds=29078771
optf is=/home/abtimcc/archive/MESSAGE-0000000214.eot
4
optf2 is=/home/abtimcc/archive/MESSAGE-0000000214.eot
FILE_AGE CRITICAL: /home/abtimcc/archive/MESSAGE-0000000214.eot is 29078771 seconds old and 57 bytes
abtimcc@cdbms-pbhi5:/usr/local/nagios/libexec$

Really thanks for your great support
0
 
LVL 3

Expert Comment

by:WeiXi
Comment Utility
> I was confused with first reply from Adam314. Where he says that
> "Also note that -M returns age in days, not seconds."

Funny coincidences :)
Adam314 was refering to this -M which is a perl operator returning the file age in days:
                                                           |

                                                           v

    my $found_ref = (sort {$b->[1] <=> $a->[1]} map {[$_, -M $_]} glob $glob_pattern)[0];

Open in new window

0
 
LVL 3

Accepted Solution

by:
WeiXi earned 500 total points
Comment Utility
> Looks it is not looking xml file now.

That it is due to the shell. Perl never sees the parameter you are trying to pass, because the shell evaluates the {eot,xml} as just eot.

To pass the parameters as intended, use quotes around the argument:
>  -m '*.{eot,xml}'
Maybe, depending on the type of shell you are using, double-quotes "*.{eot,xml}" could be prefered.
0
 

Author Closing Comment

by:tia_kamakshi
Comment Utility
Really Really Great. Many Many Thanks to you for your great help to me. It all works for me.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now