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 - #!/usr/bin/perl
-
#Purpose to monitor a folder
-
-
my $IN = "TIMEFILE1";
-
my $IN2 = "TEST";
-
-
my $srcdir = "/home/tibco/javastuff/test";
-
my $tfile = "/home/tibco/scripts/time.txt";
-
my $tfile2 = "/home/tibco/scripts/time2.txt";
-
-
open (OUT ,">$tfile") || "Could not open file $!";
-
open (FILE, ls -lt | awk -F" " '{print $8}');
-
while (<FILE>) {
-
#print $_;
-
print OUT "$_";
-
#print $_;
-
}
-
close OUT;
-
close FILE;
-
###### get system time
-
open (SYSTEM ,">$tfile2") || "Could not open file $!";
-
open (FILE2, date | awk -F" " '{print $4}');
-
while (<FILE2>){
-
#print $_;
-
print OUT "$_";
-
#print $_;
-
}
-
close SYSTEM;
-
close FILE2;
-
-
#END
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. -
#!/usr/bin/perl
-
-
use strict;
-
-
use constant WAIT => 10; # In Seconds
-
use constant TIMEOUT => 1 * 3600; # In Seconds
-
-
my $lastSeen = time;
-
my $warningMode = 0;
-
-
for (;;) {
-
if (seeFileInDirectory()) {
-
$lastSeen = time;
-
$warningMode = 0;
-
-
} elsif (! $warningMode && time > $lastSeen + TIMEOUT) {
-
sendEmailToAdmin();
-
$warningMode = 1;
-
}
-
-
sleep WAIT;
-
}
-
- Miller
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?
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
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. -
#!/usr/bin/perl
-
-
use File::Copy;
-
-
use constant WAIT => 10; # In Seconds
-
use constant TIMEOUT => 1 * 3600; # In Seconds
-
#### Dir to monitor ####
-
my $srcdir = "d:\\monitor";
-
-
### Email header/message
-
my $emailheader = "No DWH files sent int he last hour";
-
my $emailmsg = "No files were sent int he last hour.";
-
-
-
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
-
$time = sprintf("%02d%02d", $hour, $min);
-
-
#the time
-
print $time;
-
-
my $lastSeen = $time;
-
my $warningMode = 0;
-
-
for (;;) {
-
if (seeFileInDirectory()) {
-
my $lastSeen = sprintf("%02d%02d", $hour, $min);
-
my $warningMode = 0;
-
}
-
elsif (! $warningMode && $time > $lastSeen + TIMEOUT) {
-
sendEmailToAdmin();
-
$warningMode = 1;
-
}
-
-
sleep WAIT;
-
}
-
-
sub seeFileInDirectory {
-
opendir(DIR, "srcdir") || "Cannot open the dir $!";
-
my @files = grep{!/^\.+$/} readdir(DIR);
-
close(DIR);
-
print "@files";
-
close(DIR);
-
-
foreach $files(@files) {
-
if ($files =~ /^DWH/) {
-
print "DWH file found";
-
return 0;
-
}
-
else {
-
return 1;
-
print "No files found";
-
}
-
}
-
}
-
-
sub sendEmailToAdmin {
-
system "mail -s $emailheader test\@domain.com <<EOF $emailmsg EOF";
-
}
-
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. -
#!/usr/bin/perl
-
-
use File::Copy;
-
-
use strict;
-
-
use constant WAIT => 10; # In Seconds
-
use constant TIMEOUT => 1 * 3600; # In Seconds
-
-
#### Dir to monitor ####
-
my $srcdir = "d:/monitor";
-
-
### Email header/message
-
my $emailheader = "No DWH files sent in the last hour";
-
my $emailmsg = "No files were sent in the last hour.";
-
-
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
-
$time = sprintf("%02d%02d", $hour, $min);
-
-
#the time
-
print $time;
-
-
my $lastSeen = time;
-
my $warningMode = 0;
-
-
for (;;) {
-
if (seeFileInDirectory()) {
-
$lastSeen = time;
-
$warningMode = 0;
-
}
-
elsif (! $warningMode && time > $lastSeen + TIMEOUT) {
-
sendEmailToAdmin();
-
$warningMode = 1;
-
}
-
-
sleep WAIT;
-
}
-
-
sub seeFileInDirectory {
-
opendir(DIR, $srcdir) or "Cant open $sirdir: $!";
-
my @files = grep{!/^\.+$/} readdir(DIR);
-
close(DIR);
-
print "@files";
-
close(DIR);
-
-
foreach my $file (@files) {
-
if ($file =~ /^DWH/) {
-
print "DWH file found";
-
return 1;
-
}
-
}
-
-
print "No files found";
-
return 0;
-
}
-
-
sub sendEmailToAdmin {
-
system "mail -s $emailheader test\@domain.com <<EOF $emailmsg EOF";
-
}
-
- Miller
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
miller 1,089
Recognized Expert Top Contributor KevinADC 4,059
Recognized Expert Specialist
this line:
opendir(DIR, "srcdir") || "Cannot open the dir $!";
should be:
opendir(DIR, "$srcdir") || "Cannot open the dir $!";
miller 1,089
Recognized Expert Top Contributor
Thanks Kev,
I've edited the code to take that into account.
- M
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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.
|
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,...
|
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...
|
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:
| |
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?
|
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:
|
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:
|
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...
|
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...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| | |