• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

Directory Monitoring

Greetings all,

I need some sort of linux toy, script (perl or shell) or C code, that can monitor a directory to see when new files arrive.  I need it to be able to send an email to a person or group of persons when the change is detected.  I also need this program to be able to monitor multiple directories and be able to have a different email list for each directory monitored if I so desire.  It doesn't have to be fancy, just so I can send an email to a person saying "You've got new Files", and then possible list the names of the new files in the email, but that is not a priority.  Emailing is though.

I tried to look for tools that can do this, but couldn't find any to fit my needs.  If anyone has suggestions, please suggest!  If something needs to be written in C I would kindly ask you make the source available via GPL.  After all, Linux was founded on it...  :)


I considder myself an intermediate Linux Admin.  I know shell and perl fairly well, but don't claim to know it all.  I am primarily a Slackware user.  I have GUI clutter.  CLI all the way!  My servers don't even have KDE or X installed.  This programming help is going to be used to notify people when files are available for download on an FTP site.  So, it must be flexible to be able to monitor and notify many different directories and people.

Thanks in advance.
0
enkrypter
Asked:
enkrypter
1 Solution
 
stefan73Commented:
Hi enkrypter,
Check the modification time of the directory. When a new file was added, it is updated. This can be used for inexpensive, regular monitoring of several directories (use stat() in C or Perl).

Once you detect a change, you can then user opendir/readdir/closedir to get all the gory details.

Cheers,
Stefan
0
 
glassdCommented:
Run a cron, possibly every ten minutes or so, which touches a date stamp file after it completes.

The cron runs a find. Find every file in the desired structure which is less than a day old (find -mtime -1).
Compare this with the datestamp file, eg in ksh):

if [[ $Filename -nt $datestampfile ]]

If the file found is newer that the datestamp file (-nt) then get the ownership from the file and email the user.
Using perl you would probably create a list of files from the find command (I think there are Find modules for perl). You could then generate a hash, the key being the files owner and the value being a list fo files owned by them. Once complete you could email each set of files to the correct owner.

At the end, touch the datestamp file to reset its time. You might have to be careful incase files arrive during execution of the script. May be wiser to set the time on the file to the starting time of the current execution of the script.

Just a suggestion.
0
 
stefan73Commented:
enkrypter,
Try this Perl script:

#!/usr/bin/env perl
use strict;

my @mondirs=qw( one two three );

my $interval=5;

# Main loop
for(;;){
        my $dir;
        my $now = time();

        for $dir (@mondirs){
                my $mtime = (stat($dir))[9] or die "Can't find $dir: $!";

                if($now - $mtime < $interval){
                        print "New files have arrived in '$dir':\n";    
                        opendir (DIR,$dir) or die "Can't open $dir: $!";
                        my @dir_entries=readdir DIR;
                        closedir DIR;
                        my $entry;
                        for $entry(@dir_entries){
                                $mtime=(stat("$dir/$entry"))[9] or die "Can't get file info for '$entry': $!";
                                if($now - $mtime < $interval){
                                        print "$entry\n";
                                }
                        }
                }
        }
        sleep($interval);
}


Stefan
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
stefan73Commented:
Hmmm, I guess the two time comparisons should be
if($now - $mtime <= $interval)
not
if($now - $mtime < $interval)
0
 
stefan73Commented:
Be aware that the file system date stamp granularity of one second is very coarse. In case you have a file change in the very second you do a monitoring loop, you'll get a duplicate alert. But I suppose this is better than having changes slip through unnoticed.

Stefan
0
 
timbauerCommented:
I am curious, is there a way to do this without spinning in a loop (albeit with sleep calls)?
Therefore, is there support for some sort of notification from kernel?

0
 
lempsiCommented:
proposel to make a small shell-script

The main idea is to check (a grep) over file-permissions or better file owner, then change owners or permissions and send a mail with the mail command.

So thing like that: (only a draft)

ll **/* | grep "newupload" >newfiles

if newfiles then
  mail  xy@y.com newfiles

chown "newupload" -> "notedupload"


... this should lead to a simple shell-script solution.

0
 
stefan73Commented:
timbauer,
> notification from kernel?
AFAIK, not with Linux. But I'm pretty sure Solaris has this. I don't know any details, though.

Regardless if this is handled by Kernel or "by hand", the problem of timing granularity remains. Maybe the only workaround is to set back the file time by one second in case of it being on the "boundary" second. But this is evil :-)))

Stefan
0
 
enkrypterAuthor Commented:
Guys,

I thank you for your suggestions.  I will not have time to try this immediately, but I will and you have all given me a good staring point.  What I had in mind is a lot more elaborate.  I may end up coding it in C.  If I do I will share it.  I will award poinst as soon as I try this out.  Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now