I'm a newbie looking for some guidance on best practices for this specific task. I have strung up some code that is working based on help from others perl folks. I am still trying to identify my style. I learn by examples and I'd rather be more verbose with my code so that I can understand it.
I am still trying to get my arms around understanding the use of hashes. I have code here that is using hash ref. I'm unclear on how to print this out in a specific format.
Purpose of my script: 1) Get the hostname. The hostname is prefixed to the server logs. (ex. server1.log, server2.log, server2.log) 2) For each date given, either manually or systematically, give me the backup set name 3) For every backup set name, grab either one of these variables if they appear (backup-size, backup-time, backup-status, or ERROR if its given for that backup set) 4) Generate a datafile with these values delimited in whatever format. This datafile will be used later as feed to another system.
Current issues: My current script uses hashes, again from help. I'm still confused on hash of hashes and its use. I didn't go the array route, cause I'm not sure how to set that up and from what I read, accessing the hash would be easier? I'm not sure. I am having problems formatting the output in an ordered fashion. So, i'm looking at a structure like this:Current perl code:server1: MyDate (today's date) --> MyBackupSet --> Backup Attribute = Backup Value server2: MyDate (today's date) --> MyBackupSet --> Backup Attribute = Backup Value
Output from Dumper:use strict; use warnings; use File::Basename; use Data::Dumper; my %MyItems; my $ARGV ="/var/log/server1.log"; my $mon = 'Aug'; my $day = '06'; my $year = '2010'; while (my $line = <>) { chomp $line; print "Line: $line\n" if debug; if ($line =~ m/(.* $mon $day) \d{2}:\d{2}:\d{2} $year: ([^:]+):bac +kup:/) { my $server = basename $ARGV, '.log'; my $BckupDate="$1 $year"; my $BckupSet =$2; print "$BckupDate ($BckupSet): " if debug; $MyItems{$server}{$BckupSet}->{'MyLogdate'} = $BckupDate; $MyItems{$server}{$BckupSet}->{'MyDataset'} = $BckupSet; $MyItems{$server}{$BckupSet}->{'MyHost'} = $server; #$MyItems{$server}{$BckupSet}->{'MyServer'} = $server; if ($line =~ m/(ERROR|backup-size|backup-time|backup-status)[: +=](.+)/) { my $BckupKey=$1; my $BckupVal=$2; $MyItems{$server}{$BckupSet}->{$BckupKey} = $BckupVal; print "$BckupKey=$BckupVal\n" if debug; } } } print Dumper(%MyItems);
Sample output source file (server log file):$VAR1 = 'server1'; $VAR2 = { 'abc1.mil.mad' => { 'ERROR' => ' If you are sure is not +running, please remove the file and restart ', 'MyLogdate' => 'Fri Aug 06 2010', 'MyHost' => 'server1', 'MyDataset' => 'abc1.mil.mad' }, 'abc2.cfl.mil.mad' => { 'backup-size' => '187.24 GB', 'MyLogdate' => 'Fri Aug 06 2010', 'MyHost' => 'server1', 'backup-status' => 'Backup succeeded +', 'backup-time' => '01:54:27', 'MyDataset' => 'abc2.cfl.mil.mad' }, 'abc3.mil.mad' => { 'backup-size' => '46.07 GB', 'MyLogdate' => 'Fri Aug 06 2010', 'MyHost' => 'server1', 'backup-status' => 'Backup succeeded', 'backup-time' => '00:41:06', 'MyDataset' => 'abc3.mil.mad' }, 'abc4.mad_lvm' => { 'backup-size' => '422.99 GB', 'MyLogdate' => 'Fri Aug 06 2010', 'MyHost' => 'server1', 'backup-status' => 'Backup succeeded', 'backup-time' => '04:48:50', 'MyDataset' => 'abc4.mad_lvm' } };
Format I would like to try and create if possible (datafile):Fri Aug 06 00:00:04 2010: abc2.cfl.mil.mad:backup:INFO: Fri Aug 06 00:00:05 2010: abc2.cfl.mil.mad:backup:INFO: backup-set=abc +2.cfl.mil.mad Fri Aug 06 00:00:05 2010: abc2.cfl.mil.mad:backup:INFO: backup-date=20 +100806000004 Fri Aug 06 00:00:05 2010: abc2.cfl.mil.mad:backup:INFO: Fri Aug 06 00:48:54 2010: abc4.mad_lvm:backup:INFO: backup-size=422.99 + GB 0: abc4.mad_lvm:backup:INFO: flush-logs-time=00:00:00 Fri Aug 06 00:48:54 2010: abc4.mad_lvm:backup:INFO: backup-time=04:48: +50 Fri Aug 06 00:48:54 2010: abc4.mad_lvm:backup:INFO: backup-status=Back +up succeeded Fri Aug 06 00:48:54 2010: abc4.mad_lvm:backup:INFO: Backup succeeded Fri Aug 06 00:48:54 2010: abc4.mad_lvm:backup:INFO: PHASE START: Runni +ng post backup plugin Fri Aug 06 00:48:55 2010: abc4.mad_lvm:backup:INFO: PHASE END: Running + post backup plugin Fri Aug 06 00:48:55 2010: abc4.mad_lvm:backup:INFO: PHASE START: Clean +up Fri Aug 06 00:48:55 2010: abc4.mad_lvm:backup:INFO: PHASE END: Cleanup Fri Aug 06 00:48:55 2010: abc4.mad_lvm:backup:INFO: END OF BACKUP
MyHost=>server1;MyLogdate=>Fri Aug 06 2010;MyDataset=>abc2.cfl.mil.mad +;backup-time=>Fri Aug 06 2010;backup-status=>Backup succeeded MyHost=>server2;MyLogdate=>Fri Aug 06 2010;MyDataset=>abc4.mad_lvm;bac +kup-status=>Backup succeeded
In reply to Printing out a hash in specified format by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |