#!/usr/local/bin/perl
#
# log_eator - log analyser for Wcol-E
#       by k-chinen@is.aist-nara.ac.jp, 1997
#
# $Id: log_report,v 1.1 1997/10/03 20:04:38 k-chinen Exp k-chinen $
#
# require
#   - perl 5.002 or later
#   - log file which generated by Wcol-E
#
#

use Math::BigInt;
require 'getopts.pl';


$bradix = Math::BigInt->new("2");
$kilo = $bradix ** 10;
$mega = $bradix ** 20;
$giga = $bradix ** 30;
$tera = $bradix ** 40;


#
# convert to kilo, mega, giga and tera
#
sub kilo {
    my($_target, $unit) = @_;
    my($target) = Math::BigInt->new($_target);
    my($v);
    $v = new Math::BigInt "0";
    $v = $target->babs;
    my($r);

    if($v> $tera) {
        $r = ($v / $tera)->bnorm . " T";
    }
    elsif($v>$giga) {
        $r = ($v / $giga)->bnorm . " G";
    }
    elsif($v>$mega) {
        $r = ($v / $mega)->bnorm . " M";
    }
    elsif($v>$kilo) {
        $r = ($v / $kilo)->bnorm . " K";
    }
    else {
        $r = $v . "  ";
    }

    $r =~ s#^\+##;
    $r .= $unit;
#   $r .= $unit . " (" . $target . ")";

    return $r;
}

$tradix = Math::BigInt->new("10");
$thousand = $tradix **  3;
$million  = $tradix **  6;
$billion  = $tradix **  9;
$trillion = $tradix ** 12;

### convert to thousand, million, billion and trillion
sub thou{
    my($_target, $unit) = @_;
    my($target) = Math::BigInt->new($_target);
    my($v);
    $v = new Math::BigInt "0";
    $v = $target->babs;
    my($r);

    if($v>$trillion) {
        $r = ($v / $trillion)->bnorm . " trillion";
    }
    elsif($v>$billion) {
        $r = ($v / $billion)->bnorm . " billion ";
    }
    elsif($v>$million) {
        $r = ($v / $million)->bnorm . " million ";
    }
    elsif($v>$thousand) {
        $r = ($v / $thousand)->bnorm . " thousand";
    }
    else {
        $r = $v . "         ";
    }

    $r =~ s#^\+##;
    $r .= $unit;
#   $r .= $unit . " (" . $target . ")";

    return $r;
}



######
######
######

$z_line         = Math::BigInt->new("0");

### HTTP
$z_hsess        = Math::BigInt->new("0");
$z_fetch        = Math::BigInt->new("0");
$z_prefetch     = Math::BigInt->new("0");
$z_send         = Math::BigInt->new("0");
$z_ferror       = Math::BigInt->new("0");
$z_perror       = Math::BigInt->new("0");
$z_ierror       = Math::BigInt->new("0");
$z_uerror       = Math::BigInt->new("0");
$z_hothers      = Math::BigInt->new("0");

$z_outtraffic   = Math::BigInt->new("0");
$z_intraffic    = Math::BigInt->new("0");

$zin  = 0;
$zout = 0;


### ICP
$z_isess        = Math::BigInt->new("0");
$z_notify       = Math::BigInt->new("0");
$z_query        = Math::BigInt->new("0");
$z_iothers      = Math::BigInt->new("0");
$z_ihit			= Math::BigInt->new("0");
$z_imiss		= Math::BigInt->new("0");


$manager_info  = 0;
$n_gc_start    = 0;
$n_gc_end      = 0;
# $n_launch = 0;
# $n_die      = 0;


while(<>) {
    @fis = split;
    $z_line++;

#   printf "%5d %s\n", $z_line, $fis[20];
    if($fis[5] eq ";") {
        next;
    }
    if($fis[6] eq "START") {
        $manager_info{"START"}++;
        next;
    }
    if($fis[6] eq "END") {
        $manager_info{"END"}++;
        next;
    }
    if($fis[6] eq "INFO") {
        $subp_info{$fis[6]}++;
        next;
    }
    if($fis[6] eq "GC-START") {
        $n_gc_start++;
        next;
    }
    if($fis[6] eq "GC-END") {
        $n_gc_end++;
        next;
    }


    if($fis[20] ne ":") {
        next;
    }

    if($fis[6] eq "PREFETCH") {
        $z_hsess++;
        $z_hrcode{$fis[16]}++;
        $z_hctype{$fis[17]}++;

        $z_prefetch++;

        $z_intraffic  += $fis[24];

        $zin  += $fis[18];
    }
    elsif($fis[9] eq "HTTP") {
        $z_hsess++;
        $z_hrcode{$fis[16]}++;
        $z_hctype{$fis[17]}++;

        $z_httpver{$fis[10]}++;

        if($fis[6] eq "FETCH") {
            $z_fetch++;

            $z_intraffic  += $fis[24];
            $z_outtraffic += $fis[18];

            $zin  += $fis[18];
            $zout += $fis[18];
        }
        elsif($fis[6] eq "SEND") {
            $z_send++;

            $z_outtraffic += $fis[18];

            $zout += $fis[18];
        }
        elsif($fis[6] eq "ERROR-PREFETCH") {
            $z_perror++;

            $z_outtraffic += $fis[18];

            $zout += $fis[18];
        }
        elsif($fis[6] eq "ERROR-FETCH") {
            $z_ferror++;

            $z_outtraffic += $fis[18];

            $zout += $fis[18];
        }
        elsif($fis[6] eq "ERROR-IMPORT") {
            $z_ierror++;

            $z_outtraffic += $fis[18];

            $zout += $fis[18];
        }
        else {
            $z_hothers++;
        }
    }
    elsif($fis[9] eq "ICP") {
        $z_isess++;

        if($fis[6] eq "NOTIFY") {
            $z_notify++;
        }
        elsif($fis[6] eq "QUERY") {
            $z_query++;
        }

        if($fis[16] eq "HIT") {
            $z_ihit++;
        }
        elsif($fis[16] eq "MISS") {
            $z_imiss++;
        }

    }

}


print  "== Overview\n";
printf "  Total of line   %10s : %20s\n", $z_line, &thou($z_line);
printf "  HTTP operations %10s : %20s\n", $z_hsess, &thou($z_hsess);
printf "  ICP operations  %10s : %20s\n", $z_isess, &thou($z_isess);


print  "== Processes\n";
printf "  Daemon Start/End      %4d / %4d\n",
        $manager_info{"START"}, $manager_info{"END"};
printf "  Subprocess Launch/Die %4d / %4d\n",
        $subp_info{"launch"}, $subp_info{"die"};
if(0) {
            while (($name, $value) = each %subp_info) {
                printf "\t%s %d\n", $name, $value;
            }
}
printf "  Garbege Collection    %4d / %4d\n", $n_gc_start, $n_gc_end;


print  "== HTTP Sessions\n";
print  "  Type\n";
printf "    Fetch          %10d %7.2f\n", $z_fetch,  100.0*$z_fetch/$z_hsess;
printf "    Prefetch       %10d %7.2f\n", $z_prefetch, 100.0*$z_prefetch/$z_hsess;
printf "    Send           %10d %7.2f\n", $z_send,   100.0*$z_send/$z_hsess;
printf "    Error Fetch    %10d %7.2f\n", $z_ferror, 100.0*$z_ferror/$z_hsess;
printf "    Error Prefetch %10d %7.2f\n", $z_perror, 100.0*$z_perror/$z_hsess;
printf "    Error Import   %10d %7.2f\n", $z_ierror, 100.0*$z_ierror/$z_hsess;
printf "    Error Others   %10d %7.2f\n", $z_uerror, 100.0*$z_uerror/$z_hsess;
printf "    Others         %10d %7.2f\n", $z_hothers,100.0*$z_hother/$z_hsess;
print  "    ---\n";
printf "    Total          %10d\n", $z_hsess;

print  "  Content-Type\n";
$_sum = 0; while(($name, $val) = each %z_hctype) { $_sum += $val; }
while(($name, $val) = each %z_hctype) {
  printf "    %-32s %10d %5.1f%%\n", "'".$name."'", $val, $val*100/$_sum;
}
print  "    ---\n";
printf "    %-32s %10d\n", "Total", $_sum;

print  "  Response-Code\n";
$_sum = 0; while(($name, $val) = each %z_hctype) { $_sum += $val; }
while(($name, $val) = each %z_hrcode) {
  printf "    %5d %10d %5.1f%%\n", $name, $val, $val*100/$_sum;
}
print  "    ---\n";
printf "    %-5s %10d\n", "Total", $_sum;


print  "== Traffic\n";
printf "  Incoming Traffic %14d : %14s\n",
    $z_intraffic, &kilo($z_intraffic,"Bytes");
printf "  Outgoing Traffic %14d : %14s\n",
    $z_outtraffic, &kilo($z_outtraffic,"Bytes");

# print "in $zin , out $zout\n";



print  "== ICP Sessions\n";
printf "  Notify   %10d\n", $z_notify;
printf "  Query    %10d\n", $z_query;
printf "      HIT  %10d %7.2f\n", $z_ihit, 100.0*$z_ihit/$z_query;
printf "      MISS %10d %7.2f\n", $z_imiss, 100.0*$z_imiss/$z_query;
print  "  ---\n";
printf "  Total    %10d\n", $z_isess;


if($z_fetch+$z_send+$z_ferror==0) {
printf "== Requests\n";
printf "    No reqeust\n";
}
else {
printf "== Request\n";
printf "    Hit    %10d %7.2f\n",
        $z_send, 100.0*$z_send/($z_fetch+$z_send+$z_ferror);
printf "    Miss   %10d %7.2f\n",
        $z_fetch, 100.0*$z_fetch/($z_fetch+$z_send+$z_ferror);
printf "    Error  %10d %7.2f\n",
        $z_ferror, 100.0*$z_ferror/($z_fetch+$z_send+$z_ferror);
print  "    ---\n";
printf "    Total  %10d\n", $z_fetch+$z_send+$z_ferror;
}

if($z_prefetch+$z_perror==0) {
printf "== Prefetch\n";
printf "    No prefetch\n";
}
else {
printf "== Prefetch\n";
printf "    Done   %10d %7.2f\n",
        $z_prefetch, 100.0*$z_prefetch/($z_prefetch+$z_perror);
printf "    Error  %10d %7.2f\n",
        $z_perror, 100.0*$z_perror/($z_prefetch+$z_perror);
print  "    ---\n";
printf "    Total  %10d\n", $z_prefetch+$z_perror;
}

