CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Script Help

 



liketheperl2009
Novice

Dec 4, 2009, 8:41 PM

Post #1 of 23 (3225 views)
Script Help Can't Post

Hi can somebody offer some assistance with this following script?

#!/usr/bin/perl

use warnings;
use strict;

my($day, $month, $year) = (localtime)[3,4,5];
$month = sprintf '%02d', $month+1;
$day = sprintf '%02d', $day;
my $currentdate = print $year+1900, $month, $day, ".log";

open(DATA, "<$currentdate");

while(<DATA> =~ m/MemoryException/g){

##MemoryException reaches 5 times send email warning

}

close (DATA);

What i would like to know is this:
1)Can i use a variable to specify a file name. Reason i'm doing this is cause i have a directories with filenames that rotate every night so the format would look like this 20091204.log

For some reason though if i run it i get the following ~
readline() on closed filehandle DATA at date.pl line 14.
20091204.log1 and if i specify \n i get
20091204.log
1

where the heck does that 1 come from? And i would also like to know how if the expression is meet say 5 times to send out an email alert.

Thanks,


shawnhcorey
Enthusiast


Dec 5, 2009, 6:26 AM

Post #2 of 23 (3218 views)
Re: [liketheperl2009] Script Help [In reply to] Can't Post

Cleaning up the code a little:

Code
#!/usr/bin/perl 

use warnings;
use strict;

use POSIX;

my $currentdate = strftime( '%Y%m%d.log', localtime );

open my $data_fh, '<', $currentdate or die "could not open $currentdate: $!\n";

while( <$data_fh> ){
chomp;
if( m{ Memory Exception }msx ){

##MemoryException reaches 5 times send email warning

}
}

close $data_fh or die "could not close $currentdate: $!\n";



Quote
1)Can i use a variable to specify a file name. Reason i'm doing this is cause i have a directories with filenames that rotate every night so the format would look like this 20091204.log


The file's name is in $currentdate


Quote
where the heck does that 1 come from?


It comes from the print statement in my $currentdate = print $year+1900, $month, $day, ".log";

It is better to use the POSIX strftime() function to format dates. A list of its format codes can be found here: http://www.opengroup.org/onlinepubs/007908799/xsh/strftime.html

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


liketheperl2009
Novice

Dec 5, 2009, 6:33 AM

Post #3 of 23 (3217 views)
Re: [shawnhcorey] Script Help [In reply to] Can't Post

Thanks so much!! Shawnhcorey :) ~ I will be giving this a try shortly.

Regards,

Al ~


liketheperl2009
Novice

Dec 5, 2009, 7:05 AM

Post #4 of 23 (3214 views)
Re: [liketheperl2009] Script Help [In reply to] Can't Post

Shawnhcorey,

I regret to say it didn't work :( ~ Did i mention i'm trying this out on Windows version of perl.


shawnhcorey
Enthusiast


Dec 5, 2009, 7:07 AM

Post #5 of 23 (3213 views)
Re: [liketheperl2009] Script Help [In reply to] Can't Post


In Reply To
Shawnhcorey,

I regret to say it didn't work :( ~ Did i mention i'm trying this out on Windows version of perl.


What didn't work? What was the error message?

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


liketheperl2009
Novice

Dec 5, 2009, 7:46 AM

Post #6 of 23 (3210 views)
Re: [shawnhcorey] Script Help [In reply to] Can't Post

ShawnhCorey,

When i ran the script it just didn't do anything. I did add a test condition > print "$data_fh" and it did not seem to return the current file 20091205.log. What am i doing wrong here? Like i said previously these logs will rotate daily and i need look inside the file for an occurence of memory exceptions and then send out an email warning when it reaches a threshold. Thanks for all your help in the meantime :)


shawnhcorey
Enthusiast


Dec 5, 2009, 7:55 AM

Post #7 of 23 (3209 views)
Re: [liketheperl2009] Script Help [In reply to] Can't Post

Oops, change the if statement to:

Code
if( m{ MemoryException }msx ){


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


liketheperl2009
Novice

Dec 5, 2009, 8:01 AM

Post #8 of 23 (3207 views)
Re: [shawnhcorey] Script Help [In reply to] Can't Post

Shawnhcorey,

I didn't change much of your code at all. I did what you said and put in a test condition. Here's what it looks like now.

#!/usr/bin/perl

use warnings;
use strict;

use POSIX;

my $currentdate = strftime( '%Y%m%d.log', localtime );

open my $data_fh, '<', $currentdate or die "could not open $currentdate: $!\n";

print $data_fh

while( <$data_fh> ){
chomp;
if( m{ MemoryException }msx ){

##MemoryException reaches 5 times send email warning

print $data_fh

}
}

close $data_fh or die "could not close $currentdate: $!\n";

still running it ~ doesn't list the file is present at all. Is there any way i could run it with what i had previously and somehow strip off the one. I followed that link you showed and it seems to refer to unix. I'm using windows.. :(


shawnhcorey
Enthusiast


Dec 5, 2009, 8:40 AM

Post #9 of 23 (3203 views)
Re: [liketheperl2009] Script Help [In reply to] Can't Post

The variable $data_fh contains the file handle. Printing it out won't tell you much unless you want to learn the internal workings of Perl. Print out the file's name:

Code
my $currentdate = strftime( '%Y%m%d.log', localtime ); 
print "*** the file is $currentdate\n";


The link I gave is for the C function strftime(). Since the Perl sub calls the C function, it uses the same format codes. Just ignore everything but them. The codes are things like %Y

__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".


liketheperl2009
Novice

Dec 5, 2009, 8:59 AM

Post #10 of 23 (3201 views)
Re: [shawnhcorey] Script Help [In reply to] Can't Post

I added the line just to test. Still no dice. It will not even display the file :(

Directory of C:\Perl\testdir

12/05/2009 09:41 AM <DIR> .
12/05/2009 09:41 AM <DIR> ..
12/05/2009 09:42 AM 88 20091205.log
12/05/2009 09:42 AM 418 velmon.pl
2 File(s) 506 bytes
2 Dir(s) 33,643,188,224 bytes free

C:\Perl\testdir>perl velmon.pl

C:\Perl\testdir>

I do appreciate all your help....


FishMonger
Veteran / Moderator

Dec 5, 2009, 9:14 AM

Post #11 of 23 (3198 views)
Re: [liketheperl2009] Script Help [In reply to] Can't Post

Please use the 'code' and 'quote' tags when posing. It helps us to help you.

C:\TEMP>type velmon.pl

Code
#!/usr/bin/perl 

use warnings;
use strict;

use POSIX;

my $currentdate = strftime( '%Y%m%d.log', localtime );
print "*** the file is $currentdate\n";

open my $data_fh, '<', $currentdate or die "could not open $currentdate: $!\n";

print $data_fh

while( <$data_fh> ){
chomp;
if( m{ MemoryException }msx ){

##MemoryException reaches 5 times send email warning

print $data_fh

}
}

close $data_fh or die "could not close $currentdate: $!\n";


Quote
C:\TEMP>perl -c velmon.pl
syntax error at velmon.pl line 15, near "){"
syntax error at velmon.pl line 24, near "}"
velmon.pl had compilation errors.



FishMonger
Veteran / Moderator

Dec 5, 2009, 9:16 AM

Post #12 of 23 (3197 views)
Re: [FishMonger] Script Help [In reply to] Can't Post

In case you didn't catch it, I was pointing out that 2 of your print statements were missing the semicolon.

Please post your code in the same manor as I did.


liketheperl2009
Novice

Dec 5, 2009, 9:37 AM

Post #13 of 23 (3194 views)
Re: [FishMonger] Script Help [In reply to] Can't Post

Thanks Shawnh and Fishmonger. I did copy the script into another text file and ran it. But when i do i get this output
C:\Perl\testdir>perl test2.pl
20091205.log
20091205.log
20091205.log
20091205.log
20091205.log

C:\Perl\testdir>perl -c test2.pl
test2.pl syntax OK

it looks like with a print statement i added it just repeats the file name a couple of diff times. It doesn't seem to match if say "MemoryException" is in the file and then say send out email. I do appreciate all your time and feedback. :)


FishMonger
Veteran / Moderator

Dec 5, 2009, 10:28 AM

Post #14 of 23 (3189 views)
Re: [liketheperl2009] Script Help [In reply to] Can't Post

Can you post the complete script and logfile?


liketheperl2009
Novice

Dec 5, 2009, 11:02 AM

Post #15 of 23 (3187 views)
Re: [FishMonger] Script Help [In reply to] Can't Post

Fishmonger ~

There are just slight changes i did for testing purposes. Here is the script at hand.

#!/usr/bin/perl

use warnings;
use strict;

use POSIX;

my $currentdate = strftime( '%Y%m%d.log', localtime );

open my $data_fh, '<', $currentdate or die "could not open $currentdate: $!\n";

while( <$data_fh> ){
chomp;

if( m{ MemoryException }msx ){


##if ($currentdate =~ m/MemoryException/){

print "match\n";

##MemoryException reaches 5 times send email warning
} else {
print "no match\n";

}
}

close $data_fh or die "could not close $currentdate: $!\n";

output when running it

C:\Perl\testdir>perl test3.pl
match
match
match
match
match

What i have is a directory which rotates it's log everyday in this format 20091205.log ~ i want to be able to take this script and run it in a batch job and while the script runs it will search for every occurence of a memoryexception. If the memoryexception is listed at least say 5 times or more to send out an email alert. I'm very new to perl and your help is greatly appreciated.

:)


FishMonger
Veteran / Moderator

Dec 5, 2009, 12:03 PM

Post #16 of 23 (3179 views)
Re: [liketheperl2009] Script Help [In reply to] Can't Post

Do you want to send the email as soon as you reach 5 matches and then stop processing the file, or do you want to complete the processing of the file and send the email with the total count of matches?

There are a number of approaches for sending email. In most cases, my preference is to use the MIME::Lite module.
http://search.cpan.org/~rjbs/MIME-Lite-3.027/lib/MIME/Lite.pm

Another option would be the Net::SMTP module.
http://search.cpan.org/~gbarr/libnet-1.22/Net/SMTP.pm


liketheperl2009
Novice

Dec 5, 2009, 12:18 PM

Post #17 of 23 (3177 views)
Re: [FishMonger] Script Help [In reply to] Can't Post

Fishmonger,

Ideally if the memoryexception occurs multiple times in the file that would normally mean some memory leak is occuring. When this happens if we don't react quick enough our application crashes. Hey again thanks for your help.


FishMonger
Veteran / Moderator

Dec 5, 2009, 12:45 PM

Post #18 of 23 (3170 views)
Re: [liketheperl2009] Script Help [In reply to] Can't Post


Code
#!/usr/bin/perl 

use warnings;
use strict;
use POSIX qw(strftime);
use MIME::Lite;

my $currentdate = strftime( '%Y%m%d.log', localtime );
my $max_exceptions = 5;
my $count;

open my $data_fh, '<', $currentdate
or die "could not open $currentdate: $!\n";

while( <$data_fh> ){
chomp;

if( m{ MemoryException }msx ){

if ( ++$count == $max_exceptions ) {

my $msg = "$currentdate has at least $count memory exceptions";
my $email = MIME::Lite->new(
From => 'me@myhost.com',
To => 'you@yourhost.com',
Cc => 'some@other.com, some@more.com',
Subject => 'Memory Exceptions',
Data => $msg
);
$email->send;

close $data_fh;
exit;
}
}
}



liketheperl2009
Novice

Dec 5, 2009, 1:38 PM

Post #19 of 23 (3165 views)
Re: [FishMonger] Script Help [In reply to] Can't Post

FishMonger,

You are the man. I just need to find the MIME:LITE package for windows and i'm sure i'll be good to go. Thanks for all the time and patience.

:)


liketheperl2009
Novice

Dec 5, 2009, 2:14 PM

Post #20 of 23 (3162 views)
Re: [liketheperl2009] Script Help [In reply to] Can't Post

FishMonger ~ Once last question. I updated the module with ppm and now i run this and get this:

SMTP Failed to connect to mail server: Bad file descriptor
at velmon.pl line 30

Line 30 is $email->send; -- what's missing now :)


FishMonger
Veteran / Moderator

Dec 5, 2009, 4:49 PM

Post #21 of 23 (3159 views)
Re: [liketheperl2009] Script Help [In reply to] Can't Post

Sounds like you're running this on Windows.

MIME::Lite assumes that you're running sendmail. If you're not, such as when run on Windows, you'll need to specify your SMPT server.
http://search.cpan.org/~rjbs/MIME-Lite-3.027/lib/MIME/Lite.pm#Change_how_messages_are_sent


liketheperl2009
Novice

Dec 6, 2009, 8:34 AM

Post #22 of 23 (3149 views)
Re: [FishMonger] Script Help [In reply to] Can't Post

Yes it is windows :( ~ Would be so much easier with sendmail. Thanks for all the assistance. I'll test it further when i get a chance.

Thanks,

:)


liketheperl2009
Novice

Dec 11, 2009, 10:53 AM

Post #23 of 23 (3112 views)
Re: [liketheperl2009] Script Help [In reply to] Can't Post

All,

I've to tried incorporate a smtp string that i found but it's still not working. Any idea why this is not working??

#!/usr/bin/perl

use warnings;
use strict;
use POSIX qw(strftime);
use Net::SMTP;

my $currentdate = strftime( '%Y%m%d.log', localtime );
my $max_exceptions = 5;
my $count;

open my $data_fh, '<', $currentdate
or die "could not open $currentdate: $!\n";

while( <$data_fh> ){
chomp;

if( m{ MemoryException }msx ){

if ( ++$count == $max_exceptions ) {

my $msg = "$currentdate has at least $count memory exceptions";

my $server = 'smtpserver.com';

$conn = Net::SMTP->new($server);
die "Can't connect! try a different server!" unless $conn;

my $from = ("user\@something.com");
my $to = ("user\@something.com");
my $subject =("$msg");


$conn->mail($from);
$conn->to($to);
$conn->data();
$conn->datasend("To: $to\n");
$conn->datasend("Subject: $subject\n");
$conn->datasend("$msg");
$conn->dataend();
close $data_fh;
exit;
}
}
}

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives