473,566 Members | 2,776 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

154 New Member
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 3011
miller
1,089 Recognized Expert Top Contributor
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 New Member
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 Recognized Expert Contributor
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 New Member
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 Recognized Expert Top Contributor
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 seeFileInDirect ory() 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 New Member
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 Recognized Expert Top Contributor
perldoc time

- Miller
May 21 '07 #8
KevinADC
4,059 Recognized Expert Specialist
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 Recognized Expert Top Contributor
Thanks Kev,

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

- M
May 21 '07 #10

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

Similar topics

6
342
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
1257
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
4330
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 all mail coming into the e-mail box. This is using Interop.Outlook Example of code... Private Sub Form1_Load(ByVal sender As System.Object,...
4
2372
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 things independently, so the application can display certains things on one monitor (eg goods in the store) and certain other things on the other...
4
6141
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 The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned:
2
5934
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 dbname -evm db2detaildeadlock How do I clear old event and reduce disk usage?
6
6799
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 trigger's sql statment and cost, have other tools can get trigger's executing sql statment and cost ? our test case as follow: Env:
0
2620
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 trigger's sql statment and cost, have other tools can get trigger's executing sql statment and cost ? our test case as follow: Env:
1
1455
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 which name dir1 and transfers the file to another server So there are two types of files i want to check if are coming through and if they did not...
0
7666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7644
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
925
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.