#!/bin/ksh
#=============================================================================
# Nom            : GtiRecu
# Projet         : HP-Openview IT/Operations
# Cree le        : 14/03/2000
# Cree par       : S.GISLAIN
# Objet          : Decode les fichiers de config et les envoie vers le serveur
#                : de l'application GTI
# Appel(s)       : HP-OV-ITO Template Gti_Recu
# 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
# Codes retour   : = 0 => OK
#                : <>0 => KO
#=============================================================================
# Modification(s)
#-----------------------------------------------------------------------------
# JJ/MM/AA - QUI
#	QUOI
#=============================================================================
# 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 nom de la template associee
typeset NomTemplate=$1

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

# Fichiers contenu la configuration des templates de l'agent
typeset FichiersConfig="le msgi monitor trapi"

# Serveur de GTI
typeset ServeurGTI=138.21.58.100	# svtxoper
typeset RepertGTI=/users/exploit/gti/GTI/Agents

#=============================================================================
# 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" ] ; then
			ReferenceFonction=FonctionHPUX10
			OPCBIN=/opt/OV/bin/OpC
			OPCTMP=/var/opt/OV/tmp/OpC
			OPCCNF=/var/opt/OV/conf/OpC
			OPCTEM=/var/opt/OV/bin/OpC
		else
			ReferenceFonction=FonctionHPUX
			OPCBIN=/usr/OV/bin/OpC
			OPCTMP=/var/OV/tmp/OpC
			OPCCNF=/var/OV/conf/OpC
			OPCTEM=/var/OV/bin/OpC
		fi
		CommandeLs="ls -g"
		CommandePs="ps -edf"
		CommandePsUser="ps -fu"
		;;
	SunOS)
		if [ "$(echo $VersionOS|cut -c1,2)" = "5." ] ; then
				TypeMachine="Solaris"
				ReferenceFonction=FonctionSolaris
				OPCBIN=/opt/OV/bin/OpC
				OPCTMP=/var/opt/OV/tmp/OpC
				OPCCNF=/var/opt/OV/conf/OpC
				OPCTEM=/var/opt/OV/bin/OpC
				CommandeLs="ls -g"
				CommandePs="ps -edf"
				CommandePsUser="ps -fu"
		else	
				ReferenceFonction=FonctionSunOS
				OPCBIN=/usr/OV/bin/OpC
				OPCTMP=/var/OV/tmp/OpC
				OPCCNF=/var/OV/conf/OpC
				OPCTEM=/var/OV/bin/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
		OPCBIN=/usr/lpp/OV/OpC
		OPCTMP=/var/lpp/OV/tmp/OpC
		OPCCNF=/var/lpp/OV/conf/OpC
		OPCTEM=/var/lpp/OV/bin/OpC
		CommandePs="ps -edf"
		CommandePsUser="ps -fu"
		CommandeLs="ls -g"
		;;
	IRIX)
		ReferenceFonction=FonctionIRIX
		OPCBIN=/opt/OV/bin/OpC
		OPCTMP=/var/opt/OV/tmp/OpC
		OPCCNF=/var/opt/OV/conf/OpC
		OPCTEM=/var/opt/OV/bin/OpC
		CommandePs="ps -edf"
		CommandePsUser="ps -fu"
		CommandeLs="ls -g"
		;;

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

	OPCMON=${OPCBIN}/opcmon
	OPCUTIL=${OPCBIN}/utils
	OPCDCODE=${OPCUTIL}/opcdcode

	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 ${OPCTMP}/opclastd ]]
	then
		if [[ $(diff ${OPCTEM}/opclastd ${OPCTMP}/opclastd | awk 'END { print NR }') -eq 0 ]]
		then
			return 0
		fi
	fi

	cp ${OPCTEM}/opclastd ${OPCTMP}/opclastd
	
	typeset FichierLog="gti.log"
	typeset FichierOpcdcode="${OPCTMP}/${FichierLog}"
	typeset Host=$(uname -n)

	rm -f ${FichierOpcdcode}

	print "#============================================================================="  >> ${FichierOpcdcode} 2>&1

	print "# DATE : $(date '+%d/%m/%Y %H:%M:%S')\n" >> ${FichierOpcdcode} 2>&1

	print "#=============================================================================" >> ${FichierOpcdcode} 2>&1

	print "# UNAME : $(uname -a)\n" >> ${FichierOpcdcode} 2>&1

	print "#=============================================================================" >> ${FichierOpcdcode} 2>&1

	print "# HOST : $(grep ${Host} /etc/hosts | awk '{ print "# " $0 }')\n" >> ${FichierOpcdcode} 2>&1

	print "\n#-----------------------------------------------------------------------------" >> ${FichierOpcdcode} 2>&1
	print "# Fichier ${OPCTEM}/opclastd :\n" >> ${FichierOpcdcode} 2>&1

	awk '{ print "# " $0 }' ${OPCTEM}/opclastd >> ${FichierOpcdcode} 2>&1

	for Fichier in ${FichiersConfig}
	do
		print "\n#-----------------------------------------------------------------------------"
		print "# Fichier ${OPCCNF}/${Fichier} :"

		if [[ -f ${OPCCNF}/${Fichier} ]]
		then
			print "# $(ls -l ${OPCCNF}/${Fichier})\n"
			${OPCDCODE} ${OPCCNF}/${Fichier}
		else
			print "# Fichier inexistant !!!"
		fi
	done >> ${FichierOpcdcode} 2>&1

	print "\n#=============================================================================" >> ${FichierOpcdcode} 2>&1
###
	Adresse=$(grep ${Host} /etc/hosts | head -1 | awk '{ print $1 }' | awk -F"." '{ printf "%02x%02x%02x%02x", $1, $2, $3, $4 }')

	FichierDest="${Host}_${Adresse}"

	ftp -v -n ${ServeurGTI} > ${OPCTMP}/ftp.log 2>&1 <<EOFFTP
user ftp ftp
lcd ${OPCTMP}
!ls -la
delete ${FichierDest}.conf
put ${FichierLog} ${FichierDest}.conf
chmod 664 ${FichierDest}.conf
quit
EOFFTP

	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} 2>&1

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

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