#!/usr/bin/ksh
#=============================================================================
# Nom            : AppAffpX
# Projet         : HP-Openview IT/Operations
# Cree le        : 18/09/2000
# Cree par       : T.DELPLANQUE
# Objet          : Affiche les processus APP
# Appel(s)       : HP-OV-ITO Template App_Proc_#
# 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
#
# 15/12/2001 - Tibo
#	Dans l annotation, on liste un fichier ${OPCTMP}/${TMPFILE} qui est 
#	le resultat (sortie standard) de l execution du monitor associe.
#
#=============================================================================
# Fonctions
#-----------------------------------------------------------------------------
#	DeterminationOS FonctionHPUX FonctionHPUX10 FonctionSunOS
#	FonctionSolaris FonctionAIX FonctionAIX42 FonctionIRIX
#=============================================================================
#
# Verification nombre d'arguments
if [ $# -lt 1 ]
then
	echo "Probleme sur le programme $0." 1>&2
	exit 1
fi

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

# Environnement ITO
typeset ITO_APP_INST=$(eval print \$""ITO_APP_INST_${NumeroInstance})
typeset ITO_APP_USER=$(eval print \$""ITO_APP_USER_${NumeroInstance})
typeset ITO_APP_LOCK=$(eval print \$""ITO_APP_LOCK_${NumeroInstance})
typeset ITO_APP_PROC=$(eval print \$""ITO_APP_PROC_${NumeroInstance})
typeset ITO_APP_PROJ=$(eval print \$""ITO_APP_PROJ_${NumeroInstance})
typeset TMPFILE="AppProcX_${ITO_APP_INST}.out"

# Definition des variables globales locales script:
typeset -i numero=1             # Sert a indexer les variables
typeset -i NbProcess=1          # Nb de processus trouves
typeset -i NBR_ARGUMENT=`print ${ITO_APP_PROC} | awk ' { print NF } '`
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

#=============================================================================
# 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
			OPCTMP=/var/opt/OV/tmp/OpC
		else
			ReferenceFonction=FonctionHPUX
			OPCMON=/usr/OV/bin/OpC/opcmon
			OPCTMP=/var/opt/OV/tmp/OpC
		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
				OPCTMP=/var/opt/OV/tmp/OpC
				CommandeLs="ls -g"
				CommandePs="ps -edf"
				CommandePsUser="ps -fu"
		else	
				ReferenceFonction=FonctionSunOS
				OPCMON=/usr/OV/bin/OpC/opcmon
				OPCTMP=/var/opt/OV/tmp/OpC
				CommandeLs="ls -l"
				CommandePs="ps -axlww"
				CommandePsUser="${CommandePs} | grep"
		fi
		;;
	AIX)
		if [ "$(echo $VersionOS|cut -c 3,3)" = "4" ] 
			then ReferenceFonction=FonctionAIX42
			else ReferenceFonction=FonctionAIX
		fi
		OPCMON=/usr/lpp/OV/OpC/opcmon
		OPCTMP=/var/lpp/OV/tmp/OpC
		CommandePs="ps -edf"
		CommandePsUser="ps -fu"
		CommandeLs="ls -g"
		;;
	IRIX)
		ReferenceFonction=FonctionIRIX
		OPCMON=/opt/OV/bin/OpC/opcmon
		OPCMON=/opt/OV/bin/OpC/opcmon
		CommandePs="ps -edf"
		CommandePsUser="ps -fu"
		CommandeLs="ls -g"
		;;

	*)	
		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
{
	if [[ -f ${ITO_APP_LOCK} ]]
	then
		print "\nProjet (pas forcement renseigne...) : ${ITO_APP_PROJ}"
		print "\nFichier de LOCK ${ITO_APP_LOCK} :\n\n"
		ls -l ${ITO_APP_LOCK}

                # 1ere ETAPE :
                # Construction de la liste des arguments -----------------
                for mot in ${ITO_APP_PROC}
                do
                        typeset MOT_${numero}=${mot}
                        let numero=numero+1
                done
                let numero=1
                # -------------------- fin construction -------------------



                # 2eme ETAPE :
                # Traitement de la liste ----------------------------------
                # On prend 3 arguments, on verifie si le 2eme est numerique:
                #
                # 1) Si c est le cas, alors le 1er est le nom du process, le 2nd
                #       est le mini et le 3eme est le maxi.
                #
                # 2) Si ce n est pas le cas, alors le 1er est le nom du process,
                #       et on initialise MIN et MAX a 1
                #
                # Ensuite il suffit de sauter a l argument 4 (pour le cas 1) ou
                # l argument 2 (pour le cas 2)

                while [ ${numero} -le ${NBR_ARGUMENT} ]
                do
                        typeset ARG1=`eval echo \$""MOT_${numero}`

                        let numero=numero+1
                        typeset ARG2=`eval echo \$""MOT_${numero}`

                        let numero=numero+1
                        typeset ARG3=`eval echo \$""MOT_${numero}`

                        typeset PROCESS=${ARG1}

                        if [ `echo ${ARG2} | grep ^[_0-9] | grep -v [a-zA-Z]` ]
                        then
                                typeset MIN=${ARG2}
                                typeset MAX=${ARG3}
                        else
                                typeset MIN=1
                                typeset MAX=1
                                let numero=numero-2
                        fi

                        if [[ -n ${PROCESS} && -n ${MIN} && -n {MAX} ]]
                        # si on a tout, on y va !!!
                        then
                                if [[ ${MIN} = "_" ]]
                                then
                                        typeset MIN=1
                                fi

                                if [[ ${MAX} = "_" ]]
                                then
                                        typeset MAX=5000
                                fi

                                typeset Alerte=$(${CommandePsUser} ${ITO_APP_USER} | grep "${PROCESS}" | grep ${ITO_APP_INST} | grep -v grep | awk 'END {print NR}')

                                if [[ ${Alerte} -lt ${MIN} || ${Alerte} -gt ${MAX} ]]
                                then
					echo " "
					echo "${Alerte} processus ${PROCESS} present(s) / Mini : ${MIN} / Maxi : ${MAX}"
					echo "Ce processus provoque une alerte."
					echo " "
				else
					echo "Pas de probleme pour le processus : ${PROCESS}."
                                fi

                                PROCESS=""
                                MIN=""
                                MAX=""

                        fi

                        let numero=numero+1
                done
                #--------------- Fin de traitement de la liste -----

        else
        # Le LOCK est absent, on ne verifie pas les process
		echo "Fichier de LOCK absent, on ne doit pas verifier la presence des processus."
        fi

	if [[ -f ${OPCTMP}/${TMPFILE} ]]
	then
		print "Fichier ${OPCTMP}/${TMPFILE} :\n"
		print "------------------------------------------------------------"
		cat ${OPCTMP}/${TMPFILE}
		print "------------------------------------------------------------"
	else
		print "Fichier ${OPCTMP}/${TMPFILE} absent."
	fi

	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
#=============================================================================
