LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-05-2006, 08:25 AM   #1
Rustylinux
Member
 
Registered: Mar 2006
Posts: 177

Rep: Reputation: 30
Need help finding a program


Hello all,

What i'm looking for is a program or script that is able to monitor a folder and email a user whenever a file is written to it or updated in it. I have been looking around google but can't seem to find anything. Also if it could do this in any folder weather its a folder on a windows file server or a folder on a linux file server.
 
Old 12-05-2006, 08:49 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Have a look at gamin.
 
Old 12-05-2006, 08:58 AM   #3
Rustylinux
Member
 
Registered: Mar 2006
Posts: 177

Original Poster
Rep: Reputation: 30
This looks like it will work however would it be possible to program a script so it would only email the user if the folder hasn't been updated in 15min or so ? and if so how would you program such a script as my scripting is rusty.

*Edit*

Actually i'm unsure if this will work. Here is what i'm trying to accomplish:

The folder I have to check is on a windows file server. What happens is we have a temperature monitoring program that dumps data every 2 min or so into a folder. However sometimes it stops and when this stops its a problem. So i would like it to email a user if the folder hasn't been updated in 15 min or so. Not sure how I would accomplish this. I have both AIX, and Linux systems in the network if that helps any for finding a program to accomplish this.

Last edited by Rustylinux; 12-05-2006 at 09:05 AM.
 
Old 12-05-2006, 09:04 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
gamin is a library. There are python binding, so if I were to do it, I'd probably write a python program. I'll write if for you if you pay me lots of money
 
Old 12-05-2006, 09:15 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Actually scratch that, there's a command-line tool, dnotify, which looks like it would be more helpful.
 
Old 12-05-2006, 09:29 AM   #6
Rustylinux
Member
 
Registered: Mar 2006
Posts: 177

Original Poster
Rep: Reputation: 30
Will dnotify continue to run after each update? or does it have to be put into a cron to schedule itself to continue to run?
 
Old 12-05-2006, 09:59 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by Rustylinux
The folder I have to check is on a windows file server. What happens is we have a temperature monitoring program that dumps data every 2 min or so into a folder. However sometimes it stops and when this stops its a problem. So i would like it to email a user if the folder hasn't been updated in 15 min or so. Not sure how I would accomplish this. I have both AIX, and Linux systems in the network if that helps any for finding a program to accomplish this.
Read this after posting python and dnotify posts. I don't know if they'll work on a windows shared filesystem.

If you only want too check every few minutes, polling shouldn't be a problem.

Some questions to find out if it'll be an easy task:
  1. Is the filesystem mounted on your Linux machine?
  2. When you modify a file in the specified directory, does the directory timestamp get updated. This is how it works in Linux, but on a windows filesystem maybe not...
  3. Are the filesystem timestamps synched with your Linux machine's clock? What about daylight saving switches - do they happen at the same time on the windows filesystem and the Linux machine...
  4. Does mail delivery work from your Linux machine? You can try it with
    Code:
    echo "test message" | mailx -s "test subject" test@address.here
    You could try also mail instead of mailx.

Assuming all these things are OK, it should be a pretty trivial matter to write a program to do this. As long the answer to point number 2 is yes, and you're not wanting to check inside sub-directories, you only need to look at the difference between the timestamp on the directory and the system time. I'd use perl just cos I'm most familiar with it:
Code:
#!/usr/bin/perl -w

use strict;

my $notify_address       = 'address@to.notify';
my $directory_to_monitor = '/home/matthew/tmp';

# poll every 30 seconds
my $poll_sleep_time      = 30;


# after notification, don't send another notifcation for another
# this many seconds.  This is to prevent mail boxes getting totally
# crammed with warnings.
# set to 20 minutes
my $post_notify_time     = ( 60 * 20 );

# notify me if the directory hasn't been modified in this many seconds
# we'll notify if the time is > 10 minutes
my $notify_age           = ( 60 * 10 );

while(1) {
    my $dir_mod_time = (stat($directory_to_monitor))[9];
    my $update_ago   = time() - $dir_mod_time;

    if ( $update_ago > $notify_age ) {
        send_notification();
        print "sent notification\n";
        sleep $post_notify_time;
    }
    else {
        print "no notification necessary (last update was $update_ago seconds ago)\n";
    }

    sleep $poll_sleep_time;
}

sub send_notification {
    system("echo '$directory_to_monitor has not seen a update for a while...' |mailx -s 'warning' $notify_address");
}
Of course you can comment out un-wanted print statements.

Now there's two things to note about this sort of approach:
  1. You email infrastructure becomes part of a production monitoring system, so the admin of the email system may not like the extra pressure (if email goes down, or is slow and the notification doesn't happen because of that, and the brown stuff hits the fan, they will likely take a beating).
  2. You need to ensure the system which is running the script is reliable and supported and that the script keeps running (and is re-launched if it stops working).
 
Old 12-05-2006, 10:19 AM   #8
Rustylinux
Member
 
Registered: Mar 2006
Posts: 177

Original Poster
Rep: Reputation: 30
Cool thanks, i'll give this a whirl see how it works.
 
Old 12-05-2006, 10:31 AM   #9
Rustylinux
Member
 
Registered: Mar 2006
Posts: 177

Original Poster
Rep: Reputation: 30
Thanks for all the help matthewg42 my scripting is very VERY rusty and it would of taken me days to fumble through to get something along those lines. However while testing this or putting it in place i was wondering how to alter the code so it only checks once or twice a day and checks the time stamps to see if they match the current date. If they don't match the current date send an email then.

This would reduce the network traffic as there is some give on how long the information can be outdated before serious problems occur.
 
Old 12-05-2006, 10:38 AM   #10
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You can use modify the values of the $poll_sleep_time and $notify_age. The values are in seconds.

I woulnd't worry too much about the network traffic. Polling once every few minutes is not going to add any significant load. the stat call is very light.

The only reason I can think of that you'd want to reduce the traffic is if you're on some connection which only connects when there is traffic, and you want the connection down most of the time.
 
Old 12-05-2006, 02:50 PM   #11
Rustylinux
Member
 
Registered: Mar 2006
Posts: 177

Original Poster
Rep: Reputation: 30
This may be a stupid question but would I have to schedule the cron to run the script over and over ? or would it only have to run it once a day ?
 
Old 12-05-2006, 03:21 PM   #12
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Not with this script - run it once, and it should keep running. Of course if it gets killed for any reason you'll have to re-start it.

You could modify the script to not do the repeating (while loop), and then call that modified script from cron according to some schedule you want. This may be a better approach since cron is probably more robust than expecting this script to keep running. Cron will be brought up automatically after some reboot, whereas you'd have to put this script in the init system somehow if you want it to start automatically after reboots.
 
Old 12-05-2006, 04:20 PM   #13
Rustylinux
Member
 
Registered: Mar 2006
Posts: 177

Original Poster
Rep: Reputation: 30
Hey thanks again. To take out the while i'd just take out the

While {

and }

i'd leave the sleep $poll in right?

or should that go with the loop as well.
 
Old 12-05-2006, 04:43 PM   #14
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Take out the "while(1) {" and the closing } in that block. You might as well remove the sleep commands too, and the definitions of the variables for the sleep timings as well. So it becomes like this (comments removed for brevity):

Code:
#!/usr/bin/perl -w

use strict;

my $notify_address       = 'address@to.notify';
my $directory_to_monitor = '/home/matthew/tmp';
my $notify_age           = ( 60 * 10 );

my $dir_mod_time = (stat($directory_to_monitor))[9];
my $update_ago   = time() - $dir_mod_time;

if ( $update_ago > $notify_age ) {
    send_notification();
    print "sent notification\n";
}
else {
    print "no notification necessary (last update was $update_ago seconds ago)\n";
}

sub send_notification {
    system("echo '$directory_to_monitor has not seen a update for a while...' |mailx -s 'warning' $notify_address");
}
Then add a cron entry something like this (to check once per hour at 5 minutes past the hour):
Code:
5 * * * * /path/to/script
Please take time to learn how it is working. It sounds very much like you intend to use this in some business situation. When it breaks because something in your setup changes, you'll need to know how it wotks in order to fix it.
 
Old 12-05-2006, 04:59 PM   #15
Rustylinux
Member
 
Registered: Mar 2006
Posts: 177

Original Poster
Rep: Reputation: 30
Thanks for you input. My perl syntax is really bad I will have to crack open the manual and get a strong understanding of it once again, however the overall theory is mostly the same as i coded in vb, java, and C++ before, and cron setups are pretty simple. Thanks again for all your help you saved me a ton of time and troubleshooting.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding out the version of a program purefan Linux - Software 3 02-25-2005 05:37 PM
Finding installed program binarysoul Mandriva 5 01-31-2005 09:42 PM
finding program logfiles atom Linux - Software 3 08-10-2004 04:33 PM
need help finding where this installed program went nick2003 Linux - Software 6 11-16-2003 01:04 AM
finding pid with program name durden2.0 Linux - Newbie 1 08-04-2003 08:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration