#! %PERL%
#
# $Id: ip6-plot-data.pl,v 1.5 2014/06/21 16:54:31 he Exp $
#
# Siple script to output percentage of ipv6 packets on
# input and output for a given day.
#
# Usage: perl ip6-plot-data.pl -d <datespec> <data filename>
#

use Getopt::Std;

use lib "%LIBDIR%";
require "read-raw-log.pl";
require "date.pl";


use strict;

#
# Main
#

our($opt_d);

&getopts("d:");

my($f) = $ARGV[0];
my($IN);
open($IN, $f) || die "Could not open $f for read: $!";
my($tm, $nd) = &decode_datespec($opt_d);

our(%count, $max_sample, %sample_time);

&read_log($IN, $tm);

for(my $i = 0; $i <= $max_sample; $i++) {
    my($ipkts, $opkts, $iperc, $operc);
    my($i6pkts, $o6pkts);
    my($ioctets, $ooctets);

    if (defined($count{"ipIfStatsHCInOctets",$i})) {
	$ioctets = $count{"ipIfStatsHCInOctets",$i};
	$ooctets = $count{"ipIfStatsHCOutOctets",$i};

	printf("%07.4f %f %f\n",
	       $sample_time{$i},
	       $ioctets, $ooctets);
    } else {
	$ipkts =
	    $count{"ifInUcastPkts",$i} +
	    $count{"ifInNUcastPkts",$i};

	$opkts =
	    $count{"ifOutUcastPkts",$i} +
	    $count{"ifOutNUcastPkts",$i};

	$i6pkts = $count{"ipv6IfStatsInReceives",$i};
	$o6pkts = $count{"ipv6IfStatsOutForwDatagrams",$i};

	$ioctets = $count{"ifHCInOctets",$i};
	$ooctets = $count{"ifHCOutOctets",$i};

	if ($ipkts != 0) {
	    $iperc = $i6pkts * 100.0 / $ipkts;
	} else {
	    $iperc = 0;
	}

	if ($opkts != 0) {
	    $operc = $o6pkts * 100.0 / $opkts;
	} else {
	    $operc = 0;
	}

	printf("%07.4f %f %f\n",
	       $sample_time{$i},
	       $ioctets * $iperc / 100.0,
	       $ooctets * $operc / 100.0
	    );
    }
}
