#!/usr/bin/env perl

use strict;
use warnings;

my $bookkeeping;
my $flt = '[+-]?\d+(?:\.\d+)?(?:[eE][+-]?\d+)?';
my $pflt = "($flt)";
my $prefix = '^\[([^: ]+)(?::loba:\(\d+\))? ' . $pflt . '\] \[proc/INFO\] ';
my $initmatch = $prefix . 'Initial load: ' . $pflt;
my $finalmatch = $prefix . 'Final load after (\d+):(\d+) iterations: ' . $pflt;;
my $plainmatch = $prefix . '\((\d+):(\d+)\) current load: ' . $pflt;

my %alldata = ();

while (<>) {
    chomp;
    if (m{$plainmatch} or m{$finalmatch}) {
        my $host = $1;
        my $data = {
            time     => $2,
            lb       => $3,
            comp     => $4,
            load     => $5,
        };
#        print STDERR "PUSH $host $data->{time} $data->{load} (plain/final)\n";
        push @{$alldata{$host}}, $data;
    } elsif (m{$initmatch}) {
        my $host = $1;
        my $data = {
            time     => $2,
            lb       => 0,
            comp     => 0,
            load     => $3,
        };
#        print STDERR "PUSH $host $data->{time} $data->{load} (init)\n";
        push @{$alldata{$host}}, $data;
    }
}

foreach my $host (sort(keys %alldata)) {
#    print STDERR "GOT \"$host\"\n";
    my $datalist = $alldata{$host};
    print "# $host\n";
    foreach my $data (@{$datalist}) {
        print "$data->{time} $data->{load}\n";
    }
    print "\n"
}
