#!/bin/bash
################################################################################
# Filename: ./src/base/bin/qrm-mon
# 
# The contents of this file are subject to the Qlusters Public License
# Version 1.1 (the License); you may not use this file except in compliance
# with the License.
# You may obtain a copy of the License at
# http://www.openqrm.org/content/view/33/69/.
# 
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
# 
# The Original Code is openQRM.
# 
# The Initial Developer of the Original Code is Qlusters Corporation. Portions
# created by Qlusters Corporation are Copyright (C) 2003-2005. All Rights
# Reserved.
# 
# Contributor(s):
# 
# Description:
# 
# Comments:
################################################################################
### ====================================================================== ###
##                                                                          ##
##  qrmmon                                                                  ##
##                                                                          ##
##  QRM-monitoring commandline util                                         ##
##                                                                          ##
##                                                                          ##
### ====================================================================== ###

# exit status of the QRM-script :
# 1 : unknown node
# 2 : could not update local config
# 3 : node not responding
# 4 : config file not found
# 5 : assignment locked
# 6 : error in usage
# 7 : kernel does not exists
# 8 : image does not exists

q_protocol=http
eval `cat /proc/cmdline` 2> /dev/null
q_qrm_ip=$qrm

. /var/qrm/conf/node-parameter.conf 2> /dev/null 1>&2
q_qrm_ip=${q_qrm_ip:-$QRM_SERVER_IP_ADDRESS} #can be also from env

q_qrm_ip=${q_qrm_ip:-localhost}

# optional parameters
q_monitor_command=${q_monitor_command:-"all"}
q_no_truncate=${q_no_truncate:-"off"}
q_no_headers=${q_no_headers:-"off"}


# constant
q_qsem=0
q_wget="/usr/bin/wget -q -O /dev/stdout "
q_qrmmon_usage="\
Usage :\n\
	qrmmon [-q qrm ip] [-p <http|https>] [-ves]|[-nodes]|[-vms]|[-ve ve name]|[-node node id]|[-vm vmid] [-f filter name][--no-truncate] [--no-headers] [--separator=<char>]\n\
\n\
	qrmmon - lists a summary of virtual environments and nodes and virtual machines \n\
	qrmmon -ves - lists a summary of virtual environments only\n\
	qrmmon -nodes - lists a summary of nodes only\n\
	qrmmon -vms - lists a summary of virtual machines only\n\
	qrmmon -ve <ve name> - shows detailed information on a specific virtual environment\n\
	qrmmon -node <node id> - shows detailed information on a specific node\n\
	qrmmon -vm <vm id> - shows detailed information on a specific virtual machine\n\
		-f <filter name> - uses a named filter when returning lists\n\
\n\
	-q <qrm ip> - ip of qrm server. optional when run from the qrm server or cotroled nodes\n\
	-p <http|https> - what protocol to use. default is http\n\
	--no-truncate - make columns as long as needed so as to not truncate data\n\
	--no-headers - do not output general header information\n\
	--separator <char> - uses char as column separator (default is |)\n"
	
	
# local used functions
function q_send_montitoring_request() {
	local MESSAGEDATA="command=$q_monitor_command&id=$q_user_intput&notruncate=$q_no_truncate&noheaders=$q_no_headers&separator=$q_separator&filter=$q_filter"
	$q_wget "$q_monitoring_servlet?$MESSAGEDATA"
	return $?
}



# check commandline parameters
while [ $# -ne 0 ]; do
	case "$1" in
                -q|--qrm)
                        q_qrm_ip=$2
                        shift
                        ;;
                -p|--protocol)
                        q_protocol=$2
                        shift
                        ;;
		-ves)
			q_monitor_command="servers"
	                ;;
		-vms)
			q_monitor_command="vms"
			;;
        	-f)
        		shift
	        	q_filter="$1"
        	    	;;
		-nodes)
			q_monitor_command="nodes"
			;;
		--no-truncate)
			q_no_truncate="on"
			;;
		--no-headers)
			q_no_headers="on"
			;;
		--separator=?)
			q_separator=$1
			q_separator=${q_separator/--separator=/}
			if [ -z "$q_separator" ]; then
				echo "ERROR: Missing value for parameters --separator"
				exit 6
			fi
			;;
		-h|--help)
			printf "$q_qrmmon_usage"
			exit 0
			;;
                -ve)
                        q_monitor_command="ve"
                        shift
                        q_user_intput=$1
                        ;;
		-vm)
			q_monitor_command="vm"
                        shift
                        q_user_intput=$1
                        ;;
	
                -node)
                        q_monitor_command="node"
                        shift
                        q_user_intput=$1
                        ;;
	esac
	shift
done

# display
echo "QRM IP $q_qrm_ip"

q_monitoring_servlet="$q_protocol://$q_qrm_ip/monitor/qrmmon.do"
# send command to monitoring servlet
q_send_montitoring_request
q_return=$?
if [ "$q_return" != "0" ]; then
	if arping -c 1 -w 0 $q_qrm_ip 1>/dev/null 2>&1; then
		echo "ERROR: Could not access the QRM-server on $q_qrm_ip. Maybe It is not started or you need to specify the ip with -q ?"
		else
		echo "ERROR: Could not reach the QRM-server. No network connection to $q_qrm_ip!"
	fi
fi
exit $q_return


