#!/usr/bin/ksh
#=============================================================================
# Nom            : DskStmp
# Projet         : HP-Openview IT/Operations
# Cree le        : 24/03/2000
# Cree par       : S. GISLAIN
# Objet          : Script verifiant l'occupation de /tmp/
# Appel(s)       : HP-OV-ITO Template Dsk_Stmp_#
# 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 2 ]]
then
	print "Probleme sur le programme $0." 1>&2
	exit 1
fi

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

# Pourcentage d'alerte
typeset Pourcentage=$2

# Nb de filesystem dont l'occupation est au dessus du seuil $Pourcentage
typeset NbFilesystem=0

# Definition des variables globales locales script:
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
typeset Inclus="/tmp"			  # Filesystem(s) a surveiller

#=============================================================================
# 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=/usr/opt/OV/tmp/OpC
		fi

		CommandeLs="ls -g"
		CommandePs="ps -edf"
		CommandePsUser="ps -fu"
		CommandeDf_l="bdf -l"
		CommandeAwk_v="awk -v"
		;;
	SunOS)
		if [ "$(print $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"
				CommandeDf_l="df -kl"
				CommandeAwk_v="nawk -v"
		else	
				ReferenceFonction=FonctionSunOS
				OPCMON=/usr/OV/bin/OpC/opcmon
				OPCTMP=/usr/opt/OV/tmp/OpC
				CommandeLs="ls -l"
				CommandePs="ps -axlww"
				CommandePsUser="${CommandePs} | grep"
				CommandeDf_l="df -l"
				CommandeAwk_v="nawk -v"
		fi
		
		;;
	AIX)
		if [ "$(print $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"
		CommandeDf_l="df"
		CommandeAwk_v="awk -v"
		;;
	IRIX)
		ReferenceFonction=FonctionIRIX
		OPCMON=/opt/OV/bin/OpC/opcmon
		OPCTMP=/var/opt/OV/tmp/OpC
		CommandePs="ps -edf"
		CommandePsUser="ps -fu"
		CommandeLs="ls -g"
		CommandeDf_l="df -kl"
		CommandeAwk_v="nawk -v"
		;;

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

	return $?
}

#=============================================================================
# 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
#             : 1 => Erreur
#=============================================================================
function FonctionHPUX
{
	FonctionSolaris

	return $?
}

function FonctionHPUX10
{
	FonctionHPUX

	return $?
}

function FonctionSunOS
{
	FonctionSolaris

	return $?
}

function FonctionSolaris
{
	${CommandeDf_l} | \
		egrep " ${Inclus}$" | \
		sed 's/%//g' | \
		${CommandeAwk_v} pourcent=$Pourcentage '$5 >= pourcent { print $6 " " $5 }' \
		> ${FichierActuel}

	return $?
}

function FonctionAIX
{
	${CommandeDf_l} | \
		egrep " ${Inclus}$" | \
		sed 's/%//g' | \
		${CommandeAwk_v} pourcent=$Pourcentage '$4 >= pourcent { print $7 " " $4 }' \
		> ${FichierActuel}

	return $?
}

function FonctionAIX42
{
	FonctionAIX

	return $?
}

function FonctionIRIX
{
	FonctionSolaris

	return $?
}

#=============================================================================
# Partie principale
#
DeterminationOS

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

# Fichiers de surpervision
typeset FichierPrecedent=${OPCTMP}/DskStmp_Precedent.txt
typeset FichierActuel=${OPCTMP}/DskStmp_Actuel.txt
typeset FichierFlag=${OPCTMP}/DskStmp.flag

if [[ -f ${FichierFlag} ]]
then
	Fichier=$(basename ${FichierFlag})

	if [[ $(find ${OPCTMP} -name ${Fichier} -mtime +0 -print 2>/dev/null | awk 'END {print NR}') -gt 0 ]]
	then
		touch ${FichierFlag}
		rm -f ${FichierPrecedent}
	fi
else
	> ${FichierFlag}
	rm -f ${FichierPrecedent}
fi

${ReferenceFonction}

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

if [[ -f ${FichierPrecedent} ]]
then
	cat ${FichierActuel} | while read Filesystem Valeur
	do
		FilesystemTrouve=""
		ValeurTrouvee=""

		grep "^${Filesystem} " ${FichierPrecedent} | read FilesystemTrouve ValeurTrouvee

		if [[ -z ${FilesystemTrouve} ]]
		then
			let NbFilesystem=NbFilesystem+1
		else
			if [[ ${Valeur} -gt ${ValeurTrouvee} ]]
			then
				let NbFilesystem=NbFilesystem+1
			fi
		fi
	done
else
	NbFilesystem=$(cat ${FichierActuel} | awk 'END {print NR}')
fi

mv ${FichierActuel} ${FichierPrecedent}

if [[ ${NbFilesystem} -gt 0 ]]
then
	touch ${FichierFlag}
fi

${OPCMON} "${NomTemplate}=${NbFilesystem}"

if [ $? -ne 0 ]
then
	print "Probleme sur le programme $0." 1>&2
	exit 3
fi

exit 0
#
# eof
#=============================================================================
