#! /usr/bin/perl # this program merges an old and a new MRTG log into a single log use strict; my @HELP = qq( mergeolddata: invalid option -- @ARGV usage: mergeolddata [current log file] [old log file] ); die "@HELP" unless ($ARGV[0]); my $current_file = $ARGV[0]; my $old_file = $ARGV[1]; # read in log files open(CURRENTFILE,"$current_file"); my @current = ; close(CURRENTFILE); open(OLDFILE,"$old_file"); my @old = ; close(OLDFILE); # make backup my $epoch = time(); system("cp $current_file $current_file.backup_$epoch"); my %current_array; my %old_array; # make a hash of the old log data for my $line (@old) { my ($timestamp, $data1, $data2, $data3, $data4) = split (/\s+/, $line); $old_array{$timestamp} = ("$data1 $data2 $data3 $data4"); } # make a hash of the current log data my @OUTDATA; for my $line (@current) { my ($timestamp, $data1, $data2, $data3, $data4) = split (/\s+/, $line); $current_array{$timestamp} = ("$data1 $data2 $data3 $data4"); if ($data1 eq "0" && $old_array{$timestamp}) { push (@OUTDATA, "$timestamp $old_array{$timestamp}\n"); } elsif ($data1 eq "0" && ! $old_array{$timestamp}) { push (@OUTDATA, "$timestamp $current_array{$timestamp}\n"); } elsif ($data1 gt "0") { push (@OUTDATA, "$timestamp $current_array{$timestamp}\n"); } } # write out the new log open(NEWFILE,">$current_file"); print NEWFILE @OUTDATA; close(NEWFILE);