#!/bin/bash

SCRIPTDIR="$(dirname "$(readlink -f ${BASH_SOURCE})")"
source "${SCRIPTDIR}/../../Macros"

#PROXY_NEEDED

#python library needed
export PYTHONPATH=/opt/lcg/lib/python/:/opt/lcg/lib/python2.4/site-packages/:/opt/lcg/lib/python2.4/site-packages/sos/plugins/:/opt/lcg/lib64/python/:/opt/lcg/lib64/python2.4/site-packages/:/opt/lcg/lib64/python2.4/site-packages/sos/plugins/:/usr/lib/python/:/usr/lib/python2.4/site-packages/:/usr/lib/python2.4/site-packages/sos/plugins/:/usr/lib64/python/:/usr/lib64/python2.4/site-packages/:/usr/lib64/python2.4/site-packages/sos/plugins/

export PATH=$PATH:/opt/lcg/lib64/nagios/plugins/lcgdm/:/opt/lcg/lib/nagios/plugins/lcgdm/:/usr/lib64/nagios/plugins/lcgdm/:/usr/lib/nagios/plugins/lcgdm/

#list of probes to check
nagios_probes="check_cpu check_rfio check_dpm_infosys check_gridftp check_dpm_perf check_hostcert check_dpm_pool check_network check_dpns check_partition_activity check_dpns_perf check_process"

results="OK WARNING CRITICAL"

#global variable for error
is_global_error=0


for probe in $nagios_probes
do
  #Parameter atribution
  if [ $probe = "check_process" ]
  then
    probe="$probe -p gridftp,rfiod" 
  elif [ $probe = "check_hostcert" ]
  then
    probe="$probe -C /home/edguser/.globus/usercert.pem"
  fi

  #test execution 
  echo "Running ${probe}"
  temp=$(eval "${probe}")
  echo ${temp}
  echo
  

  #local variable for error 
  is_error=1
  for result in $results
  do
    #No error if one of standart nagios outupt is found
    if echo $temp|grep $result >/dev/null 2>&1
    then
      is_error=0 
    fi
  done

  #If an error is found, print it
  if [ $is_error -eq 1 ]
  then
    echo $temp
    is_global_error=1 
  fi
done

#Check if errors are found
if [ $is_global_error -eq 0 ] 
then
  TEST_PASSED
else
  TEST_FAILED
fi
