#!/bin/bash
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2004.
# See http://www.eu-egee.org/partners/ for details on the copyright
# holders.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# AUTHORS: Gianni Pucciani, CERN
#          Alejandro Álvarez Ayllón, CERN
#          Dimitar Shiyachki <Dimitar.Shiyachki@cern.ch>
# 
# Ping test
#
##############################################################################

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

NO_PROXY_NEEDED

myerrorlevel=0
echo
echo "Target node is $DPM_HOST. Date: `date`"
echo "Testing the default DPM services:"

# Check nmap!
which nmap &> /dev/null
if [ $? -ne 0 ]; then
	echo "nmap is not in the path"
	echo "-TEST FAILED-"
	exit 1
fi


for service in "5001 rfiod" "3306 mysqld" "5010 dpnsdaemon" "5015 dpm"  "2170 bdii" "22 sshd" "8446 srmv2.2" "2811 globus-gridftp"; do
	port=`echo $service | cut -d " " -f 1`
	serv=`echo $service | cut -d " " -f 2`
	state=`nmap -P0 -sT -p $port --max_rtt_timeout 1000ms ${DPM_HOST} | grep "$port/tcp" | awk '{print $2}'`

	echo "  Testing service $serv on port $port"
	if [ ! "x$state" = "xopen" ]; then
		echo FAILURE
		myerrorlevel=1
	else
		echo SUCCESS
	fi
done
echo

echo "  Overall Service Ping test result: "
if [ $myerrorlevel -ne 0 ]; then
	TEST_FAILED
else
	TEST_PASSED
fi

