#! /bin/sh
#
# duct      Duct monitoring agent
#
# chkconfig: 2345 80 30
# description: Duct is a monitoring agent which can create metrics
#              and route them to other services
# processname: master
# pidfile: /var/run/duct.pid
# config: /etc/duct/duct.yml

### BEGIN INIT INFO
# Provides:          duct
# Required-Start:    $remote_fs $network $named
# Required-Stop:     $remote_fs $network $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop duct
# Description:       Duct is a monitoring agent which can create metrics
#                    and route them to other services
### END INIT INFO


PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON="/opt/ducted/bin/twistd"
NAME=duct
DESC=Duct
FDHACK=4096

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

test -x $DAEMON || exit 0

LOGDIR=/var/log
PIDFILE=/var/run/$NAME.pid
#LOGGER="--logfile=${LOGDIR}/duct.log"
LOGGER="--syslog"
DODTIME=2
DAEMON_OPTS="--pidfile=${PIDFILE} ${LOGGER} duct -c /etc/duct/duct.yml"

if [ -f /etc/default/$NAME ]; then
    . /etc/default/$NAME
fi

set -e

ulimit -n $FDHACK

start()
{
    echo -n "Starting $DESC: "
    $DAEMON $DAEMON_OPTS
    RETVAL=$?
    echo
    return $RETVAL
}

stop()
{
    echo -n "Stopping $DESC: "
    killproc -p "$PIDFILE"
    RETVAL=$?
    echo
    return $RETVAL
}

case "$1" in
  start)
        start
	;;
  stop)
        stop
	;;
  restart)
        stop
        start
	;;
  status)
        status -p "$PIDFILE" -l "$NAME" "$DAEMON"
        ;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload|status|force-stop}" >&2
	exit 1
	;;
esac

exit 0
