#!/usr/local/gnu/bin/perl5
#mode: -*-perl-*-
require 5.002;
#
###############################################################
# This converts mrtg 1.x logfiles to 2.x format
###############################################################
# $Id: convert,v 2.0 1997/01/26 10:48:37 oetiker Exp $
#
# $Log: convert,v $
# Revision 2.0  1997/01/26 10:48:37  oetiker
# 2.0 release
#
#
#$debug=1

die "

Usage: convert  <logfile>

convert reads a mrtg 1.x <logfile>.log and converts it's contens
to mrtg 2.x format. The 1.x logfile gets stored in <logfile>.1.x-log

" unless (@ARGV && -R "$ARGV[0].log");


@step_width=(300,1800,7200,24*3600);
@number_of_steps=(600,600,600,732);

$total_steps=(600+600+600+732);

open(OLD,"<$ARGV[0].log") || die "Can't open $ARGV[0].log\n";

# get the first bytes ....

chop($ol_current=<OLD>);
chop($ol_inbytes=<OLD>);
chop($ol_outbytes=<OLD>);
chop($ol_lower=<OLD>);
&sanity;

open(NEW,">$ARGV[0].new.$$") || die "Can't open $ARGV[0].new\n";

$nl_first=int(time/$step_width[0])*$step_width[0];
$nl_current=$nl_first;
$nl_lower=$nl_current-$step_width[0];

# we trick rateup into beleving that the counter wrapped ...
# this gives a nice transition from old to new log ...

print NEW "$nl_current 999999999999999 999999999999999\n";

$nl_in=0;
$nl_out=0;


while(! eof OLD && $total_steps){
  # see wether we have to change gears

  if ( ($#step_width >= 1) &&
       ($nl_current <=  $nl_first - $step_width[0]*$number_of_steps[0]) &&

       # forget the clutch ... we do it at the right moment ...
       ($nl_current ==  int($nl_current/$step_width[1])*$step_width[1])
     )
    {
      shift @step_width;
      shift @number_of_steps;
      $nl_first = $nl_current;
      $nl_lower = $nl_current-$step_width[0];
      print "SHIFTING ... step: $step_width[0]\n";
    }
 
  # get the data ...


  #is part of the chunk still within our scope ?
  #     OOOOOOOOOOOOOOOOOOOOOOOOO
  # NOW           NNNNNNNNN
  if (($nl_lower>=$ol_lower) && ($nl_current<=$ol_current)) {
    $factor = $step_width[0]/($ol_current-$ol_lower);
   $nl_in += $ol_in* $factor;
    $nl_out += $ol_out* $factor;
  }

  #is part of the chunk still within our scope ?
  #                  OOOO
  # NOW           NNNNNNNNN
  elsif (($nl_lower<=$ol_lower) && ($nl_current>=$ol_current)) {
    $nl_in += $ol_in;
    $nl_out += $ol_out;
  }
  #is part of the chunk still within our scope ?
  #                  OOOOOOOOOOOOOOOOOOOOOOOOO
  # NOW           NNNNNNNNN
  elsif (($nl_lower>=$ol_lower) && ($nl_current>=$ol_current)) {
    $factor = ($ol_current-$nl_lower)* ($ol_current-$ol_lower);
    $nl_in += $ol_in* $factor;
    $nl_out += $ol_out* $factor;
  }
  #is part of the chunk still within our scope ?
  #          OOOOOOOOOOOOOOOOOOOOO
  # NOW                     NNNNNNNNN
  elsif (($nl_lower<=$ol_lower) && ($nl_current<=$ol_current)) {
    $factor = ($nl_current-$ol_lower)* ($ol_current-$ol_lower);
    $nl_in += $ol_in* $factor;
    $nl_out += $ol_out* $factor;
  }
  #                                      OOOOOOOOOOOOOOOOOOOOO
  # NOW                     NNNNNNNNN
  # is the gobeling sufficient, to move to the next slot nl?
  if ($nl_lower >= $ol_lower) {
    print NEW "$nl_current ".int($nl_in/$step_width[0])." ".
      int($nl_out/$step_width[0])."\n";
    $total_steps--;
    $nl_current=$nl_lower;
    $nl_lower -= $step_width[0];
    $nl_in=0;
    $nl_out=0;
    print "NL Step: $nl_current - $step_width[0]\n" if $debug;
  } 
  #

  #         OOOOOOOOOOOOOOOOOOOOO
  # NOW                       NNNNNNNNN
  # do we need more ol data to compleate the slot?
  elsif ($nl_lower <= $ol_lower) {
    $ol_current=$ol_lower;
    chop($ol_in=<OLD>);
    chop($ol_out=<OLD>);
    chop($ol_lower=<OLD>);
    &sanity;
    print "OL Step: $ol_current\n" if $debug;
  } 
  
}

close NEW;

rename "$ARGV[0].log", "$ARGV[0].1.x-log";
rename "$ARGV[0].new.$$","$ARGV[0].log";
 
print "Conversion successful ... A copy of the old logfile is in $ARGV[0].old\n";

exit 0;

sub sanity{ 
  if (($ol_lower >= $ol_current) ||
      ($ol_lower+0 != int($ol_lower))) {
    die ("\nERROR at line $.: ".
	 "$ARGV[0].log does NOT look like a 1.x mrtg logfile\n\n");
    close NEW;
    unlink "$ARGV[0].new.$$";
  }
}
