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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |