#!/usr/bin/ksh
#=============================================================================
# Nom            : CftStatX
# Projet         : HP-Openview IT/Operations
# Cree le        : 14/05/1999
# Cree par       : S. GISLAIN/ JP. JUGE
# Objet          : Recherche les processus CFT et effectue un envoi.
# Appel(s)       : HP-OV-ITO Template Cft_Stat_#
# 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)   : 1 : nom du template d'appel
#                : 2 : numero de l'instance #
# Codes retour   : = 0 => OK
#                : <>0 => KO
#=============================================================================
# Modification(s)
#-----------------------------------------------------------------------------
# JJ/MM/AA - QUI
#       QUOI
# 21/05/1999 - S. GISLAIN
#       Nombre de processus par defaut calcule'
#
#=============================================================================
# Fonctions
#-----------------------------------------------------------------------------
#       DeterminationOS FonctionHPUX FonctionHPUX10 FonctionSunOS
#       FonctionSolaris FonctionAIX FonctionAIX42 FonctionIRIX
#=============================================================================
#
# Verification nombre d'arguments
if [ $# -lt 2 ]
then
        echo "Probleme sur le programme $0." 1>&2
        exit 1
fi

# Recuperation du nom de la template associee
typeset NomTemplate=$1

# Recuperation du numero de l'instance
typeset NumeroInstance=$2

# Environnement ITO
typeset ITO_CFT_USER=$(eval print \$""ITO_CFT_USER_${NumeroInstance})
typeset ITO_CFT_LOCK=$(eval print \$""ITO_CFT_LOCK_${NumeroInstance})
typeset ITO_CFT_PROC=$(eval print \$""ITO_CFT_PROC_${NumeroInstance})
typeset ITO_CFT_X25=$(eval print \$""ITO_CFT_X25_${NumeroInstance})

# Variable de retour pour OPC NbProcess: Nb de process tournant
typeset NbProcessus=$(print ${ITO_CFT_PROC} | wc -w)

# Definition des variables globales locales script:
typeset NbProcess=0             # Nb de processus trouves
typeset CommandeLs              # Commande ls en fonction de l'OS
typeset CommandePs              # Commande ps en focntion de l'OS
typeset CommandePsUser          # Commande ps en focntion de l'OS
typeset OPCMON                  # PATH de la commande opcmon
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
			OPCMON=/opt/OV/bin/OpC/opcmon
		else
			ReferenceFonction=FonctionHPUX
			OPCMON=/usr/OV/bin/OpC/opcmon
		fi
		CommandeLs="ls -g"
		CommandePs="ps -edf"
		CommandePsUser="ps -fu"
		;;
	SunOS)
		if [ "$(echo $VersionOS|cut -c1,2)" = "5." ] ; then
			TypeMachine="Solaris"
			ReferenceFonction=FonctionSolaris
			OPCMON=/opt/OV/bin/OpC/opcmon
			CommandeLs="ls -g"
			CommandePs="ps -edf"
			CommandePsUser="ps -fu"
		else	
			ReferenceFonction=FonctionSunOS
			OPCMON=/usr/OV/bin/OpC/opcmon
			CommandeLs="ls -l"
			CommandePs="ps -axlww"
			CommandePsUser="ps -fu"
		fi
		;;
	AIX)
		if [ "$(echo $VersionOS|cut -c 3,3)" = "4" ] 
			then ReferenceFonction=FonctionAIX42
			else ReferenceFonction=FonctionAIX
		fi
		OPCMON=/usr/lpp/OV/OpC/opcmon
		CommandePs="ps -edf"
		CommandeLs="ls -g"
		CommandePsUser="ps -fu"
		;;
	IRIX)
		ReferenceFonction=FonctionIRIX
		OPCMON=/opt/OV/bin/OpC/opcmon
		CommandePs="ps -edf"
		CommandeLs="ls -g"
		CommandePsUser="ps -fu"
		;;

	*)	
		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
{
	if [[ -f ${ITO_CFT_LOCK} ]]
	then
		let NbProcess=0
		for Processus in ${ITO_CFT_PROC}
		do
			if [[ $(${CommandePsUser} ${ITO_CFT_USER} | grep ${Processus} | grep -v grep | awk 'END {print NR}') -ge 1 ]]
			then
				let NbProcess=NbProcess+1
			fi
		done

		if [[ ${NbProcess} -lt ${NbProcessus} ]]
		then
			NbProcess=0
		fi

		su - ${ITO_CFT_USER} -c "CFTUTIL send part=boucltcp,idf=AUTOSURV" > /dev/null 2>&1

		if [[ ${ITO_CFT_X25} = "O" ]]
		then
			su - ${ITO_CFT_USER} -c "CFTUTIL send part=bouclx25,idf=AUTOSURV" > /dev/null 2>&1
		fi
	else
		NbProcess=$(print ${NbProcessus})
	fi
	
	return 0
}

function FonctionHPUX10
{
	FonctionHPUX
	return 0
}

function FonctionSunOS
{
	FonctionHPUX
	return 0
}

function FonctionSolaris
{
	FonctionHPUX
	return 0
}

function FonctionAIX
{
	FonctionHPUX
	return 0
}

function FonctionAIX42
{
	FonctionAIX
	return 0
}

function FonctionIRIX
{
	FonctionHPUX
	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
	 
${OPCMON} "${NomTemplate}=${NbProcess}"

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