| 1 | use strict; use EAI::Wrap; | ||
| 2 | |||
| 3 | my $defaultSplitcharacter = "\t"; # default character that splits log entries | ||
| 4 | my $curDate = get_curdate(); | ||
| 5 | my $curDateDash = get_curdate_gen("Y/M/D"); | ||
| 6 | my $curhyphenDate = get_curdate_gen("Y-M-D"); | ||
| 7 | my $curdotDate = get_curdate_dot(); | ||
| 8 | my $curTime = "00".get_curtime_HHMM()."00"; | ||
| 9 | |||
| 10 | setupConfigMerge(); | ||
| 11 | |||
| 12 | my $logger = get_logger(); | ||
| 13 | $logger->info(">>>>>>> starting logcheck, curDate: $curDate, curDateDash: $curDateDash, curhyphenDate: $curhyphenDate, curdotDate: $curdotDate, curTime: $curTime, weekday(curDate):".weekday($curDate).",is_last_day_of_month(curDate):".is_last_day_of_month($curDate)); | ||
| 14 | my $beginOfDay = timelocal_modern(0,0,0,localtime->mday(), localtime->mon(), localtime->year()+1900); | ||
| 15 | my $fileAge = (stat("$execute{homedir}/alreadyProcessedLogErrors.txt"))[9]; | ||
| 16 | # remove yesterdays doneError information on first run of day | ||
| 17 | if (-e "$execute{homedir}/alreadyProcessedLogErrors.txt" and $fileAge < $beginOfDay) { | ||
| 18 | unlink "$execute{homedir}/alreadyProcessedLogErrors.txt" or $logger->error("couldn't remove $execute{homedir}/alreadyProcessedLogErrors.txt: $@"); | ||
| 19 | } | ||
| 20 | |||
| 21 | # read doneError information later for each check | ||
| 22 | my %doneError; | ||
| 23 | if (-e "$execute{homedir}/alreadyProcessedLogErrors.txt") { | ||
| 24 | open (DONE, "<$execute{homedir}/alreadyProcessedLogErrors.txt") or $logger->error("error opening $execute{homedir}/alreadyProcessedLogErrors.txt: $@"); | ||
| 25 | while (<DONE>) { | ||
| 26 | chomp($_); | ||
| 27 | $doneError{$_} = 1; | ||
| 28 | } | ||
| 29 | close DONE; | ||
| 30 | } | ||
| 31 | |||
| 32 | LOGCHECK: | ||
| 33 | foreach my $job (keys %{$config{checkLookup}}) { | ||
| 34 | next LOGCHECK if $job eq "checkLogExist.pl"; # don't check our own logfile, configuration only for own error emailing (e.g from setupConfigMerge) | ||
| 35 | my $splitcharacter = ($config{checkLookup}{$job}{splitcharacter} ? $config{checkLookup}{$job}{splitcharacter} : $defaultSplitcharacter); | ||
| 36 | # delayed checking for environments/specific jobs, either general $config{checkLogExistDelay}{$execute{env}} or job specific $config{checkLookup}{$job}{checkLogExistDelay}{$execute{env}}: | ||
| 37 | my $checkLogExistDelay = (defined($config{checkLookup}{$job}{checkLogExistDelay}{$execute{env}}) ? $config{checkLookup}{$job}{checkLogExistDelay}{$execute{env}} : (defined($config{checkLogExistDelay}{$execute{env}}) ? $config{checkLogExistDelay}{$execute{env}} : 0)); | ||
| 38 | my $freqToCheck = $config{checkLookup}{$job}{freqToCheck}; # frequency to check log file (business-daily, daily, monthly, etc.), default (if not given): every business day | ||
| 39 | $freqToCheck = "B" if !$freqToCheck; | ||
| 40 | my $timeToCheck = $config{checkLookup}{$job}{timeToCheck}; # earliest time to check log start entry | ||
| 41 | my $logFileToCheck = $config{checkLookup}{$job}{logFileToCheck}; # Logfile to be searched | ||
| 42 | my $logRootPath = ($config{checkLookup}{$job}{logRootPath} ne "" ? $config{checkLookup}{$job}{logRootPath} : $config{logRootPath}{""}); # default log root path | ||
| 43 | my $prodEnvironmentInSeparatePath = ($config{checkLookup}{$job}{prodEnvironmentInSeparatePath} ne "" ? $config{checkLookup}{$job}{prodEnvironmentInSeparatePath} : $config{prodEnvironmentInSeparatePath}); | ||
| 44 | my $mailsendTo = ($execute{envraw} ? $config{testerrmailaddress} : $config{checkLookup}{$job}{errmailaddress}); | ||
| 45 | # amend logfile path with environment, depending on prodEnvironmentInSeparatePath: | ||
| 46 | if ($prodEnvironmentInSeparatePath) { | ||
| 47 | $logFileToCheck = $logRootPath.'/'.$execute{env}.'/'.$logFileToCheck; | ||
| 48 | } else { | ||
| 49 | $logFileToCheck = $logRootPath.($execute{envraw} ? '/'.$execute{envraw} : "").'/'.$logFileToCheck; | ||
| 50 | } | ||
| 51 | my $logcheck = $config{checkLookup}{$job}{logcheck}; # Logcheck (regex) | ||
| 52 | $logger->info("preparing logcheck for $job, freqToCheck:$freqToCheck, timeToCheck:$timeToCheck, logFileToCheck:$logFileToCheck, logcheck regex:/$logcheck/"); | ||
| 53 | # skipping check dependent on date | ||
| 54 | my $resultSkip = skipOnDate($freqToCheck,$curDate,$config{logCheckHoliday}); | ||
| 55 | if ($resultSkip) { | ||
| 56 | $logger->info($resultSkip); | ||
| 57 | next LOGCHECK; | ||
| 58 | } | ||
| 59 | # skipping check if time not reached | ||
| 60 | my $checkTime = formatTime(make_time($timeToCheck."00",60*$checkLogExistDelay),"%02d%02d%02d%02d"); | ||
| 61 | if ($checkTime gt $curTime) { | ||
| 62 | $logger->info("IGNORING logcheck as timeToCheck ($timeToCheck) + checkLogExistDelay ($checkLogExistDelay) minutes (totals to $checkTime) is greater than curTime ($curTime)"); | ||
| 63 | next LOGCHECK; | ||
| 64 | } | ||
| 65 | # for non prod environments | ||
| 66 | if ($execute{envraw}) { | ||
| 67 | # ignore some jobs in non prod environments | ||
| 68 | if ($config{logs_to_be_ignored_in_nonprod} ne "" and $job =~ $config{logs_to_be_ignored_in_nonprod}) { | ||
| 69 | $logger->info("IGNORING logcheck as environment not Production and non production logs are to be ignored for $job due to match with ".$config{logs_to_be_ignored_in_nonprod}); | ||
| 70 | next LOGCHECK; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | my $infos = " is missing for job $job:\n"; | ||
| 74 | my $lastLogFile = $logFileToCheck; | ||
| 75 | my $logAppender = Log::Log4perl->appenders()->{"FILE"}->{"appender"} if Log::Log4perl->appenders() and Log::Log4perl->appenders()->{"FILE"}; | ||
| 76 | if ($logAppender) { | ||
| 77 | my $logprefix = get_curdate(); | ||
| 78 | unless ($logAppender->isa("Log::Dispatch::FileRotate")) { | ||
| 79 | $@ = ""; # reset bogus error messages from Log::Log4perl::init | ||
| 80 | eval {$logprefix = $config{logprefixForLastLogfile}->()} if $config{logprefixForLastLogfile}; | ||
| 81 | $logger->warn("error getting logprefix from \$config{logprefixForLastLogfile}: $@") if $@; | ||
| 82 | # if mail is watched next day, the rolled file is in $LogFPathDayBefore. Depending on appender, either append ".1" to filename or prepend $logprefix to it (default assume a date rotator, with current date in format yyyymmdd) | ||
| 83 | $lastLogFile =~ s/^(.+?[\\\/])([^\\\/]+?)$/$1$curDate\.$2/; | ||
| 84 | } else { | ||
| 85 | $lastLogFile .= ".1"; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | if (open (LOGFILE, "<$logFileToCheck")) { | ||
| 89 | # check log file for log check pattern, assumption tab separated! | ||
| 90 | while (<LOGFILE>){ | ||
| 91 | my $wholeLine = $_; | ||
| 92 | my @logline = split $splitcharacter; | ||
| 93 | # found, if log check pattern matches and date today, either YYYY/MM/DD or "german" logs using dd.mm.yyyy or log4j using YYYY-MM-DD | ||
| 94 | if (($logline[0] =~ /$curDateDash/ or $logline[0] =~ /$curdotDate/ or $logline[0] =~ /$curhyphenDate/) and $wholeLine =~ /$logcheck/) { | ||
| 95 | $logger->info("logcheck '".$logcheck."' successful, row:".$wholeLine); | ||
| 96 | if ($doneError{$job}) { | ||
| 97 | $infos = "The log starting entry in logfile $logFileToCheck was found now.\njob: <$job>, frequency: ".$freqToCheck.", time to check: ".$timeToCheck; | ||
| 98 | $doneError{$job} = 0; | ||
| 99 | $logger->info("logcheck successful for originally failed '".$job."', sending mail to: '".$mailsendTo); | ||
| 100 | # sendGeneralMail($From, $To, $Cc, $Bcc, $Subject, $Data, $Type, $Encoding, $AttachType, $AttachFile) | ||
| 101 | EAI::Common::sendGeneralMail("", $mailsendTo,"","","Resolved the starting problem for $job",$infos,'text/plain'); | ||
| 102 | } | ||
| 103 | close LOGFILE; next LOGCHECK; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | $logger->info("$logcheck wasn't found in $logFileToCheck on a line starting with $curDateDash or $curdotDate or $curhyphenDate"); | ||
| 107 | $infos = "The log starting entry in logfile $logFileToCheck".$infos; | ||
| 108 | } else { | ||
| 109 | $infos = "The logfile $logFileToCheck".$infos; | ||
| 110 | } | ||
| 111 | close LOGFILE; | ||
| 112 | # send mail for not found log entries | ||
| 113 | # insert $curDate before file name with a dot | ||
| 114 | unless ($doneError{$job}) { | ||
| 115 | $infos = $infos."\njob: <$job>, frequency: ".$freqToCheck.", time to check: ".$timeToCheck.", log in file file:///".$logFileToCheck." resp. file:///".$lastLogFile; | ||
| 116 | $logger->warn("failed logcheck for '".$job."', sending mail to: '".$mailsendTo); | ||
| 117 | # sendGeneralMail($From, $To, $Cc, $Bcc, $Subject, $Data, $Type, $Encoding, $AttachType, $AttachFile) | ||
| 118 | EAI::Common::sendGeneralMail("", $mailsendTo,"","","Starting problem detected for $job",$infos,'text/plain'); | ||
| 119 | $doneError{$job} = 1; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | my $jobErrors = ""; | ||
| 123 | for (sort keys(%doneError)) { | ||
| 124 | $jobErrors .= "$_\n" if $doneError{$_}; | ||
| 125 | } | ||
| 126 | open (DONE, ">$execute{homedir}/alreadyProcessedLogErrors.txt") or $logger->error("couldn't write to $execute{homedir}/alreadyProcessedLogErrors.txt: $@"); | ||
| 127 | print DONE $jobErrors; | ||
| 128 | close DONE; | ||
| 129 | __END__ |