Solved

Directory Monitoring

Posted on 2004-04-14
9
330 Views
Last Modified: 2010-04-22
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
Comment
Question by:enkrypter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10824405
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
 
LVL 7

Expert Comment

by:glassd
ID: 10824571
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
 
LVL 12

Accepted Solution

by:
stefan73 earned 250 total points
ID: 10824587
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
Industry Leaders: 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!

 
LVL 12

Expert Comment

by:stefan73
ID: 10824599
Hmmm, I guess the two time comparisons should be
if($now - $mtime <= $interval)
not
if($now - $mtime < $interval)
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10824735
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
 
LVL 2

Expert Comment

by:timbauer
ID: 10831031
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
 

Expert Comment

by:lempsi
ID: 10832607
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
 
LVL 12

Expert Comment

by:stefan73
ID: 10833155
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
 
LVL 2

Author Comment

by:enkrypter
ID: 10839010
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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

690 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question