#!/usr/bin/perl

use strict;

my $serveur = "192.168.1.4";
my $graphe = "gigue_perte";
my $graphepng = $graphe . ".png";
my $rrd = $graphe . ".rrd";
my $iperf="/usr/bin/iperf";
my $pathlog = "/var/log";
my $log = "$pathlog/iperf_rrd.log";

my $rrdtool = "/usr/bin/rrdtool";
my %START = (
	     '2h'  => '7200',
	     'jour' => '86400',
	     'semaine' => '604800',
	     'mois' => '2678400',
	     'annee' => '32140800',
	     );

if ( ! -d $pathlog ){
  mkdir(0755, $pathlog) or die "Creation impossible de $pathlog !\n";
}
if ( ! -e $log ){
  open(LOG, ">$log") or die "Ecriture impossible de $log : $!\n";
  close LOG;
}

if ( ! -e $rrd ){
  # Creation de la base rrd
  debug("Creation de $rrd\n");

  #my $cmd = "$rrdtool create --start N --step 300 $rrd DS:gigue_vers_serveur:GAUGE:300:0:100 DS:perte_vers_serveur:GAUGE:300:0:100 DS:gigue_vers_client:GAUGE:300:0:100 DS:perte_vers_client:GAUGE:300:0:100 RRA:AVERAGE:0.5:1:1200 RRA:AVERAGE:0.5:1:1200 RRA:AVERAGE:0.5:1:1200 RRA:AVERAGE:0.5:1:1200";
  my $cmd = "$rrdtool create --start N --step 300 $rrd  DS:gigue_vers_serveur:GAUGE:300:0:500 DS:perte_vers_serveur:GAUGE:300:0:500 DS:gigue_vers_client:GAUGE:300:0:500 DS:perte_vers_client:GAUGE:300:0:500 RRA:AVERAGE:0.5:1:1200 RRA:AVERAGE:0.5:1:1200 RRA:AVERAGE:0.5:1:1200 RRA:AVERAGE:0.5:1:1200";
  system($cmd);
  if ( $? != "0" ){
       print "Erreur de la commande '$cmd'\n";
    exit 1;
  }
}


if ( $ARGV[0] eq "png" ){

  my $value = $ARGV[1];
  $value = "2h" if ! $value;
  my $start = "end-" . $START{$value} . "s";
 
  my $iperf_cmd = "$rrdtool graph $graphepng --end now --start $start -a PNG  -w 600 -h 250 --color CANVAS#000000 --color BACK#101010 --color FONT#ffffdf --color MGRID#337fbf --color GRID#615900 --color FRAME#808080 --color ARROW#FF0099     --title=\"Gigue (ms) et perte (%)\" 'DEF:probe1=$rrd:gigue_vers_serveur:AVERAGE' 'DEF:probe2=$rrd:perte_vers_serveur:AVERAGE' 'DEF:probe3=$rrd:gigue_vers_client:AVERAGE' 'DEF:probe4=$rrd:perte_vers_client:AVERAGE' 'LINE2:probe1#FF0000:gigue_vers_serveur' 'LINE2:probe2#00ff00:perte_vers_serveur' 'LINE2:probe3#ff00ff:gigue_vers_client'  'LINE2:probe4#0000ff:perte_vers_client'";
 debug("Execution de $iperf_cmd\n");
  my @result=`$iperf_cmd`;
  if ( $! ){
    print "Erreur: $!\n";
    exit 1;
  }
}
else {
# Execution de la commande iperf ( client)
  my $iperf_cmd = "$iperf -c $serveur -d -u -b16k";
  debug("Execution de $iperf_cmd\n");
  my @result=`$iperf_cmd`;
  if ( $! ){
    print "Erreur: $!\n";
    exit 1;
  }

  # Extraction des donnees du retour de la commande iperf
  my $t=0;
  my @gigue;
  my @perte;
  foreach my $l (@result){
    if ( $l =~ /\%/ ){
      my @w = split(/\s+/,$l);
      $gigue[$t] = $w[8]*1000;
      $perte[$t] = $w[12];
      $perte[$t] =~ s/\((.*)%\)/$1/;
      $t++;
    }
  }

  # Mise a jour de la base avec les donnees extraites de la commande iperf
  my $rrd_update = "$rrdtool update $rrd N:$gigue[0]:$perte[0]:$gigue[1]:$perte[1]";
  #my $rrd_update = "$rrdtool update $rrd N:$gigue[0]";
  debug("Execution de $rrd_update\n");
  my @result=`$rrd_update`;
  if ( $! ){
    print "Erreur: $!\n";
    exit 1;
  }
}

sub debug{
  my $msg = shift;

  open(LOG,">>$log") or die "Impossible d'écrire dans '$log' : $!\n";
  print LOG $msg;
  close LOG;
}
