#!/bin/sh
#
# collectd	collectd (collectd daemon)
#
# chkconfig:	345 93 11
#
# description:	utility that colect various system information into rrd files
#
# $Id$

# Source function library
. /etc/rc.d/init.d/functions

# Get service config
[ -f /etc/sysconfig/collectd ] && . /etc/sysconfig/collectd

# Get network config
. /etc/sysconfig/network

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down collectd
		exit 1
	fi
else
	exit 0
fi

checkconfig() {
	/usr/sbin/collectd -t || exit 1
}

start() {
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/collectd ]; then
		msg_starting collectd
		daemon /usr/sbin/collectd
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/collectd
	else
		msg_already_running collectd
	fi
}

stop() {
	if [ -f /var/lock/subsys/collectd ]; then
		msg_stopping collectd
		killproc collectd
		rm -f /var/lock/subsys/collectd >/dev/null 2>&1
	else
		msg_not_running collectd
	fi
}

condrestart() {
	if [ -f /var/lock/subsys/collectd ]; then
		checkconfig
		stop
		start
	else
		msg_not_running collectd
		RETVAL=$1
	fi
}

RETVAL=0
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart)
  	checkconfig
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  force-reload)
	condrestart 7
	;;
  status)
  	checkconfig
	status collectd
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
