KnowledgeBase
PERL
Directory tree activity monitor
Directory tree activity monitor
Monitor directories for the movement and removal of files, outputting time spent.
#!/usr/bin/perl
#======================================================================================
# Dir_Comp_V04.pl by Mark Dowd of DowdTec Ltd - 20-Jan-2009
# Periodically monitors directories for the movement and removal of files, outputting
# time spent.
# ---------------------
# Use: Dir_Comp_V04.pl
# Parameters
# ==============
#
#======================================================================================
use strict;
#Set up HiRes time stamp stuff here
use Time::HiRes qw(gettimeofday);
use File::Find;
my %current; # Actual status
my %new; # Current run
# Returns the number of whole seconds from gettimeofday
sub seconds {
my @tod = gettimeofday;
return $tod[0];
}
# Called for each file reurned by File::Find
sub wanted {
if (/.+\.\w+/) {
$new{$_}{$File::Find::dir} = seconds; # Save time for later
}
}
# Mainline code, "do forever"
while (1) {
%new = (); # Empty "new" array
my $now = seconds; # Capture start run time
finddepth(\&wanted, @ARGV); # Load new values
# Capture "missing" files
for my $file (keys %current) { # For every "current" file
for my $dir (keys %{$current{$file}}) {
if ($new{$file}{$dir} == 0) { # New file missing?
my $elapsed = $now - $current{$file}{$dir}; # Capture elapsed time
print "$file,$dir,$elapsed\n"; # Print output
delete $current{$file}{$dir}; # Delete "missing" file
}
}
}
# Capture "new" files
for my $file (keys %new) {
for my $dir (keys %{$new{$file}}) {
if ($current{$file}{$dir} == 0) { # Current file missing?
$current{$file}{$dir} = $new{$file}{$dir}; # Capture time and save it
}
}
}
# Wait before going around again
sleep(0.5);
}