#!/usr/bin/ksh
#=============================================================================
# Nom            : EdmAffpK7
# Projet         : HP-Openview IT/Operations
# Cree le        : 25/11/1999
# Cree par       : Jph. BOCQUENET
# Objet          : Affiche les resultats des sauvegardes EDM.
# Appel(s)       : HP-OV-ITO Template Edm_Proc_K7
# 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 : numero de l'instance #
# 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
	print "Probleme sur le programme $0." 1>&2
	exit 1
fi

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

# Definition des variables globales locales script:
typeset NbSauv=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 OPCTMP			# PATH du fichier temporaire
typeset TypeMachine		# Recuperation de l'OS  	
typeset VersionOS		# Recuperation de la version de l'OS 
typeset ReferenceFonction	# Commande differente suivant l'OS

typeset EVMPATH="/usr/epoch/bin"
typeset EBLOG="/usr/epoch/EB/log"
#=============================================================================
# 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 [ "$(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"
		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 [ "$(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"
		;;
	IRIX)
		ReferenceFonction=FonctionIRIX
		OPCMON=/opt/OV/bin/OpC/opcmon
		OPCTMP=/var/opt/OV/tmp/OpC
		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
{
file=bilan_`date +%Y%m%d_%H%M`
resulOK=`grep SUCCEEDED ${EBLOG}/backups.log| grep -v sync | wc -l`
resulKO=`grep FAILED, ${EBLOG}/backups.log | wc -l`

if [ -f /tmp/edmsauv.txt ]
	then	rm /tmp/edmsauv.txt
		touch /tmp/edmsauv.txt
	else
		touch /tmp/edmsauv.txt
fi

if [ -f ${OPCTMP}/EdmSauv.tmp ]
        then    rm ${OPCTMP}/EdmSauv.tmp
                touch ${OPCTMP}/EdmSauv.tmp
        else
                touch ${OPCTMP}/EdmSauv.tmp
fi

print "Ce fichier est sauvegarde sur le serveur d'origine dans : \n"
print "${OPCTMP} sous le nom : EdmSauv.tmp\n\n"
print `date '+%Y%m%d_%H%M'` > ${OPCTMP}/EdmSauv.tmp 
print "\n------------------------------\n"  >> ${OPCTMP}/EdmSauv.tmp 
print "il y a ${resulOK}  sauvegardes OK\n"  >> ${OPCTMP}/EdmSauv.tmp 
print " ------------------------------\n\n"  >> ${OPCTMP}/EdmSauv.tmp 
print " -------------------------------------\n"  >> ${OPCTMP}/EdmSauv.tmp 
print "il y a ${resulKO}  sauvegardes en ECHEC\n"  >> ${OPCTMP}/EdmSauv.tmp 
print " -------------------------------------\n\n"  >> ${OPCTMP}/EdmSauv.tmp
print " -------------------------------------\n"  >> ${OPCTMP}/EdmSauv.tmp
print " Nombre de cassettes DLT disponibles : \n"  >> ${OPCTMP}/EdmSauv.tmp
cassettes=`${EVMPATH}/dbreport avail | grep -v off | grep DLT |  wc -l`
totk7=`${EVMPATH}/dbreport online | grep DLT | wc -l`
print "  ${cassettes} / `expr ${totk7}` au total\n "  >> ${OPCTMP}/EdmSauv.tmp
print " -------------------------------------\n\n"  >> ${OPCTMP}/EdmSauv.tmp
print " -------------------------------------\n"  >> ${OPCTMP}/EdmSauv.tmp
print "Liste des work-items plantes \n\n"  >> ${OPCTMP}/EdmSauv.tmp 
cat ${EBLOG}/backups.log |
  nawk -F " " '{
	if( $10 ~ "FAILED" )
		{
		ligne1 = $0;
		print ligne0  ligne1 ;
		}
		ligne0 = $0;
   }' |
  nawk -F " " '{
		print $8 "\t" $1 "\t" $2 "\t" $10 "\tlevel " $12;
   }' >> /tmp/edmsauv.txt 

sort -f /tmp/edmsauv.txt >> ${OPCTMP}/EdmSauv.tmp

print "\n\n+++++++++++++++++++++++++++++++++\n"  >> ${OPCTMP}/EdmSauv.tmp
cp ${OPCTMP}/EdmSauv.tmp ${EBLOG}/bilan/${file}
 
return 0
}

function FonctionAIX
{
	FonctionSolaris
	return 0
}

function FonctionAIX42
{
	FonctionAIX
	return 0
}

function FonctionIRIX
{
	FonctionSolaris
	return 0
}

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

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

if [ $? -ne 0 ]
then
	print "Probleme sur le programme $0." 1>&2
	exit 1
fi
	 
cat ${OPCTMP}/EdmSauv.tmp

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