#!/bin/sh
#
# Resource script for nrpe daemon
#
# Description:  Manages nrpe daemon as an OCF resource in 
#               an High Availability setup.
#
# Author: Pascal Brugier <pbrugier@easter-eggs.com>
# License: GNU General Public License (GPL) 
#
#
#	usage: $0 {start|stop|status|monitor|validate-all|meta-data}
#
#	The "start" arg starts nrpe.
#
#	The "stop" arg stops it.
#
# OCF parameters:
# OCF_RESKEY_binpath
# OCF_RESKEY_conffile
#
# Note:This RA requires that the nrpe config files has a "pid_file" 
# entry so that it is able to act on the correct process
##########################################################################
# Initialization:

: ${OCF_FUNCTIONS_DIR=${OCF_ROOT}/lib/heartbeat}
. ${OCF_FUNCTIONS_DIR}/ocf-shellfuncs

USAGE="Usage: $0 {start|stop|status|monitor|validate-all|meta-data}";

##########################################################################

usage() 
{
	echo $USAGE >&2
}

meta_data() 
{
cat <<END
<?xml version="1.0"?>
<!DOCTYPE resource-agent SYSTEM "ra-api-1.dtd">
<resource-agent name="nrpe">
<version>1.0</version>
<longdesc lang="en">
This script manages nrpe daemon
</longdesc>
<shortdesc lang="en">Manages an nrpe daemon</shortdesc>

<parameters>

<parameter name="binpath">
<longdesc lang="en">
The nrpe binary path.
For example, "/usr/sbin/nrpe"
</longdesc>
<shortdesc lang="en">Full path to the nrpe binary</shortdesc>
<content type="string" default="nrpe"/>
</parameter>

<parameter name="conffile">
<longdesc lang="en">
The nrpe daemon configuration file name with full path. 
For example, "/etc/nagios/nrpe.cfg"
</longdesc>
<shortdesc lang="en">Configuration file name with full path</shortdesc>
<content type="string" default="/etc/nagios/nrpe.cfg" />
</parameter>

</parameters>

<actions>
<action name="start" timeout="20s"/>
<action name="stop" timeout="20s"/>
<action name="monitor" depth="0" timeout="20s" interval="60s" />
<action name="validate-all" timeout="20s"/>
<action name="meta-data"  timeout="5s"/>
</actions>
</resource-agent>
END
exit $OCF_SUCCESS
}

get_pid_and_conf_file()
{
	if [ -n "$OCF_RESKEY_conffile" ]; then
		CONF_FILE=$OCF_RESKEY_conffile
	else
		CONF_FILE="/etc/nagios/nrpe.cfg"
	fi
               
	grep -v "^#" "$CONF_FILE" | grep "pid_file" > /dev/null
	if [ $? -eq 0 ]; then
		PIDFILE=`grep -v "^#" "$CONF_FILE" | grep "pid_file" | awk -F "=" '{ print $2 }'`
	fi
}

nrpe_status()
{
	if [ -n "$PIDFILE" -a -f $PIDFILE ]; then
		# nrpe is probably running
		PID=`cat $PIDFILE`
		if [ -n "$PID" ]; then
			if ps -p $PID | grep nrpe >/dev/null ; then
				ocf_log info "nrpe daemon running"
				return $OCF_SUCCESS
			else
				ocf_log info "nrpe daemon is not running but pid file exists"
				return $OCF_ERR_GENERIC
			fi
		else
			ocf_log err "PID file empty!"
			return $OCF_ERR_GENERIC
		fi
	fi
		
	# nrpe is not running
	ocf_log info "nrpe daemon is not running"
	return $OCF_NOT_RUNNING
}

nrpe_start()
{
	# if nrpe is running return success
	nrpe_status
	retVal=$?
	if [ $retVal -eq $OCF_SUCCESS ]; then
		exit $OCF_SUCCESS
	elif [ $retVal -ne $OCF_NOT_RUNNING ]; then
		ocf_log err "Error. Unknown status."
		exit $OCF_ERR_GENERIC
	fi

	if [ -n "$OCF_RESKEY_binpath" ]; then
		COMMAND="$OCF_RESKEY_binpath --daemon"
	else
		COMMAND="nrpe --daemon"
	fi
	if [ -n "$OCF_RESKEY_conffile" ]; then
		COMMAND="$COMMAND -c $OCF_RESKEY_conffile"
	fi

	if  grep -v "^#" "$CONF_FILE" | grep "pid_file" > /dev/null ; then
		$COMMAND;
		if [ $? -ne 0 ]; then
			ocf_log err "Error. nrpe daemon returned error $?."
			exit $OCF_ERR_GENERIC
		fi
	else
		ocf_log err "Error. \"pid_file\" entry required in the nrpe config file by nrpe OCF RA."
		return $OCF_ERR_GENERIC
	fi

	ocf_log info "Started nrpe daemon."
	exit $OCF_SUCCESS
}


nrpe_stop()
{
	if nrpe_status ; then
		PID=`cat $PIDFILE`
		if [ -n "$PID" ] ; then
			kill $PID
			if [ $? -ne 0 ]; then
				kill -s KILL $PID
				if [ $? -ne 0 ]; then
					ocf_log err "Error. Could not stop nrpe daemon."
					return $OCF_ERR_GENERIC
				fi
			fi
			rm $PIDFILE 2>/dev/null
		fi
	fi
	ocf_log info "Stopped nrpe daemon."
	exit $OCF_SUCCESS
}

nrpe_monitor()
{
	nrpe_status
}

nrpe_validate_all()
{
	if [ -n "$OCF_RESKEY_binpath" -a ! -x "$OCF_RESKEY_binpath" ]; then
		ocf_log err "Binary path $OCF_RESKEY_binpath does not exist."
		exit $OCF_ERR_ARGS
	fi
	if [ -n "$OCF_RESKEY_conffile" -a ! -f "$OCF_RESKEY_conffile" ]; then
		ocf_log err "Config file $OCF_RESKEY_conffile does not exist."
		exit $OCF_ERR_ARGS
	fi

	if  grep -v "^#" "$CONF_FILE" | grep "pid_file" > /dev/null ; then
		:
	else
		ocf_log err "Error. \"pid_file\" entry required in the nrpe config file by nrpe OCF RA."
		return $OCF_ERR_GENERIC
	fi

#Not checking "$OCF_RESKEY_bwlimit"

	return $OCF_SUCCESS
}


#
# Main
#
 
if [ $# -ne 1 ]; then
	usage
	exit $OCF_ERR_ARGS
fi

case $1 in
	start)	get_pid_and_conf_file
		nrpe_start
		;;
	
	stop)	get_pid_and_conf_file
		nrpe_stop
		;;

	status)	get_pid_and_conf_file
		nrpe_status
		;;

	monitor)get_pid_and_conf_file
		nrpe_monitor
		;;

	validate-all)	get_pid_and_conf_file
			nrpe_validate_all
			;;

	meta-data)	meta_data
			;;

	usage)	usage
		exit $OCF_SUCCESS
		;;

	*)	usage
		exit $OCF_ERR_UNIMPLEMENTED
		;;
esac

