473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Monitor a folder. no activity for 1 hour, send an email

154 100+
Hi I am trying to do a script to monitor a dir where files pass through approx every 5 mins but sometimes files may not come in for hours

So the purpose of the script is to monitor by if no files pass through in 1 hour send an email.

So was thinking to use the shell command to grab the time when a file comes in and write to a txtfile.
Now when the files keep coming it will overwrite the old time.
Now if no new files are coming in an hour i got the old time but i having trouble calculating the hour so to trigger out to send an email

this is what i came up with so far

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. #Purpose to monitor a folder
  3.  
  4. my $IN = "TIMEFILE1";
  5. my $IN2 = "TEST";
  6.  
  7. my $srcdir = "/home/tibco/javastuff/test";
  8. my $tfile = "/home/tibco/scripts/time.txt";
  9. my $tfile2 = "/home/tibco/scripts/time2.txt";
  10.  
  11. open (OUT ,">$tfile") || "Could not open file $!";
  12. open (FILE, ls -lt | awk -F" " '{print $8}');
  13. while (<FILE>) {
  14.     #print $_;
  15.     print OUT "$_";
  16.     #print $_;
  17. }
  18. close OUT;
  19. close FILE;
  20. ###### get system time
  21. open (SYSTEM ,">$tfile2") || "Could not open file $!";
  22. open (FILE2, date | awk -F" " '{print $4}');
  23. while (<FILE2>){
  24.     #print $_;
  25.     print OUT "$_";
  26.     #print $_;
  27. }
  28. close SYSTEM;
  29. close FILE2;
  30.  
  31. #END
May 18 '07 #1
11 2999
miller
1,089 Expert 1GB
Try something like the following. If the script is going to be running continuously then there is no reason to write information to a file. Just keep it in a variable. Also, it's not even really necessary to find the datetime value of a file, you just must mark the time close to when the file appears. Anyway, take a look at this psuedocode and see if it will work for your purposes.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4.  
  5. use constant WAIT => 10; # In Seconds
  6. use constant TIMEOUT => 1 * 3600; # In Seconds
  7.  
  8. my $lastSeen = time;
  9. my $warningMode = 0;
  10.  
  11. for (;;) {
  12.     if (seeFileInDirectory()) {
  13.         $lastSeen = time;
  14.         $warningMode = 0;
  15.  
  16.     } elsif (! $warningMode && time > $lastSeen + TIMEOUT) {
  17.         sendEmailToAdmin();
  18.         $warningMode = 1;
  19.     }
  20.  
  21.     sleep WAIT;
  22. }
  23.  
- Miller
May 18 '07 #2
jonathan184
154 100+
Hi Miller

The script that does the transfer does not actually run continuously. even thouight its close. BAsically the script that does the transfer is a shel script that has a cron job that runs every sec. I wanted to make a call to the perl script script to do the monitoring part, would i still be able to do it with your solution?
May 21 '07 #3
prn
254 Expert 100+
I'm not miller, but I think I can safely answer that miller's script is almost certainly a better solution than a cron job that fires off every second. If you're going to run it that frequently, you might as well just run it once and let it stay put. For one thing, if you restart the job once per second, you really can't rely on scheduling at that level of granularity. When I look at logs for various of my standing cron jobs I often see 1-5 seconds worth of variation. Beyond that, remember that every time you start the job, it will have to go out to disk and reload the script, as well as the perl interpreter/compiler from disk. If you load it once and let it loop and sleep, then you eliminate almost all of that excess disk activity. You also eliminate 100% of the disk writes from your script by keeping the times in a local variable rather than a file. Beyond that, miller's script is just so much simpler and more straightforward than yours.

Just start up miller's script (or a close equivalent) and let it run all the time. Drop it from your crontab and put it in your init scripts (or inittab). It's really a LOT better that way.

Best Regards,
Paul
May 21 '07 #4
jonathan184
154 100+
Thanks for the advice prn, i guess what you said did make sense.

Anyways I am trying to use Miller's script but i having some problems in getting it to work.

This is my code for the subroutines , the synax seems ok but i am not sure where i am going wrong here?
I am trying to get the print statemtns to show up but I have no luck in doing this.
If you guys could tell me where i am going wrong or what to try, i would appreciat eit.

Thanks for helping so far.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use File::Copy;
  4.  
  5. use constant WAIT => 10; # In Seconds
  6. use constant TIMEOUT => 1 * 3600; # In Seconds
  7. #### Dir to monitor ####
  8. my $srcdir = "d:\\monitor";
  9.  
  10. ### Email header/message
  11. my $emailheader = "No DWH files sent int he last hour";
  12. my $emailmsg = "No files were sent int he last hour.";
  13.  
  14.  
  15. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
  16. $time = sprintf("%02d%02d", $hour, $min);
  17.  
  18. #the time
  19. print $time;
  20.  
  21. my $lastSeen = $time;
  22. my $warningMode = 0;
  23.  
  24. for (;;) {
  25.     if (seeFileInDirectory()) {
  26.         my $lastSeen = sprintf("%02d%02d", $hour, $min);
  27.         my $warningMode = 0;
  28.     }
  29.     elsif (! $warningMode && $time > $lastSeen + TIMEOUT) {
  30.         sendEmailToAdmin();
  31.         $warningMode = 1;
  32.     }
  33.  
  34.     sleep WAIT;
  35. }
  36.  
  37. sub seeFileInDirectory {
  38.     opendir(DIR, "srcdir") || "Cannot open the dir $!";
  39.     my @files = grep{!/^\.+$/} readdir(DIR);
  40.     close(DIR);
  41.     print "@files";
  42.     close(DIR);
  43.  
  44.     foreach $files(@files) {
  45.         if ($files =~ /^DWH/) {
  46.             print "DWH file found";
  47.             return 0;
  48.         }
  49.         else {
  50.             return 1;
  51.             print "No files found";
  52.         }
  53.     }
  54. }
  55.  
  56. sub sendEmailToAdmin {
  57.     system "mail -s $emailheader test\@domain.com <<EOF $emailmsg EOF";
  58. }
  59.  
May 21 '07 #5
miller
1,089 Expert 1GB
Hello Jonathan,

Well, first of all don't change provided code for no reason. You don't need or want to format the time. And you've also localized (used my) on the variables within the if statement causing a bug. I'll remove that stuff for you now.

Concerning your own code. You don't need to use \\ in windows file systems, perl will also use / correctly. The seeFileInDirectory() sub should return TRUE for when there are files in the directory. You currently have this logic inverted. Printing after a return statement in a sub won't print. And finally your logic for no files was incorrect when it was included in the for loop.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2.  
  3. use File::Copy;
  4.  
  5. use strict;
  6.  
  7. use constant WAIT => 10; # In Seconds
  8. use constant TIMEOUT => 1 * 3600; # In Seconds
  9.  
  10. #### Dir to monitor ####
  11. my $srcdir = "d:/monitor";
  12.  
  13. ### Email header/message
  14. my $emailheader = "No DWH files sent in the last hour";
  15. my $emailmsg = "No files were sent in the last hour.";
  16.  
  17. my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
  18. $time = sprintf("%02d%02d", $hour, $min);
  19.  
  20. #the time
  21. print $time;
  22.  
  23. my $lastSeen = time;
  24. my $warningMode = 0;
  25.  
  26. for (;;) {
  27.     if (seeFileInDirectory()) {
  28.         $lastSeen = time;
  29.         $warningMode = 0;
  30.     }
  31.     elsif (! $warningMode && time > $lastSeen + TIMEOUT) {
  32.         sendEmailToAdmin();
  33.         $warningMode = 1;
  34.     }
  35.  
  36.     sleep WAIT;
  37. }
  38.  
  39. sub seeFileInDirectory {
  40.     opendir(DIR, $srcdir) or "Cant open $sirdir: $!";
  41.     my @files = grep{!/^\.+$/} readdir(DIR);
  42.     close(DIR);
  43.     print "@files";
  44.     close(DIR);
  45.  
  46.     foreach my $file (@files) {
  47.         if ($file =~ /^DWH/) {
  48.             print "DWH file found";
  49.             return 1;
  50.         }
  51.     }
  52.  
  53.     print "No files found";
  54.     return 0;
  55. }
  56.  
  57. sub sendEmailToAdmin {
  58.     system "mail -s $emailheader test\@domain.com <<EOF $emailmsg EOF";
  59. }
  60.  
- Miller
May 21 '07 #6
jonathan184
154 100+
Hi Miller

thanks for explaining it to me , i got what you showed me there. I just got one last question that is puzzling me , the value you are giving as time;

It is not a variable but how does it get the time there, that is why i changed it i was not sure how that would work, If you could tell me how that works or refer me to a doc about that i would be very greatful and thanks again for all your help. I have learned alot from the forums.

keep up the good work.

Thanks
Jonathan
May 21 '07 #7
miller
1,089 Expert 1GB
perldoc time

- Miller
May 21 '07 #8
KevinADC
4,059 Expert 2GB
this line:

opendir(DIR, "srcdir") || "Cannot open the dir $!";

should be:

opendir(DIR, "$srcdir") || "Cannot open the dir $!";
May 21 '07 #9
miller
1,089 Expert 1GB
Thanks Kev,

I've edited the code to take that into account.

- M
May 21 '07 #10
jonathan184
154 100+
thank you for your help guys.
May 22 '07 #11
KevinADC
4,059 Expert 2GB
Personally I don't see how the code will know if a file was new in the dir within the last hour. All it seems to do is check if there is a file with DWH in the filename.
May 22 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Kalyan | last post by:
Hi I need some idea, Is there any way to write vb code to monitor server activity and if server fails send out email to admin. thanks Kalyan
1
by: chris | last post by:
Is there a way that i can monitor the use of a file in terms of a timestamp? My goal is to monitor the activity of a file and when it goes beyond 10 minutes of non activity I will shut it down.
1
by: Roger | last post by:
I currently have an application that can monitor the Inbox of one mailbox running on a client using Outlook 2003. This is a standalone application that needs Outlook to be running in order to see...
4
by: John | last post by:
I'd like to write a programme that runs on a PC with two monitors. The application would be used in a shop, with one monitor for shop assistant and the other for the customer. The two must show...
4
by: Raj | last post by:
Can we create an event monitor for statements in a partitioned environment? CREATE EVENT MONITOR stmt_event FOR STATEMENTS WRITE TO FILE '/home/rajm/event' ON PARTITION 0 GLOBAL DB21034E ...
2
by: Hemant Shah | last post by:
Folks, I have several files in db2event/db2detaildeadlock directory and is using up considerable amount of disk space. When I run following command I see event for 2 years ago. db2evmon -db...
6
by: wugon.net | last post by:
Hi , Anyone know how to monitor db2 trigger activity ? We suffer some trigger issue , and we try to monitor trigger's behavior use event monitor and db2audit, but both tools can not get...
0
by: wugon.net | last post by:
Hi , Anyone know how to monitor db2 trigger activity ? We suffer some trigger issue today and we try to monitor trigger's behavior use event monitor and db2audit, but both tools can not get...
1
by: jonathan184 | last post by:
how to monitor and find out if files test1_* and test2_* files were sent in an hour and if not send an email This is on a unix system basically I got a cronjob that runs every sec polling a ftp dir...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.