#!/usr/bin/perl

sub pad {
  my ($id, $len) = @_;
  if ($len <= length($id)) {
    return $id;
  }
  else {
    return $id.(" "x($len-length($id)));
  }
}

$threshold = 1;

%res = ();
while (<>) {
	chomp ;
	($day, $month, $date, $time, $zone, $year, $dash, $host, $ip, $outcome) = split /\s+/, $_, 10 ;
	($status == 0) || next ;
	$outcome =~ s/ /:/g;
	$outcome =~ s/=/:/g;
	#($rx, $to, $version, $rtt_unr, $reorder, $vc) = split /\s+/, $outcome;
	$id = join ':', $host, $ip;
	if (exists $res{$id}) {
		if ($res{$id}[0] ne $outcome) {
			$res{$id}[0] = -1;
			$res{$id}[1] = 0;
		}
		else {
			$res{$id}[1]++;
		}
	}
	else {
		$res{$id}[0] = $outcome; 
		$res{$id}[1]++;
	}
}

%osres = ();
%totres = ();
%totos = ();

$maxhost = 0;
$maxres = 0;
foreach $id (sort {$res{$b}[0] cmp $res{$a}[0]} keys %res) {
	if ($res{$id}[1] >= $threshold) {
		($srv, $ip) = split /:/, $id;
		if (length($srv) > $maxhost) {$maxhost = length($srv);};
		if (length($res{$id}[0]) > $maxres) {$maxres = length($res{$id}[0])};

		$resname = $res{$id}[0];
		$totres{$resname} ++;
	}
}

print "<pre>\n";

foreach $id (sort {$res{$b}[0] cmp $res{$a}[0]} keys %res) {
	if ($res{$id}[1] >= $threshold) {
		($srv, $ip) = split /:/, $id;
		$file = join '.', $srv, $ip; 
		printf ("%s %s %s\n",  pad($srv, $maxhost), pad($ip, 16), pad($res{$id}[0], $maxres));
	}
}

print "================================================\n";
print "================================================\n";
$x = 0;
foreach $resid (sort {$totres{$b} <=> $totres{$a}} keys %totres) {
	printf "%4d %s\n", $totres{$resid}, $resid;
	$x += $totres{$resid};
}
printf ("%4d Total\n", $x);

print "================================================\n";
print "what each result means:\n"; 
print "=====\n";
print "RST: reset\n"; 
print "NO_CONN: No connection\n"; 
print "sr=1: SYN/ACK received,\n";
print "se=1/0: ECN_ECHO was set/not set in the SYN/ACK,\n"; 
print "ar=1/0: Ack for the data packet (sent with ECT and CE bits set) received/not received,\n"; 
print "ae=1/0: the ack had ECN_ECHO bit set/not set.\n"; 
print "=====\n";

