#!/usr/bin/env perl use 5.010; use strict; use warnings; my %h; my %m; my %db; # per-hour hash, per-minute hash, database names sub set_column_widths { my $h = shift; # reference to hash table of speeds, keyed by datetime, then by database my $databases = shift; # reference to hash of database names, where we will set the width values my $names = shift; # ref to array of database names # (so we don't need to call keys on $databases repeatedly) for my $key (keys %$h){ for my $db (@$names){ my $l = length $h->{$key}{$db}; # get length of this item in the hash table # set column width to the wider length $databases->{$db} = $databases->{$db} > $l ? $databases->{$db} : $l; } } for my $db (@$names){ # check the width of the database names themselves too my $l = length $db; $databases->{$db} = $databases->{$db} > $l ? $databases->{$db} : $l; } } while(){ next unless /\w/; # skip blank lines my($datetime,$database,$speed) = (split)[1,2,3]; my $ddhhmm = substr $datetime,0,16; # substr works well here since the lengths are static my $ddhh = substr $datetime,0,13; # this one doesn't include the minutes $h{$ddhh }{$database} += $speed; # add the speed to this hour & database $m{$ddhhmm}{$database} += $speed; # add the speed to this minute & database $db{$database} = 1; # save the database name } my @db = sort keys %db; # sort and save database names as array since we'll be looping through them many times # HOUR SECTION START # calculate column widths set_column_widths(\%h, \%db, \@db); # print out the per-hour stats # starting with a header line print "Frequency Hour:\ncollectionTime"; printf " %$db{$_}s", $_ for (@db); # print each database name as a header with dynamic width print "\n"; # end of line for my $key (sort keys %h){ print "$key "; # print the date/hour key printf " %$db{$_}s", $h{$key}{$_} for (@db); # print the value for each database that goes with this key print "\n"; } # HOUR SECTION END # MINUTE SECTION START (using %m instead of %h) # calculate column widths set_column_widths(\%m, \%db, \@db); # print out the per-minute stats # starting with a header line print "\nFrequency Minute:\n collectionTime"; printf " %$db{$_}s", $_ for (@db); # print each database name as a header with dynamic width print "\n"; # end of line for my $key (sort keys %m){ print $key; # print the date/hour/minute key printf " %$db{$_}s", $m{$key}{$_} for (@db); # print the value for each database that goes with this key print "\n"; } # MINUTE SECTION END __DATA__ server01: 2015-06-01T12:40:03-04:00 DB101 10 MB/sec server01: 2015-06-01T12:40:03-04:00 DB202 5 MB/sec server01: 2015-06-01T12:40:03-04:00 ASM 2 MB/sec server01: 2015-06-01T12:40:03-04:00 MYDB101 2 MB/sec server01: 2015-06-01T12:40:03-04:00 MYDB202 5 MB/sec server01: 2015-06-01T12:40:03-04:00 _OTHER_DB_ 30 MB/sec server01: 2015-06-01T12:41:03-04:00 DB101 3 MB/sec server01: 2015-06-01T12:41:03-04:00 DB202 4 MB/sec server01: 2015-06-01T12:41:03-04:00 ASM 2 MB/sec server01: 2015-06-01T12:41:03-04:00 MYDB101 9 MB/sec server01: 2015-06-01T12:41:03-04:00 MYDB202 7 MB/sec server01: 2015-06-01T12:41:03-04:00 _OTHER_DB_ 50 MB/sec server02: 2015-06-01T12:40:03-04:00 DB101 90 MB/sec server02: 2015-06-01T12:40:03-04:00 DB202 9 MB/sec server02: 2015-06-01T12:40:03-04:00 ASM 2 MB/sec server02: 2015-06-01T12:40:03-04:00 MYDB101 3 MB/sec server02: 2015-06-01T12:40:03-04:00 MYDB202 1 MB/sec server02: 2015-06-01T12:40:03-04:00 _OTHER_DB_ 90 MB/sec server02: 2015-06-01T12:41:03-04:00 DB101 1 MB/sec server02: 2015-06-01T12:41:03-04:00 DB202 4 MB/sec server02: 2015-06-01T12:41:03-04:00 ASM 2 MB/sec server02: 2015-06-01T12:41:03-04:00 MYDB101 7 MB/sec server02: 2015-06-01T12:41:03-04:00 MYDB202 7 MB/sec server02: 2015-06-01T12:41:03-04:00 _OTHER_DB_ 55 MB/sec