JaeDre619 has asked for the wisdom of the Perl Monks concerning the following question:
I am definitely a newbie and perl is my first programming language! In the last few weeks, I've managed to learn to write some basic code to parse a log file and extract some needed data. Now after using it for a few days, I'm finding that I do need to rework this to get a complete data set and need your help in reworking and/or adding to this.
Code synopsis: Analyze logfile based on a date. For the given date, read each line in the log and extract data for each backup set and gather particular name(attribute)=value pairs.
My issue: I need to alter/change this logic to read not on the given date, but on the last time I read the log entry. I have another shell script that pulls in a copy of the logfile to be read. Each time I copy could have new records added to it. So, I need to somehow bookmark this. This is where I need help coding this piece.
For example, if the backup (backup.set2_lvm) started on the night before (Aug 15 20:00 and continues to Aug 16 00:33), I would not capture (Aug 15 20:00) entry since my script is only set to read the data for the 16th.
For example, my raw logfile/input looks something like this:Sun Aug 15 20:00:03 2010: backup.set2_lvm:backup:INFO: START OF BACKUP Sun Aug 15 20:00:04 2010: backup.set2_lvm:backup:INFO: backup-set=back +up.set2_lvm Sun Aug 15 20:00:04 2010: backup.set2_lvm:backup:INFO: backup-date=201 +00815200003 Sun Aug 15 20:00:04 2010: backup.set2_lvm:backup:INFO: backup-type=reg +ular Sun Aug 15 20:00:04 2010: backup.set2_lvm:backup:INFO: backup-date-epo +ch=1281927603 Sun Aug 15 20:00:04 2010: backup.set2_lvm:backup:INFO: backup-director +y=/home/backups/backup.set2_lvm/20100815200003 Mon Aug 16 00:00:04 2010: backup.set1_lvm:backup:INFO: START OF BACKUP Mon Aug 16 00:00:05 2010: backup.set1_lvm:backup:INFO: backup-set=back +up.set1_lvm Mon Aug 16 00:00:05 2010: backup.set1_lvm:backup:INFO: backup-date=201 +00816000003 Mon Aug 16 00:00:05 2010: backup.set1_lvm:backup:INFO: backup-type=reg +ular Mon Aug 16 00:00:05 2010: backup.set1_lvm:backup:INFO: backup-date-epo +ch=1281942003 Mon Aug 16 00:33:15 2010: backup.set2_lvm_lvm:backup:INFO: last-backup +=/home/backups/backup.set2_lvm_lvm/20100814200003 Mon Aug 16 00:33:15 2010: backup.set2_lvm_lvm:backup:INFO: backup-size +=424.53 GB Mon Aug 16 00:33:15 2010: backup.set2_lvm_lvm:backup:INFO: backup-time +=04:33:12 Mon Aug 16 00:33:15 2010: backup.set2_lvm_lvm:backup:INFO: backup-stat +us=Backup succeeded Mon Aug 16 00:33:15 2010: backup.set2_lvm_lvm:backup:INFO: Backup succ +eeded Mon Aug 16 00:33:16 2010: backup.set2_lvm_lvm:backup:INFO: END OF BACK +UP Mon Aug 16 01:59:07 2010: backup.set1_lvm:backup:INFO: last-backup=/ho +me/backups/backup.set1_lvm/20100815000006 Mon Aug 16 01:59:07 2010: backup.set1_lvm:backup:INFO: backup-size=187 +.24 GB Mon Aug 16 01:59:07 2010: backup.set1_lvm:backup:INFO: backup-time=01: +59:04 Mon Aug 16 01:59:07 2010: backup.set1_lvm:backup:INFO: backup-status=B +ackup succeeded Mon Aug 16 01:59:07 2010: backup.set1_lvm:backup:INFO: Backup succeede +d Mon Aug 16 01:59:09 2010: backup.set1_lvm:backup:INFO: END OF BACKUP
Basically, I would like to keep track of the events per given "backup.set#". So next time the script is initiated it will look at the next new set of events for that given set.
I hope that makes sense on what I am looking for. Please provide any good examples of doing this if you have any. Thanks.
My code:I appreciate the help!use strict; use warnings; use File::Basename; use Data::Dumper; my ($ServerName)=@ARGV; #ARGV = /var/log/server1.mydomain.com.backup-s +oftware.log my %MyItems; my $mon; my $day; my $year; foreach my $ServerName(@ARGV){ while (my $line = <>){ chomp $line; print "Line: $line\n" if debug; sub spGetCurrentDateTime; ($mon, $day, $year) = spGetCurrentDateTime; ($mon, $day, $year) = split(" ", $mon); if ($line =~ m/(.* $mon $day) \d{2}:\d{2}:\d{2} $year: ([^:]+):bac +kup:/){ my $ServerName = basename $ARGV, '.mydomain.com.backup-software. +log'; my $BckupDate="$1 $year"; my $BckupSet =$2; $MyItems{$ServerName}{$BckupSet}->{'1-Server'} = $ServerName; $MyItems{$ServerName}{$BckupSet}->{'2-Logdate'} = $BckupDate; $MyItems{$ServerName}{$BckupSet}->{'3-BackupSet'} = $BckupSet; if ($line =~ m/.* \w+ \d{2} (\d{2}:\d{2}:\d{2}) \d{4}: ([^:]+):b +ackup:.*(START OF BACKUP)/){ my $BckupKey=$2; my $BckupVal=$1; $MyItems{$ServerName}{$BckupSet}->{'4-StartTime'} = $BckupVa +l; } if ($line =~ m/(backup-time)[:=](.+)/){ my $BckupKey="5-Duration"; my $BckupVal=$2; $MyItems{$ServerName}{$BckupSet}->{$BckupKey} = $BckupVal; } if ($line =~ m/(backup-size)[:=](.+)/){ my $BckupKey="6-Size"; #my $BckupKey=$1; my $BckupVal=$2; $MyItems{$ServerName}{$BckupSet}->{$BckupKey} = $BckupVal; } if ($line =~ m/(Backup succeeded)/){ my $BckupKey="7-Status"; my $BckupVal="Succeeded"; $MyItems{$ServerName}{$BckupSet}->{$BckupKey} = $BckupVal; } if ($line =~ m/(ERROR)[:=](.+)/){ my $BckupKey="8-Status"; my $BckupVal="Unsuccessful"; $MyItems{$ServerName}{$BckupSet}->{$BckupKey} = $BckupVal; } } } sub spGetCurrentDateTime{ my ($sec, $min, $hour, $mday, $mon, $year) = localtime(); my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ); my $currentDateTime = sprintf "%s %02d %4d", $abbr[$mon], $mday, $ +year+1900; #Returns => 'Jul 26 2010' return $currentDateTime; } #print Dumper(\%MyItems); for my $ServerName(keys%MyItems){ for my $BckupSet(keys%{$MyItems{$ServerName}}){ for(sort keys%{$MyItems{$ServerName}{$BckupSet}}){ print$_,'=',$MyItems{$ServerName}{$BckupSet}{$_},';'; } print"\n"; } } }
|
---|