#!/usr/bin/perl
use strict;
use warnings;

use EBox;
use EBox::Network::Report::ByteRate;

use constant {
  RRDTOOL_PATH => '/usr/bin/rrdtool',
};


sub _checkEnvironment
{
  if ( $> != 0) { # $> means EUID
    die 'only superuser can use this script';
  }
  
  if (not -x RRDTOOL_PATH) {
    die 'cannot found rrdtool program at ' . RRDTOOL_PATH;
  }

}


_checkEnvironment();


EBox::init();

# preparate termination
$SIG{TERM} = \&_terminate;
# prepate flush bps alarm
$SIG{ALRM} = \&_flushBps;
alarm (2); # start the flush timer

while (1) {
  my $line = <STDIN>;
  # XXX debug remove debug statement in production code
#  print "RECEIVED $line\n";

  chomp $line;
  my ($proto ,$src, $sport, $dst, $dport, $totalbps) = split ',', $line;
  $proto = lc $proto;
    
  # XXX debug remove debug statement in production code  
#  print "$proto $src:$sport -> $dst:$dport  : $totalbps\n";

  EBox::Network::Report::ByteRate::addBps(
				     proto => $proto,
				     src => $src,
				     sport => $sport,
				     dst => $dst,
				     dport => $dport,
				     bps   => $totalbps,
				    );


}


sub _flushBps
{
  EBox::Network::Report::ByteRate::flushBps();

  # restart the alarm
  alarm(2);
}


sub _terminate
{
  print "TERMINATE CALLED\n"; # XXX debug!!

  alarm(0); # deactivate the flus alarm
  sleep 1; # to assure we don't collide with the previous flush
  EBox::Network::Report::ByteRate::flushBps();
  
  # exit the program
  exit 0;
}

1;
