Q about Coro file::find

Aleksandar Lazic al-anyevent at none.at
Tue Jan 31 18:07:46 CET 2012


Hi,

please can anybody help me or point me to the right doc or way to solve 
the following problem.

I have a NFS-Share and I want to parallel the

du -sm /NFS-SHARE

I have used the File::Find like the program at the end.

My idea is similar like

http://cpansearch.perl.org/src/MLEHMANN/Coro-6.07/eg/prodcons3

###
in sub wanted call $work->put($i);
###

But how do I get the data back to the caller?
Does I need global vars?

I'am a little bit unsure what the right way is to combine the Coro with 
File::Find.

Maybe there are some better Solutions?

I'am very happy for some hints an tips ;-)

Thank you
Aleks

### Programm
#! /usr/bin/perl -w
     eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
         if 0; #$running_under_some_shell

#use strict;
use File::Find ();
use Data::Dumper;
use Time::HiRes qw(gettimeofday tv_interval);

use IO::File;
use Number::Format;
use POSIX;
use Log::Log4perl qw(:easy);

use vars qw/$num_format $df $iof $fs_to_check $out_hash/;

# Set the variable $File::Find::dont_use_nlink if you're using AFS,
# since AFS cheats.

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*name *dir *prune $out_hash/;
*name   = *File::Find::name;
*dir    = *File::Find::dir;
*prune  = *File::Find::prune;

my $OUT_DIR = '/home/user/logs';

$fs_to_check->{'Dir1'}='/mnt/Share/Dir1';
$fs_to_check->{'Dir2'}='/mnt/Share/Dir2';
$fs_to_check->{'Dir3'}='/mnt/Share/Dir3';
sub wanted;

Log::Log4perl->easy_init($DEBUG);

my $DATE =strftime("%Y-%m-%d",localtime());
$num_format = new Number::Format;
$iof = IO::File->new;
$iof->autoflush(1);

print "$OUT_DIR/NAS-Daten-".$DATE.".log\n";
if (!$iof->open(">$OUT_DIR/NAS-Daten-".$DATE.'.log')){
     die "Can't open file '':".$iof->error;
}


my $logger = get_logger();
$logger->info("start NAS-Checker");

$iof->print("NAS STATUS am $DATE\n");

my $tgesammt = [gettimeofday];
my $t0 = [gettimeofday];
# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '/mnt/Shere/Dir4');
$logger->debug("Elapsed :",strftime("%T",tv_interval ( $t0, 
[gettimeofday]),0,0,0,0,0));

print_output("Das Filesystem /mnt/Shere/Dir4 ist 
".$num_format->format_bytes($out_hash->{'bytes'},unit => 'M')." 
gross\n");

$out_hash = {};
$t0 = [gettimeofday];

File::Find::find({wanted => \&wanted}, $fs_to_check->{'Dir1'});

$logger->debug("Elapsed :",strftime("%T",tv_interval ( $t0, 
[gettimeofday]),0,0,0,0,0));

$fs_to_check->{'curr'}=$fs_to_check->{'Dir1'};
print "Das Filesystem $fs_to_check->{'curr'} ist 
",$num_format->format_bytes($out_hash->{'bytes'},unit => 'M')," 
gross\n";# >> $LOGFile
print_output("Das Filesystem $fs_to_check->{'curr'} 
".$num_format->format_bytes($out_hash->{'bytes'},unit => 'M')." 
gross\n");

print_output("Im Ordner Dir1 befinden sich ".$out_hash->{'Dir1'}." 
Unterverzeichnisse\n");

$out_hash = {};
$t0 = [gettimeofday];
#File::Find::find({wanted => \&wanted}, $fs_to_check->{'Dir2'}); #< 
~10k Directories in this dir
File::Find::find({wanted => \&wanted}, $fs_to_check->{'Dir3'});
$fs_to_check->{'curr'}=$fs_to_check->{'Dir3'};
print_output("Im Ordner Dir3 befinden sich ".$out_hash->{'user-dirs'}." 
Unterverzeichnisse\n");
print_output("Das Filesystem $fs_to_check->{'curr'} 
".$num_format->format_bytes($out_hash->{'bytes'},unit => 'M')." 
gross\n");

$logger->debug("Elapsed :",strftime("%T",tv_interval ( $t0, 
[gettimeofday]),0,0,0,0,0));

$logger->debug("Total Elapsed :",strftime("%T",tv_interval ( $tgesammt, 
[gettimeofday]),0,0,0,0,0));

print Dumper($out_hash),"\n";

$logger->info("end NAS-Checker");

exit;


sub wanted {

     my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
         $atime,$mtime,$ctime,$blksize,$blocks) = stat(_);

     $out_hash->{'num'}++;
     $out_hash->{'bytes'} += $size;
     $out_hash->{'files'}++ if -f _ ;
     $out_hash->{'UIDS'}->{$uid}++;

     if( ! ($out_hash->{'num'} % 5000)){
       $logger->debug("Currently counted ",
       sprintf("% 10s with % 14s 
",$out_hash->{'num'},$num_format->format_bytes($out_hash->{'bytes'},unit 
=> 'M')),
       "Running ",strftime("%T",tv_interval ( $t0, 
[gettimeofday]),0,0,0,0,0));
     }
     1;
}

sub print_output{
   my $oline = shift;

   print '-' x 41,"\n";
   $iof->print('-' x 41,"\n");

   print $oline;
   $iof->print($oline);
}# end sub print_output()

###



More information about the anyevent mailing list