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

Hello

I will start by saying I am spanky new to perl, having great fun, but plenty to learn, so feel free to point out where I can improve. I am working through a O'Reilly book currently, but and working on a simple task I need filled as my learning project.

What I am trying to do is make a simple check for Nagios/Icinga, I just want to check a Windows FTP servers directory to check that a back end system is collecting files. So I just want to see if anything in the directory is older than say 1 hour.

So here is what I have so far

#!/usr/bin/perl use strict; my $directory = "D:\\FTP-Accounts\\upload"; my $hours = 1; my $count = 0; print "\n"; opendir (DIR, $directory) or die $!; while (my $_ = readdir(DIR)) { #next unless (-f "$directory/$_"); my $name = $_; my $_ = -M; print "$name - Days = $_\n"; $_ = $_ * 24; if ($_ > $hours) { $count = $count + 1; print " * Triggersed * Hours = $_\n\n"; } } closedir(DIR); print "\nFile Count - $count -\n"; if ($count == 0) {print "OK:"} else {print "\nWarning: file is older t +han $hours hours\n"}; exit 0 if $count == 0; exit 1;

I have # out the command to remove .. and . as I don't seem to be getting file age correct, if I run the script I get this

D:\Perl-Scripts>dirlist.pl . - Days = 0.767615740740741 * Triggersed * Hours = 18.4227777777778 .. - Days = 11760.4493171296 * Triggersed * Hours = 282250.783611111 informant-std-16.zip - Days = VSE880LMLRP1.Zip - Days = File Count - 2 - Warning: filelist is older than 1 hours

my two .zip files are not getting a age, and I cant for trying work out why that is...

Also a second minor question, I have plonked in "my $count = 0;" but I feel this is wrong, but if I remove it I get an error...

D:\Perl-Scripts>dirlist.pl Global symbol "$count" requires explicit package name at D:\Perl-Scrip +ts\dirlist.pl line 19. Global symbol "$count" requires explicit package name at D:\Perl-Scrip +ts\dirlist.pl line 19. Global symbol "$count" requires explicit package name at D:\Perl-Scrip +ts\dirlist.pl line 25. Global symbol "$count" requires explicit package name at D:\Perl-Scrip +ts\dirlist.pl line 26. Global symbol "$count" requires explicit package name at D:\Perl-Scrip +ts\dirlist.pl line 27. Execution of D:\Perl-Scripts\dirlist.pl aborted due to compilation err +ors.

Also any tips on how to do this better would be great.

Thanks

Replies are listed 'Best First'.
Re: Search windows folder for old files...
by Corion (Patriarch) on Mar 13, 2012 at 11:35 UTC
    my $name = $_;

    You need to prepend the directory, beacuse readdir only returns the filename and not the complete path to it. Also see the documentation of readdir, which states your use case:

    ... If you're planning to filetest the return values out of a readdir, you'd better prepend the directory in question. ...

      Ahhh great, yer I see that now, so I guess my . and .. maybe are not in the directory I think they are, something to work on...

      Now to work our how to fix it!! :)

        Unsure if any use, but if somebody comes across this again, the fix was simple, I just had to add...

        chdir $directory or die $!;
Re: Search windows folder for old files...
by GrandFather (Saint) on Mar 14, 2012 at 05:39 UTC

    Unrelated to your question, but in the interests of improving your style you may find the following of interest:

    #!/usr/bin/perl use strict; my $directory = "D:\\FTP-Accounts\\upload"; my $kMaxHours = 1; my $count; print "\n"; opendir my ($scan), $directory or die $!; while (my $name = readdir($scan)) { #next unless (-f "$directory/$_"); my $age = -M $name; print "$name - Days = $age\n"; $age *= 24; if ($age > $kMaxHours) { ++$count; print " * Triggersed * Hours = $_\n\n"; } } closedir $scan; print "\nFile Count - $count -\n"; if (! $count) { print "OK:"; exit 0; } print "\nWarning: file is older than $kMaxHours hours\n"; exit 1;

    Points to note:

    • the k prefix in $kMaxHours as a convention to indicate a "constant"
    • Use a lexical handle for the directory scan handle ($scan)
    • use explicit variables instead of overuse of the default variable to make code easier to understand ($age, $name)
    • Use early exit to avoid needless nested code and repeated tests (<c>if (! $count) ...

    There may be other minor changes - can you spot them?

    True laziness is hard work