File ipvsadm.init of Package ipvsadm

#! /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
# Short-Description:	Loads IPVS rules
# Description:    	This shell script takes care of loading IPVS rules.
### END INIT INFO

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

test -f /etc/sysconfig/ipvsadm && . /etc/sysconfig/ipvsadm

# 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.

# Check if needed tools are installed
ipvsadm="/sbin/ipvsadm"
ipvsadm_load="/sbin/ipvsadm-restore"
ipvsadm_save="/sbin/ipvsadm-save"
[ -x $ipvsadm -a -x $ipvsadm_load -a -x $ipvsadm_save ] || {
	[ "$1" = "stop" ] && exit 0 || exit 5
}

empty_config ()
{
  [ -n "$IPVSADM_CONFIG" ] && [ -f $IPVSADM_CONFIG ] && {
    # not configured if empty 
    [ 0 -lt $(grep -v ^# $IPVSADM_CONFIG 2>/dev/null | \
                 grep -v --count ^$) ] || return 6
  } || return 6
  return 0
}

# Source status functions
. /etc/rc.status

# Reset status of this service
rc_reset
case "$1" in

    start|restore|load)
        # check if configured
        empty_config || exit 6

	echo -n "clearing the current IPVS table, "
	$ipvsadm -C
	rc_check

	echo -n "applying IPVS configuration "
	grep -v "^#" "$IPVSADM_CONFIG" 2>/dev/null | \
	grep -v "^$" 2>/dev/null | $ipvsadm_load
	rc_status -v
    ;;

    stop)
        # check if configured
        empty_config || exit 0

	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
	rc_failed 3
	rc_status
    ;;

    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 -n "IPVS module not loaded"
		rc_failed 3
	fi
	rc_status -v
    ;;

    list)
	$ipvsadm -L -n
	rc_status
    ;;

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

esac

# Set exit status
rc_exit