#/usr/bin/sh
###############################################################################
# (C) Copyright 2012-2013 Hewlett-Packard Development Company, L.P.
#               ALL RIGHTS RESERVED.
# @(#) Serviceguard Get Volume Size Script
# @(#) Product Name                :  HP Serviceguard
# @(#) Product Version             :  A.12.10.00
# @(#) Patch Name                  :  
#
# *** Note: This file MUST NOT be edited. *****
#
# Any changes made to it will be overwritten when you upgrade to the
# next release of HP Serviceguard.
#
# Changing this file may lead to unrecoverable problems with
# Logical Volume Monitoring.
#
#
# DESCRIPTION:
#          cmvxvolsize provides size of single LVM or VxVM or CVM volumes.
#          Volumes are to be supplied to the script as block device files.
#          Instructions for proper usage of the script are listed
#          immediately below.
###############################################################################

UNAME="$(uname)"
LVDISPLAY=/usr/sbin/lvdisplay
VXPRINT=/usr/sbin/vxprint
ECHO=/usr/bin/echo
AWK=/usr/bin/awk
GREP=/usr/bin/grep

if [ "$UNAME" = "Linux" ]; then
    LVDISPLAY=/sbin/lvdisplay
    ECHO=/bin/echo
    GREP=/bin/grep
    AWK=/bin/awk
fi

typeset -i LOG_LEVEL=0

function usage
{
    $ECHO "usage: cmvxvolsize   [-h, --help] [-v, --version]"
    $ECHO "                     [-d, --device-path <volume_path>]"
}


# set SG paths for this distro. File HAS to be present, else
# condition is just in case...
if [[ -f /etc/cmcluster.conf ]]
then
    . /etc/cmcluster.conf
else
    printf "This system is missing the /etc/cmcluster.conf file.\n"
    printf "Please verify the Serviceguard installation.\n"
    exit 1
fi

function log
{
    integer LEVEL=$1
    shift
    if (( $LEVEL <= $LOG_LEVEL ))
    then
        LINE="$(date +'%b %d %H:%M:%S') $*"
        if [[ "X$LOG_FILE" != "X" ]]
        then
            $ECHO $LINE >> $LOG_FILE
        else
            $ECHO $LINE
        fi
    fi
}

###############################################################################
# Function get_size_of_volume
#
# This function return size of volume.
# Return 0 if volume not belongs to LVM or VxVM or CVM or invalid path
# Return size_in_byte if volume belongs to LVM or VxVM or CVM
###############################################################################
function get_size_of_volume
{
    local in_byte="";
    local integer size_in_byte=0;

    if [[ -n "$1" ]];then
        size_in_byte=`$LVDISPLAY $1 2>/dev/null|$GREP -v $GREP|\
                      $GREP -i "LV Size (Mbytes)"|$AWK '{print $NF}'`
        if [[ -n "$size_in_byte" ]];then
            in_byte="MB"
        else
            $ECHO $1|$GREP -i -q "/dev/vx/"
            if [ $? -eq 0 ];then #If device is a VxVM/CVM volume
                # Extract second last token of complete path of volume as
                # disk group name assign to variable "dg_name" and last
                # token of complete path of volume as volume name assign
                # to variable "volume_name"
                dg_name=`$ECHO $1 |$AWK -F '/' '{print $(NF-1)}'`
                volume_name=`$ECHO $1 |$AWK -F '/' '{print $NF}'`
                # To differentiate between mirrored volume and without mirrored
                # volume "is_volume_or_vset" variable has been used.
                # If   "is_volume_or_vset" -> "Volume" then
                #                   volume => without mirrored volume
                # Else "is_volume_or_vset" -> "Vset" then
                #                   volume => mirrored volume
                cmd_op=`$VXPRINT -g $dg_name -l $volume_name 2>/dev/null`
                is_volume_or_vset=`$ECHO $cmd_op | $AWK -F ":" '{print $1}'|grep -e Volume -e Vset`
                if [[ "$is_volume_or_vset" = "Volume" ]];then
                    size_in_byte=$($VXPRINT -g $dg_name -l $volume_name | $GREP -i "info:"| \
                                 $GREP -ie "len="| $AWK -F 'len=' '{print $NF}'|$AWK '{print $1}')
                    # convert the size in KB from sectors 
                    let size_in_byte=$size_in_byte/2
                elif [[ "$is_volume_or_vset" = "Vset" ]];then
                    size_in_byte=-2
                else
                    size_in_byte=-2
                fi
            else
                size_in_byte=-3 # If device is a disk
            fi
            if [[ -n $size_in_byte ]];then
                if [[ $size_in_byte -ge 0 ]];then
                    in_byte="KB"
                fi
            fi
        fi
    fi
    $ECHO $size_in_byte$in_byte
}

# parse arguments
if [[ $1 = "-h" || $1 = "--help" ]]
then
    usage
    exit 0
elif [[ $1 = "-v" || $1 = "--version" ]]
then
    log 0 "Version is 1.0"
    exit 0
elif [[ $1 = "-d" || $1 = "--device-path" ]]
then
    shift
    VOLUME_GROUP=$1
    if [[ ! -b $VOLUME_GROUP ]]
    then
        $ECHO -1
        exit 1
    fi
    get_size_of_volume $VOLUME_GROUP;
    exit 0
else
    usage
    exit 1
fi
