#!/bin/csh -f
#*****************************************************************************
#                                                                            *
#            Copyright (C) 1993, Alias Research, Inc.                        *
#                                                                            *
#    These coded instructions,  statements and  computer programs contain    *
#    unpublished information proprietary to Alias Research, Inc.  and are    *
#    protected by the Canadian and US Federal copyright law. They may not    *
#    be disclosed to third parties  or copied  or duplicated, in whole or    *
#    in part,  without the prior written consent of Alias Research,  Inc.    *
#                                                                            *
# Unpublished-rights reserved under the Copyright Laws of the United States. *
#                                                                            *
#****************************************************************************/
#
#       $Source: /tmp_mnt/repo/root/v/alias3.2.2/src/Gifts/bin/RCS/drender_health.csh.sgi,v $
#       $Author: psantan $ $Date: 93/06/23 16:05:33 $
#
#
#

if (! $?ALIAS_LOCATION ) then
    echo "ALIAS_LOCATION environment variable must be set to install drender"
    exit -1
else
    set NT_DR=${ALIAS_WORKENV}/user_data/demo/misc_data/dlock.pst

    pst ${NT_DR}
    if ( $status == "1" ) then
        exit 1
    endif

    set NT_MACHINES=(`pstget ${NT_DR} '*.hosts' 'all'`)

    umask 000

    pushd ${NT_MASTER_DIR}/config > /dev/null

    if ( "${NT_MACHINES}" == "all" || "${NT_MACHINES}" == "ALL" || "${NT_MACHINES}" == "" ) then
        set NT_FILTER='*'
    else
        set NT_FILTER="${NT_MACHINES}"
    endif

    echo "*** Drender network state for `date` ***"

    foreach mach ( ${NT_FILTER} )

        if ( "${mach}" == "localhost" ) then

            set machn=`hostname`
        else

            set machn=${mach}
        endif

        if ( "${machn}" != "`hostname`" ) then
            set NT_RCMD="rsh ${machn} "
        else
            set NT_RCMD=""
        endif

        #
        # for any file not ending in .locked, we want to report on
        # the corresponding machine.
        #
        if ( "`basename ${machn} '.locked'`" == ${machn} ) then

            echo "Checking machine ${machn}"

            if ( "`rsh ${machn} echo TRUE`" == "TRUE" ) then

                #
                # Here we check to see if there is a drender daemon recorded as
                # runningon this host. 
                #
                if (-e ${NT_MASTER_DIR}/bin/${machn}.kill) then

	                set NT_OLD_PID=`csh -f ${NT_MASTER_DIR}/bin/${machn}.pid`

                    #
                    # nowdoa ps to see if such a process exists, and whether it
                    # is *likely* a distributed renderer
                    #
                    set NT_TMP2=/usr/tmp/NT_TMP2.$$
                    ${NT_RCMD} ps -p ${NT_OLD_PID} -f > ${NT_TMP2}
   
                    set NT_WC_ARRAY=(`wc ${NT_TMP2}`)

                    # skip the header and read the process line
                    set NT_PS_RET=(`tail -1 ${NT_TMP2}`)

                    /bin/rm -f ${NT_TMP2}

                    set NT_PS_IND=$#NT_PS_RET


                    if ( $NT_WC_ARRAY[1] == "2" ) then

                        set NT_COMSTR=$NT_PS_RET[$NT_PS_IND]

                        if ( $NT_COMSTR:t == "${machn}.check" ) then

                            set NT_RUNNING=TRUE

                        else

                            set NT_RUNNING=FALSE

                        endif
                    else
                        set NT_RUNNING=FALSE
                    endif

                    if ( "${NT_RUNNING}" == "TRUE" ) then 
                        echo "    DRENDER deamon is running, PID=${NT_OLD_PID}."
                    else
                        echo "    DRENDER deamon is not running."
                    endif
                endif 

            else
                echo "    machine ${machn} is not responding."
            endif
        endif
    end

    popd > /dev/null

endif




