#!/sbin/sh
# OPC_WHAT_STRING="@(#)HP OpenView Operations A.07.10 (05/07/02)"
################################################################################
#
# File:         opcvmsping
# Description:  ALIVE_CHECK for OpenVMS
# Language:     Bourne Shell
# Package:      HP OpenView OperationsCenter
#
# (c) Copyright Hewlett-Packard Co. 1993 - 2004
#
################################################################################

#
#  DESCRIPTION
#    OpenVMS ALIVE_CHECK script.
#    This script checks if $OPC_NODE is reachable.
#    Following is list of all actions:
#
#      1. check if OPC_NODE is reachable via ping utility.
#
#      2. check if all send packagages are echoed back.
#
#      3. check password of OPC_NODE OPC_INST_USER .
#         This does unix common function: opcvmspwget
#         Parameters :
#         Output environment variables:
#           OPC_PASSWD_REQ OPC_PASSWD
#         Return code:
#           0 .. OK
#           1 .. ERROR in password 
#         If return code equals 1, display error and skip this node.
#
#      4. check time difference between Management Server and Managed Node,
##        if OPC_METHOD equals INSTALL
#
#
#  PARAMETERS
#    output_file .. name of temporary file where line of password_file(5) for
#                   actual node will be placed
#
#  EXIT_CODE
#    0 .. ok, node is reachable via ping utility
#    1 .. error, node is not reachable
#

# defined in every script - the version of this script
INST_OPCVERS=A.07.10

#-------------------------------------
# Module name definition
#-------------------------------------

# define module name

. "$OPC_AGTINST_DIR"/opcprocs
. "$OPC_AGTINST_DIR"/opcvmsprocs

#===============================================================================
#
# STATIC VARIABLES
#
DATE_FORMAT="+%j/%Y %m%d%y %H:%M:%S"		    # date format
AL_DATE_FORMAT="+%j/%y %m%d%y %H:%M:%S"		    # date format without %Y
						    # used because SunOS 
OUTPUT_F=${OPC_TOPC}/output.data		    # tmp filename
SPKG=5						    # number of packets
						    # $OPC_NODE and server time

#===============================================================================
#
#  DESCRIPTION
#    resolve UNIX node Internet address, by using nslookup utility, from
#    node Name, if ip_address is not set (ip_address equals node_name).
#
#  PARAMETERS
#    node_name  .. node Name
#    ip_address .. node IP address
#
#  ENVIRONMENT VARIABLES
#    PING_NODE .. IP address of node
#
#  EXIT_CODE
#    0 .. ok, node is known
#    1 .. error, node is not known
#    2 .. usage error
#
resolve_name()
{
    if [ $1 = $2 ]
    then
        ND_NAME=`nslookup $1 2>/dev/null | awk '/^Name:/  { print $2 }'`
        if [ -z "${ND_NAME}" ]
        then
            unset ND_NAME
            return 1
        fi
        PING_NODE=`nslookup $1 2>/dev/null | awk '/^Address:/  { ADR=$2 }\
                                       END          { print ADR }'`
        unset ND_NAME
    else
        PING_NODE=$2
    fi
    return 0

}

#===============================================================================
#
#  MAIN script body
#

#OPC_ADEBUG="OPCAYES"
#OPC_DEBUG="YES"
#OPC_TRACE="YES"
#OPC_TRACE_FILE="/var/opt/OV/log/OpC/mgmt_sv/jtping.log"
OPC_INST_USER="SYSTEM"
export OPC_INST_USER
# display start module
if [ "${OPC_ADEBUG}" = "OPCAYES" ]
then
    set -x -v
    DEBUG_CNT=`expr $DEBUG_CNT + 1`
fi
if [ "${OPC_TRACE}" = "YES" ]
then
    CALLOUT_NAME=$0
    CALLOUT_PARAMS="$*"
    #oi_trace opc 588 "${MODULE_NAME}" ${CALLOUT_NAME} "\"${CALLOUT_PARAMS}\""
    echo "TRACE:    ${TRACE_HEADER}" | \
    sed -e "s=MODULxxxxxxx=${MODULE_NAME}=" \
        -e "s=SCRIPTxxxxxx=${CALLOUT_NAME}=" \
    >> ${OPC_TRACE_FILE}
fi
if [ "${OPC_DEBUG}" = "YES" ]
then
    # enable tracing in all functions
    eval `typeset -f | awk '/^function/ { printf "typeset -tf %s\n", $2 }'`
fi
if [ "${OPC_ADEBUG}" = "OPCAYES" ]
then
    if [ ${DEBUG_CNT} -gt 0 ]
    then
	DEBUG_CNT=`expr $DEBUG_CNT - 1`
    fi
    if [ ${DEBUG_CNT} -gt 0 ]
    then
	set -x
    fi
fi

#   input parameters
#
if [ $# -ne 1 ]
then
    #	usage error
    #
    oi_error "opc" 511 "opcvmsping"
    EXIT 2
fi
OPC_OUTPUT_FILE=$1

OPC_STATUS="NOTREADY"

#
#   first check, if node equals current system,
#   because this script can be also used for HP-UX series 700 or 800
#
opcux_localhost ${OPC_NODE}
if [ $? -eq 0 ]
then
    # this is local Managed Node: of course it is alive
    #
    OPC_STATUS="READY"
    OPC_PASSWD_REQ=0
    OPC_PASSWD=OPC_UNKNOWN_PASSWORD
    KERBEROS_BYPASS="NO"
    ACTIVATE_ONLY=0
    produce_password
    EXIT 0
fi

#    node is remote; 
#    1. check, if system is responding, by using ping utility
#    NOTE: ping utility can handles only ARPA name or IP Address
#

resolve_name ${OPC_NODE} ${OPC_IP_ADDRESS}
if [ $? -ne 0 ]
then
    oi_error "opc" 020 ${OPC_NODE}
    produce_password
    EXIT 1
fi

do_ping ${PING_NODE}
if [ $? != 0 ]
then
    EXIT 1
fi

if [ "${OPC_METHOD}" = "INSTALL" ]
then
## Note: on 06.03.97 was decided to not activate node from server
##       SO following code is put in comments
##       If it will be (hope not) activated again, also opcragt utility
##       must be corrected as well.
## START_OF_COMM
##    2. check if opcclta is alive first 
##    #check if opcclta is alive first 
##    #
##    do_opcragt ${OPC_NODE}
##    if [ $? -eq 0 ]
##    then
##	# opcctla is alive; if ITO agent is in preinstall state and 
##	# ITO version equals selected version, only activation of node
##	# is enought
##	if [ "${CA_STATE}" = "preinstalled" ]
##	then
##	    if [ "${OPC_METHOD}" = "INSTALLED" ]
##	    then
##		if [ "${CA_VERSION}" = "${OPC_VERSION}" ]
##		then
##		    OPC_STATUS="READY"
##		    ACTIVATE_ONLY=1
##		    OPC_PASSWD_REQ=2
##		    OPC_PASSWD=OPC_UNKNOWN_PASSWORD
##		    KERBEROS_BYPASS="NO"
##		    oi_note opc 678 ${OPC_NODE}
##		    produce_password
##		    EXIT 0
##		else
##		    oi_note "opc" 684 ${CA_VERSION} ${OPC_NODE} ${OPC_VERSION}
##		fi
##	    else
##		oi_note "opc" 684 ${CA_VERSION} ${OPC_NODE}
##	    fi
##	fi
##    fi
## END_OF_COMM

    # no activation
    ACTIVATE_ONLY=0

    if [ ${OPC_DEPOT_NODE} = "VOID" -o ${OPC_DEPOT_NODE} = ${OPC_NODE} ]
    then
        #    3. check node's SYSTEM password
        #
        opcvmspwget
        if [ $? -eq 1 ]
        then
            #       invalid password specified; skip this node
            #
            oi_error "opc" 165 ${OPC_NODE}
            produce_password 
            EXIT 1
        fi
    else
        OPC_PASSWD_REQ=0
        OPC_PASSWD=OPC_UNKNOWN_PASSWORD
    fi

    #    4. get the HP ITO Agent account password
    #
    if [ $OPC_INST_METHOD -ne 4 ]
    then
      opcvmspwget_account
      if [ $? -eq 1 ]
      then
          #   invalid password specified; use the default password
          #
          oi_verbose "opc" 373
      fi
    fi

    #    produce password line
    #
    OPC_STATUS="READY"
    KERBEROS_BYPASS="NO"
    produce_password 

    EXIT 0
else
    OPC_STATUS="READY"
    ACTIVATE_ONLY=0
    OPC_PASSWD_REQ=0
    OPC_PASSWD=OPC_UNKNOWN_PASSWORD
    KERBEROS_BYPASS="NO"
    produce_password 

    EXIT 0
fi

EXIT 0

################################################################################
# end of opcvmsping
################################################################################
