#! /usr/local/perl/bin/perl -w

use strict;

#use lib qw( /usr/local/rrdtool/lib/perl ../lib/perl );

use RRDs;

my $RRD_DIR = "rrd";

my $last_update = min_last_rrd();

my $NB_VALUES = 12;

my ($stamp, $conn, $msgs, $to, $exes, $attach, $luser, $no_from, $no_to, $outlook, $abort, $close);

my ($cmd, @old, @new, @diff);

@old = @new = @diff = qw();


while ($#old  < $NB_VALUES) { push @old, 0; }
while ($#new  < $NB_VALUES) { push @new, 0; }
while ($#diff < $NB_VALUES) { push @diff, 0; }

foreach (<>) {
  chomp;
	(undef, @new) = split;
  next if $#new < 0;

  $stamp = shift(@new);
  while ($#new < $NB_VALUES)  {
		push @new, 0;
	}
  @old = @new if $stamp == $last_update;

  next if $stamp <= $last_update;

  my $i;
  for $i ( 0 .. $#new) {
		$diff[$i] = $new[$i] - $old[$i];
		$diff[$i] = 0 if $diff[$i] < 0;
	}
  @old = @new;
  ($conn, $msgs, $to, $exes, $attach, $luser, 
	 $no_from, $no_to, $outlook, $abort, $close, undef) = @diff;

  $cmd = sprintf ("%d:%d:%d:%d:%d:%d:%d",
									$stamp,
									$conn,
									$abort,
									$close,
									$msgs,
									$to,
									$attach);
  print "$cmd\n";

  RRDs::update ("$RRD_DIR/charge.rrd", 
		"--template=conn:abort:close:msgs:to:attach",
		$cmd);

  my $ERR = RRDs::error;
  die "ERREUR pendant m.a.j. de $RRD_DIR/charge.rrd : $ERR\n" if $ERR;

  $cmd = sprintf ("%d:%d:%d:%d:%d:%d",
									$stamp,
									$exes,
									$no_from,
									$no_to,
									$luser,
									$outlook);
  print "$cmd\n";

  RRDs::update ("$RRD_DIR/rejets.rrd", 
		"--template=exes:no_from:no_to:luser:outlook",
		$cmd);
 
  $ERR = RRDs::error;
  die "ERREUR pendant m.a.j. de $RRD_DIR/rejets.rrd : $ERR\n" if $ERR;
}

exit 0;

#   LAST  PID   Size Resident   Shared  Private  Process
# 




#################################
#
#
sub min_last_rrd {
  my $result = time();
  $result = RRDs::last ("$RRD_DIR/charge.rrd") if ( -f "$RRD_DIR/charge.rrd");
  $result = 0 if ! defined($result);
  return $result;
}

