Hello perlmonks. I currently have an ugly script that is comparing two logfiles and using a config file to check if the error in the config file exist in the logfile and report the error if there is a difference.

This script is being used on a windows box using Active Perl.

My problem is that I need for the two files to compare, if they are different is the difference because of an error. If it is, does that error match what is in the config file. Currently the way the script is written it will compare the files and return an error if the files are different, but it spits out every alert including those from a previous run. Please excuse the mess, going to clean it up after the initial requirements are met.

#!/usr/bin/perl use warnings; use strict; use File::Compare; use FileHandle; use File::Stat; use File::Copy; my $config = 'C:\NSClient++\scripts\check_logfiles.cfg'; my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = loc +altime(time); my $time_now = sprintf("%02d%02d", $hour,$min); my @errors = (); my $return = 0; if (-e $config) { check_files(); if ( scalar(@errors) gt 0 ) { print join(", ", @errors); $return = 2; } elsif ( scalar(@errors) eq 0 ) { print "No problems detected"; $return = 0; } else { print "This should never happen"; $return = 3; } } else { print "Config file: $config does not exist!\n"; $return = 2; } #sub dates_match # Main sub check_files{ open (CFG, "<", "$config") or die "Can't open config ($config) file: $ +!"; while(<CFG>) { next if /^#/; # Skip comments next if /^\s*$/; # Skip blank lines my ($start_time, $end_time, $start_day, $end_day, $drive, +$fpath, $fname, $error) = split /:/; chomp ($error); if (($time_now >= $start_time && $time_now <= $end_time) & +& ($wday >= $start_day && $wday <= $end_day)) { my $filename = "$drive:". "$fpath". "\\". "$fname"; my $file2 = $filename.".2"; my $count = 0; #print "hello $error"; if ( File::Compare::compare_text($filename, $file2) == + 1){ open (LOG, "<", "$filename") or die "Can't open ($ +filename) file: $!"; while(<LOG>) { chomp; if (/$error/) { # does the error exist push (@errors, "$error"); copy ("$filename","$file2") or die "Copy f +ailed: $!"; } } close(LOG); } elsif ( File::Compare::compare_text($filename, $file2) + == 0){ } copy ("$filename","$file2") or die "Copy failed: $!"; } #use File::Copy; } close (CFG); }

In reply to Logfile checking by pramone

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.