#!/usr/bin/ksh
#=============================================================================
# Nom            : TuxStatX
# Projet         : HP-Openview IT/Operations
# Cree le        : 28/06/1999
# Cree par       : S. GISLAIN
# Objet          : Recherche des serveurs Tuxedo.
#                : Attention : si des serveurs sont dupliques,
#                : on n'en verifie qu'un seul.
# Appel(s)       : HP-OV-ITO Template Tux_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 => ValeurStatus
#                : <>0 => KO
#=============================================================================
# Modification(s)
#-----------------------------------------------------------------------------
# JJ/MM/AA - QUI
#       QUOI
# 20/10/1999 - S.GISLAIN
#       Ajout a l'appel d'OPCMON d'un objet
#=============================================================================
# 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_TUX_LOCK=$(eval print \$""ITO_TUX_LOCK_${NumeroInstance})
typeset ITO_TUX_USER=$(eval print \$""ITO_TUX_USER_${NumeroInstance})
typeset ITO_TUX_INST=$(eval print \$""ITO_TUX_INST_${NumeroInstance})

# Fichiers temporaires de travail
typeset FicTemp1="/tmp/Ito_Tux_Temp_1.txt"
typeset FicTemp2="/tmp/Ito_Tux_Temp_2.txt"

# Definition des variables globales locales script:
typeset ValeurStatus=0          # Nb de processus trouves
typeset CommandeLs              # Commande ls en fonction de l'OS
typeset CommandePs              # Commande ps en fonction de l'OS
typeset CommandeTr		# Commande tr en fonction 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 => ValeurStatus
#             : 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
			CommandeTr="tr -ds"
                else
                        ReferenceFonction=FonctionHPUX
                        OPCMON=/usr/OV/bin/OpC/opcmon
			CommandeTr="tr -d"
                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"
				CommandeTr="tr -d"
                else
                                ReferenceFonction=FonctionSunOS
                                OPCMON=/usr/OV/bin/OpC/opcmon
                                CommandeLs="ls -l"
                                CommandePs="ps -axlww"
                                CommandePsUser="${CommandePs} | grep"
				CommandeTr="tr -d"
                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"
                CommandePsUser="ps -fu"
                CommandeLs="ls -g"
		CommandeTr='tr -d '
                ;;
        IRIX)
                ReferenceFonction=FonctionIRIX
                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 => ValeurStatus
#             : 2 => Erreur
#=============================================================================
function FonctionHPUX
{
        FonctionSolaris
        return 0
}

function FonctionHPUX10
{
        FonctionHPUX
        return 0
}

function FonctionSunOS
{
        FonctionSolaris
        return 0
}

function FonctionSolaris
{
	if [[ -f ${ITO_TUX_LOCK} ]]
	then
		ValeurStatus=1
		# ValeurStatus = 1 : les serveurs sont presents
		# ValeurStatus = 0 : au moins un serveur est KO, alerte !

		# on determine les numeros des serveurs Tuxedo a verifier.
		# pour cela, on constitue un fichier contenant tout les paraemtres de la
		# configuration.
		# On en extrait les numeros (SRVID) du WSL et des serveurs applicatifs

		su - ${ITO_TUX_USER} -c "tmunloadcf" > ${FicTemp1}

		ListeNumeros=$(grep SRVID ${FicTemp1} | awk -F"=" '{ print $3 }')

		# on cherche le nombre de groupes de TMS definis ...
		NbTms=$(grep -c TMSNAME ${FicTemp1})

		# on ecrit les serveurs tuxedo actifs dans le fichier /tmp/tux.inst
		su - ${ITO_TUX_USER} -c "tmadmin -r" > ${FicTemp2} 2>&1 << !!
pclt
psr
q
!!
		# Test WSH pas lances (gestion des connexions clients)

		if [ -z $(grep WSH ${FicTemp2}) ]
		then
			ValeurStatus=0
		else
			
			NbTmsReel=$(grep -c "^TMS" ${FicTemp2})

			if [[ $NbTms -gt $NbTmsReel ]]
			then
				ValeurStatus=0
			else
				for Numero in 0 ${ListeNumeros}
				do
					#on teste si le Numero est dans la liste des actifs

					if [[ $(grep -c ${Numero} ${FicTemp2}) -eq 0 ]]
					then
						ValeurStatus=0
					fi
				done
			fi
		fi
	else
		ValeurStatus=1
	fi
}

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
         
${OPCMON} "${NomTemplate}=${ValeurStatus}" -object "${ITO_TUX_INST}"

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