On Mon, 21 Jan 2008 23:29:20 -0800 (PST) (e-mail address removed) wrote:
n> For monitoring files in directories i am using SGI::FAM module...
n> this module is monitoring perfectlt but taking more 90% CPU usage
n> while monitoring ... so any one can suggest me the better module to
n> monitor the files in directoties....here one more thing i want to
n> monitor more than a single directory at a time...
It could be a bug in your code. Can you show the code?
Use `top' or something similar to see what's actually using the CPU. Is
it the FAM daemon or your code? You could be in a busy loop...
Ted
Here is the code to monitor directories using Fam....
use strict;
use warnings;
use SGI::FAM;
my $fam;
my $terminate = 0;
my $path = '/data';
my %interested_dirs = ();
sub today_datetime
{
my($ss,$mi,$hh,$day,$mon,$year) = localtime();
$mon++;
$year+=1900;
my $now = "$year-$mon-$day:$hh:$mi:$ss";
return($now);
}
opendir(DIR,$path);
my @file_s = readdir(DIR);
foreach(@file_s)
{
if ($_ !~ /\.$/)
{
if(-d "$path/$_")
{
# print "$_ <-> $path/$_ \n ";
$interested_dirs{$_} = $path."/".$_ ;
}
}
}
closedir(DIR);
until ($terminate)
{
unless (defined($fam))
{
eval { $fam = new SGI::FAM; };
if ($@)
{
print "Failed to connect to FAMd - starting forcibly...\n\n\n";
system("/usr/local/sbin/famd");
next;
}
die "FAM Failed: $!\n" unless (defined($fam));
foreach (values %interested_dirs)
{
$fam->monitor($_);
#print "$_\n";
}
}
# check for FAM events
if ($fam->pending)
{
my $event;
eval { $event = $fam->next_event; };
if ($@)
{
print "FAM error: rebuilding cache...\n\n\n";
$fam = undef;
next;
}
#print "Received: " . $event->type . " for " . $event->filename .
"\n";
&process_fam_event($event);
}
}
sub process_fam_event
{
my $event = shift;
my $filename = $event->filename;
my $dir = $fam->which($event);
my $now = &today_datetime;
if($event->type eq 'create')
{
if($filename ne $dir)
{
my $subdir = "$dir/$filename";
while(`/usr/sbin/lsof +d $dir | grep "$filename"`)
{
next;
}
if(-d $subdir)
{
print "Creating $subdir \n";
eval { $fam->monitor($subdir); };
if($@)
{
print "Fam Error : Failed to monitor $subdir : $@ \n";
}
}
}
print "Createed--------NewFile : $filename , Dir : $dir $now\n";
}elsif($event->type eq 'delete')
{
if($filename ne $dir)
{
my $subdir = "$dir/$filename";
while(`/usr/sbin/lsof +d $dir | grep "$filename"`)
{
next;
}
if(-d $subdir)
{
print "Deleting $subdir \n";
eval { $fam->cancel($subdir); };
if($@)
{
print "Fam Error : Failed to monitor $subdir : $@ \n";
}
}
}
print "Delete : $filename , Dir : $dir $now\n";
}
}
Nagandla