Hello monks
I have a question about scoping. I have a logfile which is like this
The job started at Fri Oct 11 07:01:10 BST 2013
log4j:WARN No appenders could be found for logger
log4j:WARN Please initialize the log4j system properly.
Error checking credentials
The job completed at Fri Oct 11 08:01:10 BST 2013
From this logfile I need to perform various check at 9am. One is to check job started on correct date, if it doesn’t print that is hasn’t, check that job has completed on correct date, again if it hasn’t then print it hasn’t. This check is done 9am every day. The script is working but the problem I am having is I want to store the last known output value even outside the time window, so if during the time window the error message is"The job failed to complete", I need to store this value outside the time window until the next days check. As it stands and I understand why but my output is getting overwritten with the default output when it completes the time block
Do I need to write this to a logfile or can I use some special array/variable retainer?
Script as is.
#!/usr/bin/perl
use strict;
use warnings;
my $joblog = "/usr/logs/app.log";
my $M1 = "The job completed successfully";
my $M2 = "The job failed to start";
my $M3 = "The job failed to complete";
my $M4 = "The log does not exist";
my @tm = localtime;
my ($started,$completed);
chomp(my $jobdate=`date +"%a %b %d"`);
my @output;
if (($tm[2] == 9) && ($tm[1] == 00))
{
if (-e $joblog)
{
local $/;
open(my $fh, '<', $joblog);
my @content = <$fh>;
for(@content)
{
next unless (/^The job/);
if (m/^The job started at $jobdate/){
$started = 1;}
push (@output,"$M2") if ! $started;
print @output and exit if ! $started;
if (/^The job ended at $jobdate/){
$completed = 1;
push (@output,"$M1");
print @output and exit if $started;}
}
@output="$M3" if ! $completed;
print @output and exit if ! $completed;
}
else{print "$M4";}
}
else {
print @output if defined;}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.