views:

46

answers:

1

I have a while loop that reads in a single file:

my %MyItems = ();
while (my $line = <>)
{
    chomp $line;

    if ($line =~ m/(.* $mon $day) \d{2}:\d{2}:\d{2} $year: ([^:]+):backup:/)   
    {      
        my $BckupDate="$1 $year";               
        my $BckupSet =$2;               

        $MyItems{$BckupSet}->{'MyLogdate'} = $BckupDate; 
        $MyItems{$BckupSet}->{'MyDataset'} = $BckupSet; 

        if ($line =~ m/(ERROR|backup-size|backup-time|backup-status)[:=](.+)/) 
        {
            my $BckupKey=$1;   
            my $BckupVal=$2; 
            $MyItems{$BckupSet}->{$BckupKey} = $BckupVal;
        } 
    }
}

The script is executed like so, passing in the log file as an argument:

./hashtst3.pl /var/log/server1.log 

My next progression of this script is to:

1-read more than 1 log file 
2-store the hostname of where the logfile is from
3-eventually print this data out with the other hashes  

Would I store this information in a separate text file to read in? Would I use a hash for this?

server1 /var/log/server1.log
server2 /var/log/server2.log
server3 /var/log/server3.log
...

here are my print statements:

#Final print format
print "### Verify Final Output\n";
for my $PrintItems (sort keys %MyItems)
{
    my %PrintVal = %{$MyItems{$PrintItems}};
    for my $PrintKey (sort keys %PrintVal)
    {
        printf "%s=>%s;\n", $PrintKey, $PrintVal{$PrintKey};
    }
    print "\n";
}

Here is my output as of adding code referencing {$server}:

### Verify Final Output
abc2.cfl.mil.mad=>HASH(0x9da338c);
abc3.mil.mad=>HASH(0x9e20708);
abc4.mad_lvm=>HASH(0x9e20660);
abc1.mil.mad=>HASH(0x9e20774);

backup-size=>187.24 GB;
backup-status=>Backup succeeded;
backup-time=>01:54:27;

backup-size=>46.07 GB;
backup-status=>Backup succeeded;
backup-time=>00:41:06;

backup-size=>422.99 GB;
backup-status=>Backup succeeded;
backup-time=>04:48:50;

Adding current output per help, using dumper:

$VAR1 = 'server1';
$VAR2 = {
          'abc1.mil.mad' => {    'ERROR' => ' Error mesg here',
                                 'MyLogdate' => 'Fri Aug 06 2010',
                                 'MyDataset' => 'abc1.mil.mad'
                               },
          'abc2.cfl.mil.mad' => {
                                  'backup-size' => '187.24 GB',
                                  'MyLogdate' => 'Fri Aug 06 2010',
                                  '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',
                                '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',
                                'backup-status' => 'Backup succeeded',
                                'backup-time' => '04:48:50',
                                'MyDataset' => 'abc4.mad_lvm'
                              }

Sample output I am looking to print:

MyHost=>Server1;MyDataset=>abc2.cfl.mil.mad;MyLogdate=>Fri Aug 06 2010;backup-size=>187.24 GB;backup-status=>Backup succeeded;backup-time=>01:54:27;
+3  A: 

You will find the builtin variable $ARGV useful:

$ARGV contains the name of the current file when reading from <>.

use strict; use warnings;

use File::Basename;
my %MyItems;

while (my $line = <>) {
    chomp $line;

    if ($line =~ m/(.* $mon $day) \d{2}:\d{2}:\d{2} $year: ([^:]+):backup:/)
    {
        my $server = basename $ARGV, '.log';
        my $BckupDate ="$1 $year";               
        my $BckupSet = $2;               

        $MyItems{$server}{$BckupSet}{MyLogdate} = $BckupDate; 
        $MyItems{$server}{$BckupSet}{MyDataset} = $BckupSet; 

        if ($line =~ m/(ERROR|backup-size|backup-time|backup-status)[:=](.+)/) 
        {
            my $BckupKey=$1;   
            my $BckupVal=$2;
            # *** Don't forget *** # 
            $MyItems{$server}{$BckupSet}{$BckupKey} = $BckupVal;
        } 
    }
}
Sinan Ünür
@Sinan - thanks for your reply. So would I set my $ARGV = 'hosts.txt'?where 'hosts.txt' file include (server1 /var/log/server1.log)?
jda6one9
You are already reading from `<>`. I am assuming you are passing the filenames to be processed on the command line.
Sinan Ünür
@Sinan - got that piece working. I am having issues now trying to print out that hash were got the $server name. I posted my output above.
jda6one9
You are not printing the structures correctly. And, it seems like you forgot to add the `$server` level to the hash in the second hash. Use `Data::Dumper` or `YAML` to dump complicated data structures reliably.
Sinan Ünür
@Sinan - sorry, I am confused. When you say "add the $server level to hash in the second hash", are you referring to hash (%PrintVal) in the print section?
jda6one9
@jaedre619 See the line marked `# *** Don't forget ***`
Sinan Ünür