#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/cspoc/utilities/clresactive.sh 1.7.2.6 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1996,2011 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)57	1.7.2.6 src/43haes/usr/sbin/cluster/cspoc/utilities/clresactive.sh, hacmp.cspoc, 61haes_r714 11/28/11 15:06:23
#
#   COMPONENT_NAME: CSPOC
#
#   FUNCTIONS:
#
#   ORIGINS:
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1996
#   All Rights Reserved
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#
###############################################################################
#
# Name:
#       clresactive.sh
#
# Description:
#   clresactive returns one of yes, no, active, or inactive depending on
#   the accessibility of some resource specified on the command line.
#   Resources that may be queried are:
#   -v volume group
#   -l logical volume
#   -f filesystem
#   -u user
#   -g group
#   -V HACMP version
#   -c cmd[::cmd...]
#
#   Usage: clresactive {-v <vg>,-l <lv>,-f <fs>,-u <user>,-g <group>,-V <ver>,-c <cmd[::cmd...]>}
#
# Return Values:
#       0       success
#       1       failure
#
################################################################################
set -u

#usage="clresactive {-v <vg>,-l <lv>,-f <fs>,-u <user>,-g <group>,-V <ver>,-v <cmd[::cmd...]}"

usage=$(dspmsg -s 2  cspoc.cat 14 "clresactive {-v <vg>,-l <lv>,-f <fs>,-u <user>,-g <group>,-V <ver>,-v <cmd[::cmd...]}\n")

# init all to empty strings
vg=""
lv=""
fsys=""
user=""
group=""
integer version=0

function leave
{
    print $1
    exit 0
}

(( $# != 2 )) && {
    print "$usage"
    exit 1
}

#
:   Parse options
#
while getopts ":v:l:f:u:g:V:c:" option 
do
    case $option in
	v )
	    vg=$OPTARG
	;;
	l )
	    lv=$OPTARG
	;;
	f )
	    fsys=$OPTARG
	;;
	u )
	    user=$OPTARG
	;;
	g )
	    group=$OPTARG
	;;
	V )
	    version=$OPTARG
	;;
	c )
	    cmds=$OPTARG
	;;
	* )
	    print "$usage"
	    exit 1
	;;
    esac
done

#
:   asked for a VG
#
if [[ -n $vg ]]
then
    #
    :	Start out with the output of lsvg, we determine what is going on
    :	by trying to pick out key parts of error messages and what not
    #
    if ! lsvg_out=$(LC_ALL=C lsvg $vg 2>&1)	# lsvg output plus any error msgs
    then
	if print -- "$lsvg_out" | grep -qi "varyonvg" 
	then
	    #
	    :	Error message indicates not varied on
	    #
	    leave "inactive"
	else
	    #
	    :	Volume group is not known on this node
	    #
	    leave "no"
	fi
    else
	if print -- "$lsvg_out" | grep -i -q "passive-only"
	then
	    #
	    :	Varied on in passive mode - varyonvg -c -P
	    :	Note that this test must preceed the test for 'concurrent',
	    :	since the VM mode is 'concurrent' in that case
	    #
	    leave "passive"

	elif print -- "$lsvg_out" | grep -i -q 'VG Mode: * Concurrent ' 
	then
	    #
	    :	Varied on in concurrent mode - varyonvg -c
	    #
	    leave "concurrent"

	elif print -- "$lsvg_out" | grep -i -q 'VG STATE: * active '
	then
	    #
	    :	Ordinary serial access varyon - varyonvg
	    #
	    leave "active"
	fi
    fi
fi

#
:   asked for an LV
#
if [[ -n $lv ]]
then
    STATE=$(LC_ALL=C lslv $lv 2>/dev/null | sed -n '/LV STATE:/s/.*LV STATE: *\([^ ]*\).*/\1/p')
    case $STATE in
	opened/* | closed/*)
	    STATE=active
	    ;;
	\?)
	    STATE=inactive
	    ;;
	*)
	    STATE=no
	    ;;
    esac
    leave "$STATE"
fi

#
:   asked for an FS
#
if [[ -n $fsys ]]
then
    if ! lsfs $fsys > /dev/null 2>&1
    then
	leave "no"		# file system not known on this node
    fi

    if mount | grep -qw $fsys
    then
	leave "active"		# file system is mounted
    else
	leave "inactive"	# file system is not mounted
    fi
fi

#
:   asked for a user
#
if [[ -n $user ]]
then
    if lsuser $user >/dev/null 2>&1
    then
	leave "yes"
    else
	leave "no"
    fi
fi

#
:   asked for a group
#
if [[ -n $group ]]
then
    if lsgroup $group >/dev/null 2>&1 
    then
	leave "yes"
    else
	leave "no"
    fi
    return 0
fi

#
:   asked for HACMP version check
#
if (( $version != 0 )) 
then
	#
	:   Determine HACMP version
	#
	integer V R M F
	typeset -Z2 R                       # two digit release
	typeset -Z3 M                       # three digit modification
	typeset -Z3 F                       # three digit fix

	lslpp -lcqOr cluster.es.server.rte 2>/dev/null | \
	    cut -f3 -d':' | IFS=. read V R M F

	V=$V$R$M$F
	if [[ -z $V ]] || (( $V < $version )) 
	then
	    leave "no"
	else
	    leave "yes"
	fi
fi

#
:   Run arbitrary commands
#
if [[ -n $cmds ]]
then
    for cmd in $(print "$cmds" | sed -e 's/::/ /g') 
    do
	c=$(print $cmd | tr ":" " ")
	print "__CSPOC_NODE_CMD__$c"
	$c 2>&1
	print "__CSPOC_RETCODE__$?"
    done
    print "__CSPOC_CMD_END__"
    return 0
fi

# should never get here, but just in case...
exit 1
