#!/bin/bash
# by Sebastian Clanzett < sebastian[at]clanzett[DOT]de
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# Revision:
# - 20/10/2006: initial release
# - 20/10/2006: added domU check
# - 24/10/2006: lsof must be executed with sudo
# - 29/06/2007: check for sudo access and command
# - 29/06/2007: added /etc/sudoers example 
# - 29/10/2007: added small patch submitted by Tony Whitmore for better DomU checking
# - 12/03/2008: fixed some bugs with xm command 

VERSION=0.9.1

# set this to 0 tol disable check for Relocation,- and HTTP-Server by the lsof command. if enabeld you need to allow the nagios user 
# sudo access to the lsof command e.g:
#<code>
#	nagios          ALL=NOPASSWD: /usr/bin/lsof
#	nagios          ALL=NOPASSWD: /usr/sbin/xm
#</code>
CHK_PORTS=1

# Port to check for relocation server
RELOC_PORT=8002
# Port to check for XEN-HTTP server
HTTPD_PORT=8000

# find xm command :: you can also specify the exact path here
XM_CMD=`which xm`



function check_domU {
	DOMU=$1
	
	# if not root user then check for sudo access	
	if [ $UID -ne 0 ]; then
		sudo -l | grep $XM_CMD 1>/dev/null
		if [ $? -ne 0 ]; then
			echo "UNKNOWN: Perhaps no sudo acces to xm-command ?!?"
			exit 3
		fi
	fi

	# check if lsof is installed
	which lsof 1>/dev/null
	if [ $? -ne 0 ]; then
		echo "UNKNOWN: Cant find lsof"
		exit 3
	fi

	# check if sudo is installed
	which sudo 1>/dev/null
	if [ $? -ne 0 ]; then
		echo "UNKNOWN: Cant find sudo"
		exit 3
	fi

	sudo $XM_CMD list | grep $DOMU 1>/dev/null
	if [ $? -ne 0 ]; then
		echo "CRITICAL: The domU $DOMU seems to be down!"
		exit 2
	else
		TIME=`sudo $XM_CMD list | grep $DOMU | awk '{ print $6 }'`
		VCPU=`sudo $XM_CMD list | grep $DOMU | awk '{print $4}'`
		MEM=`sudo $XM_CMD list | grep $DOMU | awk '{print $3}'`
		STATE=`sudo $XM_CMD list | grep $DOMU | awk '{print $5}' | sed 's/-//g'`
		if [ $STATE == "d" ]; then
			echo "WARNING: $DOMU seems to be dying (Time=$TIME) (MEM=$MEM) (VCPU=$VCPU) (STATE=$STATE)";
		elif [ $STATE == "p" ]; then
			echo "WARNING: $DOMU seems to be paused (Time=$TIME) (MEM=$MEM) (VCPU=$VCPU) (STATE=$STATE)";
		elif [ $STATE == "c" ]; then
			echo "CRITICAL: $DOMU seems to be crashed (Time=$TIME) (MEM=$MEM) (VCPU=$VCPU) (STATE=$STATE)";
		else echo "OK: $DOMU seems to be up (Time=$TIME) (MEM=$MEM) (VCPU=$VCPU) (STATE=$STATE)"
		fi
		exit 0
	fi

}



function check_dom0 {
	# check if lsof is installed
	which lsof 1>/dev/null
	if [ $? -ne 0 ]; then
		echo "UNKNOWN: Cant find lsof"
		exit 3
	fi

	# check if sudo is installed
	which sudo 1>/dev/null
	if [ $? -ne 0 ]; then
		echo "UNKNOWN: Cant find sudo"
		exit 3
	fi

	# if not root user then check for sudo access	
	if [ $UID -ne 0 ]; then
		sudo -l | grep /usr/bin/lsof 1>/dev/null
		if [ $? -ne 0 ]; then
			echo "UNKNOWN: Perhaps no sudo acces to lsof-command ?!?"
			exit 3
		fi
	fi 

	# check if relocation-server is necessary
	if [ $1 == "reloc=y" ]; then
		CHK_RELOC=1
	elif [ $1 == "reloc=n" ]; then
		CHK_RELOC=0
	else 
		echo ""
		echo " >> SYNTAX ERROR << "
		echo ""
		print_usage
		exit 3
	fi

	# check if xen-httpd is necessary
	if [ $2 == "httpd=y" ]; then
		CHK_HTTPD=1
	elif [ $2 == "httpd=n" ]; then
		CHK_HTTPD=0
	else 
		echo ""
		echo ">> SYNTAX ERROR << "
		echo ""
		print_usage
		exit 3
	fi

	# check for XEN Daemon
	ps aux --cols 1024 | grep [x]end 1>/dev/null
	if [ $? -ne 0 ]; then
		XEND="not running"

	else
		XEND="OK"
	fi
	
	# if check for networking is enabled
	if [ $CHK_PORTS -eq 1 ]; then	
		# check for relocation Server
		sudo lsof -i | grep $RELOC_PORT 1>/dev/null
		if [ $? -ne 0 ]; then
			RELOC="is not running ($RELOC_PORT)"
			if [ $CHK_RELOC -eq 1 ]; then
				echo "CRITICAL: Xen Relocation Server is not running"
				exit 2
			fi



		else
			RELOC="OK ($RELOC_PORT)"
		fi

		#check for xend-http
		sudo lsof -i | grep $HTTPD_PORT 1>/dev/null
		if [ $? -ne 0 ]; then
			HTTPD="is not running ($HTTPD_PORT)"
			if [ $CHK_HTTPD -eq 1 ]; then
				echo "CRITICAL: Xen HTTPD Server is not running"
				exit 2
			fi
	
	
		else
			HTTPD="OK ($HTTPD_PORT)"
		fi

		echo "STATUS>> XEND: $XEND; Relocation-Server: $RELOC; Xen-Httpd: $HTTPD"
		exit 0
	fi
	echo "STATUS>> XEND: $XEND"

	exit 0


}


function print_usage {
	echo ""
	echo ""
	echo "  USAGE: $0 (dom0|domU)"
	echo ""
	echo ""
	echo "  dom0 ( reloc=(y|n) httpd=(y|n) )-> checks Status of dom0"
	echo "  Example: $0 dom0 reloc=y httpd=y  -> check dom0; Relocation Server must be running; Xen-HTTPD must be running"
	echo "  Example: $0 dom0 reloc=n httpd=y  -> check dom0; Relocation Server is not necessary; Xen-HTTPD must be running"
	echo ""
	echo ""
	echo "  domU <domU-Name> -> needs sudo permissions to the 'xm' command !!"
	echo "  Example: $0 domU Webserver1 -> checks if domU Webserver1 is running"
	echo ""
	echo "  Version: $VERSION by Sebastian Clanzett  <GPL>"
	echo "  Important: There is a flag (requiretty) on RedHat and CentOS systems in /etc/sudoers which prevents remote processes from using sudo."
	echo "  You have to remove this flag to let e.g nrpe use sudo."
	echo ""
}


case $1 in
	dom0)
		RELOC=$2
		HTTPD=$3
		if [ -z $2 ] ; then 
			RELOC="reloc=n"
		fi
		if [ -z $3 ]; then
			HTTPD="httpd=n"
		fi
		check_dom0 $RELOC $HTTPD
		
		;;

	domU)
		DOMU=$2
		if [ -z $2 ] ; then
                        echo ""
			echo "You must specify a domU to check"
			echo ""
		        exit 3
                fi
		check_domU $DOMU
		;;

	*)
		print_usage
		;;
esac	
