#!/usr/bin/perl
use strict;
#use lib "/usr/local/groundwork/nagios/libexec/";
use Newtestlib;
use Cwd;



                 #########################################################################################
       		 #                                                                                       #
       		 #                               Plugin Robot Version 1                                  #
       		 #                                                                                       #
        	 #########################################################################################


##########################################################################################################
our %ERRORS=('OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4);
my $date;#%message;
my $cpt=0;#$i=0;
my @tab1;#@a;
my @tab2;#@b;
my $index_rbt;#$v=0;#contain the index of the robot in the line which contain the right information (in the pooler file)
my$x=0;
my $config_file;
my $case;#$cas;
my $nmc;
my @param;
my $file_pooler;#$file;
my $log;
my $length_file_pooler;#$t;
my $rbt;#$z;#robot
my($line,@words,$word,%total);
my $length_rbt_name;#$len
my $current_dir;#$arg;
my $name_plug_rbt;#$name1;
my $name_plug_rbt_sce;#$exec_name1;
my $road_sce;#$ar;#the road to obtain the plugin scenario
my @state_nagios;
my $passif_arg;#the passif arugument (default : = p )
my @suffix_preffix_file_pooler;# $suffix_preffix_file_pooler[0]=suffix , $suffix_preffix_file_pooler[1]=preffix
my @suffix_preffix_trace_plug_rbt;# $suffix_preffix_trace_plug_rbt[0]=suffix , $suffix_preffix_trace_plug_rbt[1]=preffix
my $word_for_robot;
my @rbt_state;
my $param_ok;#to know if all parametrs are ok
#######################################################################################################################################
$param_ok=Newtestlib::more_param($ARGV[0],$ARGV[1],$ARGV[2],$ARGV[3],$ARGV[4]);
if(!($param_ok eq "")){ exit;}

else{

if (not -e $ARGV[0] )
	{
	print("config file ".$ARGV[0]." doesn t exist \n");
	exit;
        }

$config_file=$ARGV[0];#le chemin et nom du fichier de configuration newtest_config.cfg
$case= $ARGV[1];#p ou a
$nmc= $ARGV[2];#l'adresse du nmc   $file_r
$rbt=$ARGV[3]; #nom du robot
$rbt=uc($rbt);
 #$ARGV[4] le nom du scenario pour le cas actif ,le nom de l'host pour le cas passif
$passif_arg=Newtestlib::get_passif_arg();
$nmc=~ s/\./_/g;
Newtestlib::get_config_file($ARGV[0]);
@param=Newtestlib::get_param();
@suffix_preffix_file_pooler=Newtestlib::get_suffix_preffix_file_pooler();
$file_pooler=$param[2].$suffix_preffix_file_pooler[0].$nmc.$suffix_preffix_file_pooler[1];
@suffix_preffix_trace_plug_rbt=Newtestlib::get_suffix_preffix_trace_plug_rbt();
$log=$param[2].$suffix_preffix_trace_plug_rbt[0].$nmc.$suffix_preffix_trace_plug_rbt[1];
open(LOG,">>$log") || print " error when opening the file $log\n";
print LOG "			******$ARGV[3]:Newtest_plugin_rbt.pl*******\n";
START: open(FILE,$file_pooler) || print " error when opening the file $file_pooler\n";
$length_file_pooler= -s $file_pooler;
if($length_file_pooler == 0 || !(-e $file_pooler)){ print LOG "$ARGV[3]:UNKNOWN:file is empty please wait";
                           close(FILE);
                           goto START;
                           }
else{
 $word_for_robot=Newtestlib::get_word_for_robot();
while( defined( $line = <FILE> ) )
{
   @words = split( /\|\|+/, $line );  #recuperer une ligne
   @tab1=@words;
   foreach $word (@words)
   {
   $tab2[$cpt]=$word;
        if($tab2[$cpt-1] eq "$word_for_robot" && $word eq "$rbt"){
        $index_rbt=$cpt;
        }

       $cpt++;
   }
}
$date=$tab2[$index_rbt+2];
close(FILE);
$date=~ s/\n+/ /;
$length_rbt_name=length($tab2[$index_rbt]);
if(($length_rbt_name<length($rbt)) || !(defined($tab2[$index_rbt+1]))){
	                                        			print  ("error: the name of robot $rbt don't exist");
	                                       				print LOG ("error: the name of robot $rbt don't exist");
	                                                              }

#################APPEL PLUGIN SCE en mode passif

if( $ARGV[1] eq "$passif_arg" ){

$current_dir=$0;
$road_sce=Newtestlib::exec_plug_sce_passif($current_dir);
system "perl \"$road_sce\" $ARGV[0] $ARGV[1] $ARGV[2] $ARGV[3] $ARGV[4] 1>> $param[2]passif.log 2>&1";
print LOG ("$ARGV[3]:execution passive sce,with:perl \"$road_sce\" $ARGV[0] $ARGV[1] $ARGV[2] $ARGV[3] $ARGV[4]\n");

}

##################################
@state_nagios= Newtestlib::get_nagios_rbt_state($tab2[$index_rbt+1]);
@rbt_state=Newtestlib::get_rbt_state();

if ($tab2[$index_rbt+1] eq $rbt_state[0])#'OK')
          	{
	                print "$state_nagios[0]:probe newtest is running, last message date is at $date|Status=$state_nagios[2];;;;\n";
                        print LOG "$ARGV[3]:$state_nagios[0]:probe newtest is running, last message date is at $date|Status=$state_nagios[2];;;;\n";
	                exit $ERRORS {"$state_nagios[0]"};
                }
elsif ($tab2[$index_rbt+1] eq $rbt_state[1])#'SUSPENDED')
        	{ print "$state_nagios[0]:probe newtest is suspended,last message date is at $date|Status=$state_nagios[2];;;;\n";
                  print LOG "$ARGV[3]:$state_nagios[0]:probe newtest is suspended,last message date is at $date|Status=$state_nagios[2];;;;\n";
                  exit $ERRORS {"$state_nagios[0]"};
                }

elsif ($tab2[$index_rbt+1] eq $rbt_state[2])#'STOPPED')
           	{print "$state_nagios[0]:probe newtest is $tab2[$index_rbt+1], last message date is at $date|Status=$state_nagios[2];;;;\n";
                 print LOG "$ARGV[3]:$state_nagios[0]:probe newtest is $tab2[$index_rbt+1], last message date is at $date|Status=$state_nagios[2];;;;\n";
                 exit $ERRORS {"$state_nagios[0]"};
               }

elsif ($tab2[$index_rbt+1] eq $rbt_state[3])#'UNKNOWN')
              	{print "$state_nagios[0]:probe newtest is $tab2[$index_rbt+1], last message date is at $date|Status=$state_nagios[2];;;;\n";
                 print LOG "$ARGV[3]:$state_nagios[0]:probe newtest is $tab2[$index_rbt+1], last message date is at $date|Status=$state_nagios[2];;;;\n";
                 exit $ERRORS{"$state_nagios[0]"};
                }

elsif ($tab2[$index_rbt+1] eq $rbt_state[4])#'UNINSTALLED')
         	{ print "$state_nagios[0]:probe newtest is $tab2[$index_rbt+1], last message date is at $date|Status=$state_nagios[2];;;;\n";
                  print LOG "$ARGV[3]:$state_nagios[0]:probe newtest is $tab2[$index_rbt+1], last message date is at $date|Status=$state_nagios[2];;;;\n";
                  exit $ERRORS{"$state_nagios[0]"};
                }
elsif ($tab2[$index_rbt+1] eq $rbt_state[5])#'INOPERATIVE')
        	{ print "$state_nagios[0]:probe newtest  is $tab2[$index_rbt+1], last message date is at $date|Status=$state_nagios[2];;;;\n";
                  print LOG "$ARGV[3]:$state_nagios[0]:probe newtest  is $tab2[$index_rbt+1], last message date is at $date|Status=$state_nagios[2];;;;\n";
                  exit $ERRORS{"$state_nagios[0]"};
                 }
elsif ($tab2[$index_rbt+1] eq $rbt_state[6])#'RESUME')
            	{ print "$state_nagios[0]:probe newtest is $tab2[$index_rbt+1], last message date is at $date|Status=$state_nagios[2];;;;\n";
      print LOG "$ARGV[3]:$state_nagios[0]:probe newtest is $tab2[$index_rbt+1], last message date is at $date|Status=$state_nagios[2];;;;\n";               exit $ERRORS{"$state_nagios[0]"};
                 }
}
}