rscott212 has asked for the wisdom of the Perl Monks concerning the following question:

I have been working on this script for over a month. I have read up on last, if, next, and other commands. The more I change, the further off my script is. Everything works EXCEPT when my script is looking for errors. The script should read in all of the log files in a directory When it finds the word ERROR in a file it should page mike and then go to the next. It should not page Mike more than once for each file. I put need help where this block starts.

Below is my code:

#*****************************# #** HelpDeskLog error check **# #*****************************# use File::Copy; use File::Basename; #******** Setting Date Stamp ********## ($Second, $Minute, $hour, $DayOfMonth, $Month, $Year, $WeekDay, $DayOfYear, $IsDST) = localtime(time); $yyyy = $Year + 1900; my ($d,$m) = (localtime)[3,4]; my $date = sprintf("%02d%02d",++$m,$d,); my $archive = sprintf ("%02d",$m); my $archivemonth = "L\:\\helpdesklog\\2002\\$archive"; #******** Variables ********# $datematch = "$yyyy$date"; $search = "ERROR"; $holding = "L\:\\helpdesklog\\holding"; $error_dir = "L\:\\helpdesklog\\errors"; #******** Error Check ********# system ("net use L\: \\\\BOB\\d\$ turtle \/user\:MIKE\\ANDY");; #system ('L: \n'); opendir (HELPDESK, 'L:\helpdesklog'); chdir 'L:\HelpDeskLog'; @records = readdir (HELPDESK); foreach $record (@records) { copy ("$record", "$holding"); if (-d $record) {next} else { system ("del $record \/q"); } } closedir (HELPDESK); #******** Checking asci files for corresponding log file ********# opendir (HOLDING, 'L:\helpdesklog\holding'); chdir 'L:\helpdesklog\holding'; @asci = glob ('*.asc'); foreach $asci (@asci) { my $fullpath = "L\:\\helpdesklog\\holding\\$asci"; my ($name, $dir, $extension) = fileparse($fullpath, '\..*'); my $ext = "\.log"; my $newfile = $name++.$ext; @logfiles = glob ("$newfile*"); if (scalar(@logfiles) == 0) { system ('page Mike "Check the HelpDeskLog\ERRORS dir, an asci file was created without a corresponding log file."'); copy ("$asci", "$error_dir"); system ("del $asci \/q"); } } #******** Checking log files for corresponding asci file ********# @log = glob ('*.log'); foreach $log (@log) { my $fullpath = "L\:\\helpdesklog\\holding\\$log"; my ($name, $dir, $extension) = fileparse($fullpath, '\..*'); my $ext = "\.asc"; my $newfile = $name++.$ext; @ascifiles = glob ("$newfile*"); if (scalar(@ascifiles) == 0) { system ('page Mike "Check the HelpDeskLog\ERRORS dir, a log file was created without a corresponding asci file."'); copy ("$log", "$error_dir"); system ("del $log \/q"); } } #******** Checking log files for errors ********# # need help @errorcheck = glob ('*.log'); my $error = "ERROR"; my $errorcount = 0; foreach $logrecord (@errorcheck) { open (LOGFILE, "$logrecord"); while (<LOGFILE>) { my $logline = $_; if ($logline =~ /$error/) { $errorcount++; } print "$errorcount\n"; if ($errorcount != 0) {system ('page Mike "An error was encountered processing a log file. Check the HelpDeskLog\ERRORS dir f +or more information."'); last;} } } #print "$errorcount\n"; close (LOGFILE); #******** Archiving files *******# my $copypath = "L\:\\HelpDeskLog\\$yyyy\\$archive"; print "$archive\n"; my $oldcopypath = $archive -1; my $oldcopypath2 = sprintf ("%02d",$oldcopypath); my $oldarchive = "L\:\\HelpDeskLog\\$yyyy\\$oldcopypath2"; my @finalcopy = readdir (HOLDING); foreach $fcopy (@finalcopy) { if ($fcopy =~ /^$yyyy$archive/) { copy ("$fcopy", "$copypath"); } else { copy ("$fcopy", "$oldarchive"); } } system ('del *.* /q'); closedir (HOLDING); chdir 'd:'; system ("net use \/DELETE L\:");

Any help would be greatly appreciated! TIA! ~rscott212

  • Comment on Scanning a file for all ERRORs, then sending only one page (break out?)
  • Download Code

Replies are listed 'Best First'.
Re: Scanning a file for all ERRORs, then sending only one page (break out?)
by Kanji (Parson) on Apr 30, 2002 at 22:17 UTC

    Well, the simplest method would be to increment a counter everytime you system('page...') and then not page again if the counter is ever greater than 0.

    system('page...') unless $already_paged++;

        --k.


Re: Scanning a file for all ERRORs, then sending only one page (break out?)
by Albannach (Monsignor) on Apr 30, 2002 at 23:19 UTC
    You could re-arrange your code a bit. You are paging every time an ERROR is found (and so you really don't need to be checking $errorcount each time you page either). I suspect you don't really want to print $errorcount each time it is incremented either. You also seem to want to page poor Mike for each file that contains an error, so you need to re-set your $errorcount after each file too. Actually if you are going to page him for each bad file, you might as well tell him the filename too. The loop could become:
    foreach my $logrecord (@errorcheck) { my $errorcount; open (LOGFILE, $logrecord); while (<LOGFILE>) { if(/$error/) { $errorcount++; } } print "$logrecord: $errorcount\n"; if($errorcount != 0) { system ('page Mike "An error was encountered processing file $logr +ecord. Check the HelpDeskLog\ERRORS dir for more information."'); #last; here if you don't want to bother Mike more than once }
    You should consider checking that the opens and the system call in fact worked too. Probably the best solution would be to keep a count of all errors, and all files that contained errors, and then just page the long suffering Mike with a message like "$errortotal errors found in $badfiletotal files".

    --
    I'd like to be able to assign to an luser