#!/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) = localtime(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() { 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() { chomp; if (/$error/) { # does the error exist push (@errors, "$error"); copy ("$filename","$file2") or die "Copy failed: $!"; } } close(LOG); } elsif ( File::Compare::compare_text($filename, $file2) == 0){ } copy ("$filename","$file2") or die "Copy failed: $!"; } #use File::Copy; } close (CFG); }