#!/usr/bin/ksh
#####################################################################
# TITRE:	ESOpoller
#--------------------------------------------------------------------
# CREE LE:	03/10/97	Par: D.Stoll
#--------------------------------------------------------------------
# OBJET: 	Tester si le process poller tourne
#			
#====================================================================
# Modifie le:	JJ/MM/AA		Par: 
#	Motif: motif ayant justifie la modification et points
#	modifies si besoin
#####################################################################
# Environnement materiel:
#	Machine: 			Constructeur: HP,SUN,IBM,SGI
# Environnement logiciel:
#	OS: hp-ux			Version: 9.05
#	    hp-ux				 10.20
#	    Solaris				 5.3
#	    Solaris				 5.5
#	    SunOs 				 4.1.3
#	    AIX					 3.x
#	    AIX					 4.2
#	    IRIX				 5.3
# Initialisation des variables globales (environnement, reseau...):
#	VARIABLE1	=	
#	VARIABLE2	=
#####################################################################

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

# Variable de retour pour OPC
typeset NbProcess=0

# Definition du fichier temporaire
fic_temp=/tmp/ps_poller

# Definition des variables globales locales script:
typeset CommandeLs			  # Commande ls en fonction de l'OS
typeset CommandePs			  # 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

#####################################################################
# FONCTION: 	DeterminationOS	
# CREEE LE:	12/12/94		Par: Johann CURE HP
# OBJET:	Cette fonction permet de connaitre l'OS sur lequel
#		le script se deroule
#====================================================================
# Modifiee le:	12/12/96		Par:  Vincent Garreau HP
#	Motif: prise en compte de nouvelles plate-formes HP, AIX, SOLARIS, IRIX
#####################################################################
# Modifiee le:	JJ/MM/AA		Par: 
#	Motif: motif ayant justifie la modification et points
#	modifies si besoin
#####################################################################
# REMARQUES:
# 
#####################################################################
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"
		;;
	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"
		else	
				ReferenceFonction=FonctionSunOS
				OPCMON=/usr/OV/bin/OpC/opcmon
				CommandeLs="ls -l"
				CommandePs="ps -axlww"
		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"
		;;
	IRIX)
		ReferenceFonction=FonctionIRIX
		OPCMON=/opt/OV/bin/OpC/opcmon
		CommandePs="ps -edf"
		CommandeLs="ls -g"
		;;

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

#####################################################################
# FONCTION:     FonctionHP-UX
#		FonctionHP-UX10
#		FonctionSunOS 
#		FonctionSolaris
#		FonctionAIX
#		FonctionAIX42
#		FonctionIRIX
# CREEE LE:     12/12/94                Par: Johann CURE HP
# OBJET:        Ces 6 fonctions execute les instructions propres a
#		chaque systeme pour obtenir les memes resultats 
#====================================================================
# Modifiee le:  JJ/MM/AA                Par:
#####################################################################
# REMARQUES:	Recuperation du nombre de process MQueue
#
#####################################################################
# Fonctions DU SCRIPT appelees
#
#####################################################################


function FonctionHPUX
	{
	NbProcess=`grep -c poller ${fic_temp}`
	return 0
	}

function FonctionHPUX10
	{
	FonctionHPUX
	return 0
	}

function FonctionSunOS
	{
	FonctionHPUX
	return 0
	}

function FonctionSolaris
	{
        FonctionHPUX
	return 0
	}

function FonctionAIX
	{
        FonctionHPUX
	return 0
	}

function FonctionAIX42
	{
        FonctionHPUX
	return 0
	}

function FonctionIRIX
	{
        FonctionHPUX
	return 0
	}

####################################################################
#			PROGRAMME PRINCIPAL
####################################################################
DeterminationOS

${CommandePs} > ${fic_temp}

${ReferenceFonction}

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