#! /usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %hash;
my $log_file = '/path/to/file.log';
open(LOG,"<$log_file") or die $!;
while(<LOG>){
next if($_ =~ /^\s*?$/);
$_ =~ s/^\s+//;
my ($path,$online,$offline) = split(/\s+/,$_);
$path =~ s/^\///; #/
my ($key,$sub) = split(/\//,$path,2);
$hash{$key} = insert_rec($key,$sub,$online,$offline,$hash{$key});
}
close LOG;
my ($traffic_on,$traffic_off) = calc_traffic(\%hash,'');
print "Gesamt: Online: ",$traffic_on," Offline: ",$traffic_off,"\n";
# insert_rec builds the hash recursivly.
# Parameters:
# key of anonymous hash that has to be expanded
# remaining path
# online traffic
# offline traffic
# reference to hash
sub insert_rec{
my ($key2,$path,$online,$offline,$hashref) = @_;
my ($key,$sub) = split(/\//,$path,2);
unless($sub){
$hashref->{$key} = {online => $online, offline => $offline};
return($hashref);
}
$hashref->{$key} = insert_rec($key,$sub,$online,$offline,$hashref->{$key}) if($sub);
return ($hashref);
}# end insert_rec
# calc traffic calculates the traffic.
# Traffic of a directory includes the traffic of sub-directories
# Parameters:
# Reference to a hash that contains the structure and the on-/offline-traffic
sub calc_traffic{
my ($hashref,$path) = @_;
my $onlines = $hashref->{online};
my $offlines = $hashref->{offline};
foreach my $key(keys(%$hashref)){
next unless(ref($hashref->{$key}));
my $new_path = $path.'/'.$key;
my ($on,$off) = calc_traffic($hashref->{$key},$new_path);
$onlines += $on;
$offlines += $off;
print $new_path," online= ",$on," offlines= ",$off,"\n";
}
return ($onlines,$offlines);
}# end calc_traffic