Solved

Directory Monitoring

Posted on 2004-04-14
9
324 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
9 Comments
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
Hmmm, I guess the two time comparisons should be
if($now - $mtime <= $interval)
not
if($now - $mtime < $interval)
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

771 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now