#!/bin/sh

# License: BeeGFS EULA

print_usage()
{
	echo
	echo "DESCRIPTION: Show free space information and available number of inodes for"
	echo "individual BeeGFS metadata servers and storage targets."
	echo "(This is a wrapper for beegfs-ctl.)"
	echo
	echo "USAGE: `basename $0` [options]"
	echo
	echo "   (default) - If no command line argument is given, list servers and targets"
	echo "               based on the default client config file."
	echo
	echo "   -h        - Print this help."
	echo
	echo "   -p <path> - Show servers and targets for the given BeeGFS mount point."
	echo
	echo "   -c <path> - Show servers and targets for the given BeeGFS client"
	echo "               configuration file."
	echo
	echo "   -e        - Exit code reports an error if a target is not"
	echo "               in the reachability state online or not in the"
	echo "               consistency state good."
	echo
	echo "   -s <ID>   - Show only storage targets belonging to storage pool with ID <ID>"
	echo
	echo "               consistency state good, requires the option --state."
}


################# end of function definitions ####################

ERROR=0


# parse command line arguments

while getopts "hp:c:es:" opt; do
	case $opt in
	h)
		print_usage
		exit 0
		;;
	p)
		BEEGFS_PATH_ARG="--mount=$OPTARG"
		;;
	c)
		BEEGFS_CFG_ARG="--cfgFile=$OPTARG"
		;;
	e)
		OPTIONAL_ARGS="--state --errorcodes"
		;;
	s)
		STORAGE_POOL_ARG="--storagepoolid=$OPTARG"
		;;
	*)
		echo "Invalid option: -$OPTARG" >&2
		print_usage
		exit 1
		;;
	esac
done

# show metadata server space info

echo "METADATA SERVERS:"
beegfs-ctl --listtargets --hidenodeid --pools --spaceinfo $OPTIONAL_ARGS --nodetype=meta \
$BEEGFS_PATH_ARG $BEEGFS_CFG_ARG
if [ $? -ne 0 ]
then
	ERROR=1
fi

echo

# show storage server space info

echo "STORAGE TARGETS:"
beegfs-ctl --listtargets --hidenodeid --pools --spaceinfo $OPTIONAL_ARGS --nodetype=storage \
$STORAGE_POOL_ARG $BEEGFS_PATH_ARG $BEEGFS_CFG_ARG
if [ $? -ne 0 ]
then
	ERROR=1
fi

if [ -n "$OPTIONAL_ARGS" ] && [ $ERROR -eq 1 ]
then
	exit 1
fi
