#!/bin/bash

ME=$(basename $0);

source //etc/common-utils.rc;

function show_help()
{
    usage_banner;
    cat <<EOF

Usage:  $ME [-h] TARGET

Check accessiblity of Gluster in Gluster Storage Virtual Appliance
instance.  TARGET can be hostname or IP address of the instance.

Miscellaneous:
  -h                        display this help and exit

Example:
  $ME ec2-192-168-1-123.compute-1.amazonaws.com
  $ME 192.168.1.67
EOF
}


function main()
{
    # Parse command line arguments.
    while getopts :h OPT; do
	case "$OPT" in
	    h)
		show_help
		exit 0
		;;
	    \?)
                # getopts issues an error message
		echo "Invalid option: -$OPTARG"
		show_help
		exit 1
		;;
	esac
    done

    # Remove the switches we parsed above.
    shift `expr $OPTIND - 1`

    # We want only one non-option arguments.
    if [ $# -ne 1 ]; then
	show_help
	exit 1
    fi

    set -e

    echo "Ports marked closed may indicate that Gluster is not running on the remote host."
    echo "Ports marked filtered have been blocked by a firewall."
    ports=( '111'  '24007' '24009' '24010' '24011' '38465' '38466' '38467' )

    for i in "${ports[@]}"; do
	status=`nmap -PN -p $i $1 | grep $i | awk '{print $2}'`
	echo "Port $i is $status"
    done
}


main "$@";
