#! /usr/bin/env perl
##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************

use strict;
use warnings;
use Time::ParseDate;

# Update the module include path
BEGIN
{
    my $Dir = $0;
    if ( $Dir =~ /(.*)\/.*/ )
    {
	push @INC, "$1";
    }
}
use HawkeyePublish;
use HawkeyeLib;

# Setup the hawkeye stuff
my $Hawkeye;

# Setup
my %Config = (
	      LogFile => "",
	     );

# Do it
Init();
RunIt();

# Init logic
sub Init
{
    HawkeyeLib::DoConfig( );

    $Hawkeye = HawkeyePublish->new;
    $Hawkeye->Quiet( 1 );
    $Hawkeye->AutoIndexSet( 1 );

    my $Tmp;
    $Tmp = HawkeyeLib::ReadConfig( "_log_file", "" );
    $Config{LogFile} = $Tmp if ( $Tmp ne "" );
    die "No log file configured" if ( $Config{LogFile} eq "" );
}


# Do the real work here...
sub RunIt
{
    # Start things off
    my $Hash = HawkeyeHash->new( \$Hawkeye, "" );

    # Keep track of the latest
    my %Last = (
		Send => { Time => -1, },
		Recv => { Time => -1, },
	       );

    # Note when "now" is
    my $Now = time( );

    # Open & read the log file...
    my $Log = $Config{LogFile};
    ReadLog( $Now, $Log, \%Last ) || die "Can't read log file '$Log'\n";
    my $Old = $Config{LogFile} . ".old";
    ReadLog( $Now, $Old, \%Last ) || warn "Can't read log file '$Old'\n";

    # Compute the rates for the past hour
    if ( exists $Last{SendHour} )
    {
	$Last{SendHour}{Rate} = -1;		# Unknown
	if ( $Last{SendHour}{Seconds} )
	{
	    $Last{SendHour}{Rate} =
		( $Last{SendHour}{Bytes} / $Last{SendHour}{Seconds} );
	}
    }
    if ( exists $Last{RecvHour} )
    {
	my $Rate = -1;				# Unknown
	if ( $Last{RecvHour}{Seconds} )
	{
	    $Rate = ( $Last{RecvHour}{Bytes} / $Last{RecvHour}{Seconds} );
	}
	$Last{RecvHour}{Rate} = sprintf "%.2f", $Rate;
    }

    # Finally, publish the data...
    foreach my $What ( keys %Last )
    {
	foreach my $Key ( keys %{$Last{$What}} )
	{
	    $Hash->Add( $What . "_" . $Key,
			HawkeyePublish::TypeNumber,
			$Last{$What}{$Key} );
	}
    }

    # Ok, summary is done...
    $Hash->Store( );
    $Hawkeye->Publish( );
}

sub ReadLog( $$$ )
{
    my $Now = shift;
    my $Log = shift;
    my $Last = shift;

    open( LOG, $Log ) || die "Can't read log file '$Log'\n";
    while( <LOG> )
    {
	if ( /(\d+\/\d+ \d+:\d+:\d+)\s+(RECV|SEND) transferred (.*)/ )
	{
	    my $Time = parsedate( $1 );
	    my $What = ucfirst( lc ( $2 ) );
	    my $Details = $3;
	    my ( $Bytes, $Seconds, $Rate ) = split( /\D+/, $Details );

	    # Store it away if it's older...
	    if ( $Time > $Last->{$What}{Time} )
	    {
		$Last->{$What}{Time} = $Time;
		$Last->{$What}{Bytes} = $Bytes;
		$Last->{$What}{Seconds} = $Seconds;
		$Last->{$What}{Rate} = $Rate;
	    }

	    $What .= "Hour";
	    my $MaxTime = $Now - ( 60 * 60 );
	    if ( $Time >= $MaxTime )
	    {
		if ( ! exists $Last->{$What} )
		{
		    $Last->{$What}{Bytes} = 0;
		    $Last->{$What}{Seconds} = 0;
		    $Last->{$What}{Count} = 0;
		}
		$Last->{$What}{Bytes} += $Bytes;
		$Last->{$What}{Seconds} += $Seconds;
		$Last->{$What}{Count}++;
	    }
	}
    }
    close( LOG );
}
