#!/bin/sh
#
# telegraf	Telegraf daemon.
#
# chkconfig: - 90 10
# description:	Telegraf is an agent for collecting, processing, aggregating, and writing metrics.
# processname: telegraf
# config: /etc/telegraf/telegraf.conf
# pidfile: /run/telegraf/telegraf.pid
### BEGIN INIT INFO
# Provides:          telegraf
# Required-Start:    $network
# Required-Stop:     $remote_fs $syslog
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: Telegraf daemon.
### END INIT INFO

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

USER=telegraf
PIDFILE=/run/telegraf/telegraf.pid
LOCKFILE=/var/lock/subsys/telegraf
CONFIG=/etc/telegraf/telegraf.conf
CONFDIR=/etc/telegraf/telegraf.d
DAEMON=telegraf
SourceIfNotEmpty /etc/sysconfig/telegraf
RETVAL=0

start()
{
	start_daemon --background --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --user "$USER" -- $DAEMON --pidfile "$PIDFILE" --config "$CONFIG" --config-directory "$CONFDIR" "$TELEGRAF_OPTS"
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user "$USER" -- $DAEMON
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
	msg_reloading telegraf
	stop_daemon --pidfile "$PIDFILE" --expect-user "$USER" -HUP -- $DAEMON
	RETVAL=$?
	return $RETVAL
} 

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user "$USER" -- $DAEMON
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
