#! /bin/sh
# Copyright (c) 1995-2001 SuSE Linux Solutions AG, Germany.
# Copyright (c) 2002-2003 SuSE Linux AG, Nuernberg, Germany.
# Copyright (c) 2004-2008 SUSE LINUX Products GmbH, Nuernberg, Germany.
# All rights reserved.
#
# Author: Matthias Eckermann, SuSE Linux Solutions AG
#         Marius Tomaschewski, SuSE Linux AG
#
### BEGIN INIT INFO
# Provides:       ipvsadm
# Required-Start: $network
# Required-Stop:  $network
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    This shell script takes care of starting and stopping ipvsadm 
### END INIT INFO

# Source status functions
. /etc/rc.status || exit 1

# Reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
#
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

# Set defaults
IPVSADM_CONFIG="/etc/ipvsadm.rules"

# source optional settings (overrides)
if test -f /etc/sysconfig/ipvsadm ; then
	.  /etc/sysconfig/ipvsadm || exit 1
fi

# needed tools
ipvsadm="/sbin/ipvsadm"
ipvsadm_load="/sbin/ipvsadm-restore"
ipvsadm_save="/sbin/ipvsadm-save"

config_is_empty()
{
	if [ -n "$1" -a -f "$1" ] ; then
		# not configured if empty 
		if [ $(grep -Ev --count -e '^[[:space:]]*(#.*)?$' -- "$1" \
						2>/dev/null) -gt 0 ] ; then
			return 1
		fi
	fi
	return 0
}
read_clean_config()
{
	grep -Ev -e '^[[:space:]]*(#.*)?$' -- "$1" 2>/dev/null
}

# check the availability of the binaries
if ! [ -x $ipvsadm       -a \
       -x $ipvsadm_load  -a \
       -x $ipvsadm_save   ] ; then
	case $1 in
	stop)	;;
	*)	echo -n >&2 "${0##*/}: \"ipvsadm\" binaries are not executable, exiting."
		case $1 in
		status)	rc_failed 4 ;;
		*)	rc_failed 5 ;;
		esac
		rc_status -v
	;;
	esac
	rc_exit
fi

# check the availability of a configu
if config_is_empty "$IPVSADM_CONFIG" ; then
	case $1 in
	stop|status|save|list|show) ;;
	*)
		echo -n >&2 "${0##*/}: \"$IPVSADM_CONFIG\" configuration missed, exiting."
		rc_failed 6
		rc_status -v
		rc_exit
	;;
	esac
fi

case "$1" in
    start|restore|load)
	echo -n "clearing the current IPVS table, "
	$ipvsadm -C
	rc_check

	echo -n "applying IPVS configuration "
	read_clean_config "$IPVSADM_CONFIG" | $ipvsadm_load
	rc_status -v
    ;;

    stop)
	echo -n "clearing the current IPVS table"
	$ipvsadm -C
	rc_status -v
    ;;

    restart|try-restart|force-reload)
	$0 start
	rc_status
    ;;

    reload)
	## acc. to LSB we refuse reload
	echo -n "IPVS reload not supported"
	rc_failed 3
	rc_status -v
    ;;

    save)
	echo -n "saving the current IPVS table to $IPVSADM_CONFIG"
	rm -f "$IPVSADM_CONFIG"
	$ipvsadm-save -n > "$IPVSADM_CONFIG" 2>/dev/null
	rc_status -v
    ;;

    status)
	# Status has a slightly different for the status command:
	# 0 - service running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running
	if [ -d /proc/sys/net/ipv4/vs ] ; then
		echo -n "IPVS module loaded"
		count=$(ipvsadm -L -n 2>/dev/null | \
                        grep -v --count '^$' 2>/dev/null)
		if [ $((count)) -gt 3 ] ; then
			active=$(($count - 3))
			echo -n ", $active rule"
			[ $((active)) -gt 1 ] && echo -n "s"
			echo " active"
		else
			echo ", no rules active"
			rc_failed 3
		fi
	else
		echo "IPVS module not loaded"
		rc_failed 3
	fi
	rc_status -v1
    ;;

    list|show)
	$ipvsadm -L -n
	rc_status
    ;;

    *)
	echo "Usage: $0 {start|stop|status|restart|reload|save|restore}"
	exit 1
    ;;

esac

# Set exit status
rc_exit

