Hash of Hashes

D

doni

Hi,

I am having some problems in printing the Hash of Hashes.
MAC, PHY and Network stats gets generated every 30 minutes and are
copied to a file.
I want to extract all the stats information that is copied to a file
into an hash of hashes and print them.
The file has multiple MAC, PHY and Network Statistics in it and I am
extracting the MAC data in a MAC hash of hashes, PHY data in a PHY hash
of hashes and Network data in a Network hash of hashes.
Here is how the data that is represented in a file. There will be
multiple MAC, PHY and Network Statistics in the file but I am showing
only one of them in here as an example.

MAC statistics:
0 frames with invalid header
546 confirmed frames sent succesfully
1693 confirmed frames sent unsuccesfully
542 confirmed frames received
4597 total frames received
851 data packets received
549 packets sent successfully
152 packets sent unsuccessfully
763 packets queued by network layer
61 packet transmissions timed out
0 packets with invalid length
0 out of order fragments received
87 duplicate fragments received
0 partial packets received
4 packet reassemblies timed out
0 no buffers
PHY statistics:
3404 frames transmitted
105 transmits aborted due to rx
0 transmit errors
0 late transmits
11 tx buffer loads interrupted
0 tx buffer load failures
4598 receive interrupts
4598 frames received
0 received frames lost
0 zero length frames received
0 receive cmd errors
Network statistics:
851 total packets received
0 bad packet length
0 bad version number
0 outgoing packets with no route
0 input packets with bad destination address
0 input packets from wrong channel
0 packets dropped for exceeding TTL
0 packets dropped due to full socket buffers
0 packets dropped due to no receiver
0 packets dropped due to running out of memory
762 total packets sent
549 packets succesfully transmitted
213 packets sent unsuccessfully

For example, if there were 10 MAC Statistics in the file, this is how I
want the data from the "frames with invalid header" stats message to be
copied to a hash.
$mac{'frames with invalid header'}{1} = 0
$mac{'frames with invalid header'}{2} = 0
........ $mac{'frames with invalid header'}{10} = 0

Here is the program I wrote to do the above operation but its not
working as expected. Can anyone let me know where I went wrong.

Thanks,
doni

### Open the netstat file copied to this location ###
my $ex_data = 'netstat.log';
open (NETSTAT,$ex_data) || die("Cannot Open File: $!");

my %mac = (); # Will be the HOH for MAC Statistics
my %phy = (); # Will be the HoH for PHY Statistics
my %bbu = (); # Will be the HoH for BBU Statistics
my %dli = (); # Will be the HoH for DLI Statistics
my $stats; # State Variable
my $i = 0; # Count variable for MAC
my $j = 0; # Count Variable for PHY
my $a = 0; # Count Variable for Network
my $x = 0; # Count Varialbe for Hash

while (<NETSTAT>) {
chomp;
if (/^(\S+)\s+statistics:/) {
$stats = $1;
if ($stats =~ /mac/i)
{ $i++; }
elsif ($stats =~ /phy/i)
{ $j++; }
else
{ $a++; }
}

elsif (/^\s+(\d+)\s+(\S.*)/)
{
($value, $key) = ($1, $2);
if ($stats =~ /mac/i) {
next unless defined $stats;
$mac{$key}{$i} = $value;
}
if ($stats =~ /phy/i) {
next unless defined $stats;
$phy{$key}{$j} = $value;
}
if ($stats =~ /network/i) {
next unless defined $stats;
$network{$key}{$a} = $value;
}
}
else { }
}

foreach $key (keys (%mac)) {
foreach $x (keys %{$mac{$key} })
{
while ($x <= $i) {
print "Message Value is: $mac{$key}{$i}\n";
$x++;
}

}
}

close(NETSTAT) || die("Cannot close $ex_data: $!");
 
K

kens

Hi,

I am having some problems in printing the Hash of Hashes.
MAC, PHY and Network stats gets generated every 30 minutes and are
copied to a file.

You do not say what the problem is?
I want to extract all the stats information that is copied to a file
into an hash of hashes and print them.
The file has multiple MAC, PHY and Network Statistics in it and I am
extracting the MAC data in a MAC hash of hashes, PHY data in a PHY hash
of hashes and Network data in a Network hash of hashes.
Here is how the data that is represented in a file. There will be
multiple MAC, PHY and Network Statistics in the file but I am showing
only one of them in here as an example.

MAC statistics:
0 frames with invalid header
546 confirmed frames sent succesfully
1693 confirmed frames sent unsuccesfully
542 confirmed frames received
4597 total frames received
851 data packets received
549 packets sent successfully
152 packets sent unsuccessfully
763 packets queued by network layer
61 packet transmissions timed out
0 packets with invalid length
0 out of order fragments received
87 duplicate fragments received
0 partial packets received
4 packet reassemblies timed out
0 no buffers
PHY statistics:
3404 frames transmitted
105 transmits aborted due to rx
0 transmit errors
0 late transmits
11 tx buffer loads interrupted
0 tx buffer load failures
4598 receive interrupts
4598 frames received
0 received frames lost
0 zero length frames received
0 receive cmd errors
Network statistics:
851 total packets received
0 bad packet length
0 bad version number
0 outgoing packets with no route
0 input packets with bad destination address
0 input packets from wrong channel
0 packets dropped for exceeding TTL
0 packets dropped due to full socket buffers
0 packets dropped due to no receiver
0 packets dropped due to running out of memory
762 total packets sent
549 packets succesfully transmitted
213 packets sent unsuccessfully

For example, if there were 10 MAC Statistics in the file, this is how I
want the data from the "frames with invalid header" stats message to be
copied to a hash.
$mac{'frames with invalid header'}{1} = 0
$mac{'frames with invalid header'}{2} = 0
....... $mac{'frames with invalid header'}{10} = 0

Here is the program I wrote to do the above operation but its not
working as expected. Can anyone let me know where I went wrong.

What is it doing that you did not expect?
Thanks,
doni


Always begin with the following two lines:

use strict;
use warnings;
### Open the netstat file copied to this location ###
my $ex_data = 'netstat.log';
open (NETSTAT,$ex_data) || die("Cannot Open File: $!");

my %mac = (); # Will be the HOH for MAC Statistics
my %phy = (); # Will be the HoH for PHY Statistics
my %bbu = (); # Will be the HoH for BBU Statistics
my %dli = (); # Will be the HoH for DLI Statistics
my $stats; # State Variable
my $i = 0; # Count variable for MAC
my $j = 0; # Count Variable for PHY
my $a = 0; # Count Variable for Network
my $x = 0; # Count Varialbe for Hash

while (<NETSTAT>) {
chomp;
if (/^(\S+)\s+statistics:/) {
$stats = $1;
if ($stats =~ /mac/i)
{ $i++; }
elsif ($stats =~ /phy/i)
{ $j++; }
else
{ $a++; }
}

elsif (/^\s+(\d+)\s+(\S.*)/)
{
($value, $key) = ($1, $2);
if ($stats =~ /mac/i) {
next unless defined $stats;
$mac{$key}{$i} = $value;
}
if ($stats =~ /phy/i) {
next unless defined $stats;
$phy{$key}{$j} = $value;
}
if ($stats =~ /network/i) {
next unless defined $stats;
$network{$key}{$a} = $value;
}
}
else { }

}foreach $key (keys (%mac)) {

Why do you have this 'foreach' loop?
Set $x = 1 and just use the 'while' loop'.
foreach $x (keys %{$mac{$key} })
{
while ($x <= $i) {

Note that $i never changes, I believe you want $x.
print "Message Value is: $mac{$key}{$i}\n";
$x++;
}

}

}close(NETSTAT) || die("Cannot close $ex_data: $!");

HTH,
Ken
 
D

doni

You do not say what the problem is?

When I am trying to print %mac HoH, I am not getting the desired
result. I was expecting that all the stats under MAC will be printed 10
times starting with (x=1) if there were 10 repeats of MAC Statistics in
the file.

I was using this loop so that I know what particular stats under
MAC is coming out...

Thanks,
doni
 
J

John W. Krahn

doni said:
I am having some problems in printing the Hash of Hashes.
MAC, PHY and Network stats gets generated every 30 minutes and are
copied to a file.
I want to extract all the stats information that is copied to a file
into an hash of hashes and print them.
The file has multiple MAC, PHY and Network Statistics in it and I am
extracting the MAC data in a MAC hash of hashes, PHY data in a PHY hash
of hashes and Network data in a Network hash of hashes.
Here is how the data that is represented in a file. There will be
multiple MAC, PHY and Network Statistics in the file but I am showing
only one of them in here as an example.

MAC statistics:
0 frames with invalid header
546 confirmed frames sent succesfully

[ snip ]
549 packets succesfully transmitted
213 packets sent unsuccessfully

For example, if there were 10 MAC Statistics in the file, this is how I
want the data from the "frames with invalid header" stats message to be
copied to a hash.
$mac{'frames with invalid header'}{1} = 0
$mac{'frames with invalid header'}{2} = 0
....... $mac{'frames with invalid header'}{10} = 0

Here is the program I wrote to do the above operation but its not
working as expected. Can anyone let me know where I went wrong.


### Open the netstat file copied to this location ###
my $ex_data = 'netstat.log';
open (NETSTAT,$ex_data) || die("Cannot Open File: $!");

[ snip code ]

Perhaps you need something like this:

use warnings;
use strict;

### Open the netstat file copied to this location ###
my $ex_data = 'netstat.log';
open NETSTAT, $ex_data or die "Cannot Open $ex_data: $!";

my $state; # State Variable
my %stats = ( mac => undef, phy => undef, network => undef );

while ( <NETSTAT> ) {
if ( /^(\S+)\s+statistics:/ ) {
$state = lc $1;
}
if ( exists $stats{ $state } && /^\s+(\d+)\s+(.+)/ ) {
$stats{ $state }{ $2 } += $1;
}
}

close NETSTAT or die "Cannot close $ex_data: $!";

for my $key ( keys %stats ) {
print "$key statistics:\n";
for my $msg ( keys %{ $stats{ $key } } ) {
print "\t$stats{$key}{$msg} $msg\n";
}
}

__END__




John
 
J

Josef Moellers

doni said:
Hi,

I am having some problems in printing the Hash of Hashes.

Beg your pardon?
Why are you starting a new thread on almost the same subject as the old
thread? Are you not satisfied with the answers that you get? Or is
progress too slow and the deadline for your homework is nearing and you
are desperate to get answers?
 
D

doni

Beg your pardon?
Why are you starting a new thread on almost the same subject as the old
thread? Are you not satisfied with the answers that you get? Or is
progress too slow and the deadline for your homework is nearing and you
are desperate to get answers?

Since, my comments were not clear in my old thread, I rephrased my
question on this thread with more comments and what answer I was
expecting.
I am sorry if I am not supposed to open a new thread if I have stated
my question in an old thread.

Thanks,
doni
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top