#!/usr/bin/ksh
#=============================================================================
# Nom            : PngSldX
# Projet         : HP-Openview IT/Operations
# Cree le        : 26/07/2000
# Cree par       : T. DELPLANQUE
# Objet          : Effectue un ping sur une liste de serveur contenu dans
#			/logiciel/sysload/coll/conf/sldsmd-init
# Appel(s)       : HP-OV-ITO Template (LogFile) Png_Sld
# Environnements : HP-UX 9.05 10.20
#                : Solaris 5.3 5.4 5.5 5.5.1 5.6
#                : SunOs 4.1.3
#                : Aix 3.x 4.2 4.3
#                : Irix 5.3
# Parametre(s)   : Pas de parametre
#                : 
# Codes retour   : = 0 => OK
#                : <>0 => KO
#=============================================================================
# Modification(s)
#-----------------------------------------------------------------------------
# JJ/MM/AA - QUI
#	QUOI
#=============================================================================
# Fonctions
#-----------------------------------------------------------------------------
#	DeterminationOS FonctionHPUX FonctionHPUX10 FonctionSunOS
#	FonctionSolaris FonctionAIX FonctionAIX42 FonctionIRIX
#=============================================================================
#

# Definition des variables globales locales script:
typeset FICHIER_AGENT="/logiciel/sysload/coll/conf/sldsmd-init"
typeset LOG="/var/log/PngSld.log"
typeset TEMP="/tmp/PngSld.tmp"
typeset MESSAGE="-"
typeset EXCEPTION="/usr/local/bin/servtec/PingAgent.sauf"
typeset TypeMachine		# Recuperation de l'OS  	
typeset VersionOS		# Recuperation de la version de l'OS 
typeset ReferenceFonction	# Commande differente suivant l'OS

#=============================================================================
# DeterminationOS	
# Reconnaitre l'OS sur lequel le script se deroule
# Code retour : 0 => OK
#             : 2 => Erreur
#=============================================================================
function DeterminationOS
{
	TypeMachine=$(uname -s)           # Recuperation de l'OS  	
	VersionOS=$(uname -rv)            # Recuperation de la version de l'OS 

	case ${TypeMachine} in
	HP-UX)
		if [[ "$(echo $VersionOS|cut -d. -f2)" = "10" || "$(echo $VersionOS|cut -d. -f2)" = "11" ]] ; then
			ReferenceFonction=FonctionHPUX10
		else
			ReferenceFonction=FonctionHPUX
		fi
		;;
	SunOS)
		if [ "$(echo $VersionOS|cut -c1,2)" = "5." ] ; then
				TypeMachine="Solaris"
				ReferenceFonction=FonctionSolaris
		else	
				ReferenceFonction=FonctionSunOS
		fi
		;;
	AIX)
		if [ "$(echo $VersionOS|cut -c 3,3)" = "4" ] 
			then ReferenceFonction=FonctionAIX42
			else ReferenceFonction=FonctionAIX
		fi
		;;
	IRIX)
		ReferenceFonction=FonctionIRIX
		;;

	*)	
		print "$TypeMachine non supporte par ce programme"
		exit 2
		;;
	esac
	return 0
}

#=============================================================================
# FonctionHP-UX FonctionHP-UX10 FonctionSunOS FonctionSolaris FonctionAIX
# FonctionAIX42 FonctionIRIX
# Ces 6 fonctions execute les instructions propres a chaque systeme pour
# obtenir les memes resultats 
# Code retour : 0 => OK
#             : 2 => Erreur
#=============================================================================
function FonctionHPUX
{
	FonctionSolaris
	return 0
}

function FonctionHPUX10
{
	FonctionHPUX
	return 0
}

function FonctionSunOS
{
	FonctionSolaris
	return 0
}

function FonctionSolaris
{
	cat ${FICHIER_AGENT} | while read Ligne
	do
		MESSAGE="-"
		Agent=$(print "${Ligne}" | grep "@" | grep -v "#" | cut -d"@" -f1)
		if [[ $(echo ${Agent}) != "" ]] && [[ $(grep -c ${Agent} ${EXCEPTION}) -eq 0 ]]
		then
			/usr/sbin/ping ${Agent} 64 10 > ${TEMP} 2>&1
			Status=$?

			if [[ ${Status} -ne 0 ]]
			# Pb avec le script
			then
				MESSAGE="`date \"+%b %d %T\"` opcpdjc0 Ping[00]: not_a_tty root /var/opt/OV/bin/OpC/monitor/PngSldX Ping 00 2 PING 00 Probleme dans le script PngSldX qui ping les serveurs Netware"
			fi
	
			if [[ $(wc -l ${TEMP} | awk '{print $1}') -lt 15 ]]
			# Probleme de ping
			then
				MESSAGE="`date \"+%b %d %T\"` opcpdjc0 ${Agent}[00]: not_a_tty root /var/opt/OV/bin/OpC/monitor/PngSldX Ping 00 1 PING 00 Ping impossible du noeud : ${Agent}"
			fi

			rm -f ${TEMP}

		fi

		if [[ $(echo ${MESSAGE}) != "-" ]]
		then echo ${MESSAGE} >> ${LOG}
		fi
	done

	return 0
}

function FonctionAIX
{
	FonctionSolaris
	return 0
}

function FonctionAIX42
{
	FonctionAIX
	return 0
}

function FonctionIRIX
{
	FonctionSolaris
	return 0
}

#=============================================================================
# Partie principale
#
DeterminationOS

if [ $? -ne 0 ]
then
	echo "Probleme sur le programme $0." 1>&2
	exit 1
fi
	 
${ReferenceFonction}

if [ $? -ne 0 ]
then
	echo "Probleme sur le programme $0." 1>&2
	exit 1
fi

exit 0
#
# eof
#=============================================================================
