#!/bin/bash

# This program is Copyright (c) SolarWinds Worldwide, LLC. All rights reserved.

VERSION=unknown

### BEGIN INIT INFO
# Provides:          vividcortex
# Required-Start:    $remote_fs $local_fs
# Required-Stop:     $remote_fs $local_fs
# Should-Start:      $syslog $network $named $time
# Should-Stop:       $syslog $network $named $time
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: SolarWinds DPM supervisor daemon
# Description:       agent_007 is the main SolarWinds DPM daemon. It's in charge
#                    of starting other agents as required and updating
#                    binaries when new versions are available.
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin

DESC="SolarWinds DPM supervisor daemon"
NAME=vc-agent-007
SERVICE=vividcortex
BIN_PATH=/usr/local/bin
DAEMON=$BIN_PATH/$NAME
SUPERVISOR_ARGS="-startup-network-retries=10 -startup-network-retry-interval=10s"
AGENTS="vc-aggregator vc-es-query vc-es-metrics vc-mongo-query vc-mongo-metrics vc-mssql-metrics vc-mysql-query vc-mysql-metrics vc-os-metrics vc-pgsql-query vc-pgsql-metrics vc-redis-query vc-redis-metrics vc-vitess-metrics"
set_conf_file=0
if [ "${set_conf_file}" != "0" ]; then
	SUPERVISOR_ARGS="${SUPERVISOR_ARGS} -config-file=/etc/vividcortex/global.conf"
fi


# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read optional config file
[ -r "/etc/sysconfig/$SERVICE" ] && . /etc/sysconfig/$SERVICE

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

# Set HOME in case it isn't or points to a user dir (sudo)
HOME=~root
test -d "${HOME}" || HOME=/root
test -d "${HOME}" || HOME=
export HOME

lockfile="/var/lock/subsys/${SERVICE}"
pidfile="/var/run/${NAME}.pid"
RETVAL=0

start() {
	echo -n $"Starting ${NAME}: "
	if [ -f "${lockfile}" ]; then
		if [ -s "${pidfile}" ]; then
			PID=$(cat "${pidfile}")
			if ps --pid="${PID}" | grep "${NAME}" > /dev/null 2>&1 ; then
				echo "already running (pid ${PID})"
				return 0
			fi
		fi
	fi
	rm -f "${lockfile}" "${pidfile}"
	daemon --pidfile "${pidfile}" "${DAEMON}" ${SUPERVISOR_ARGS}
	RETVAL=$?
	test ${RETVAL} -eq 0 && touch "${lockfile}"
	echo
	return $RETVAL
}

stop() {
	# Return
	#   0 if daemon has been stopped
	#   7 if daemon was already stopped
	#   other if a failure occurred

	# Kill vc-agent-007, return if that cannot be done. (If it wasn't running, that's OK.)
	# (Trying to kill the other agents if vc-agent-007 won't die will only lead to madness.)
	echo -n "Stopping $NAME: "
	rm -f "${lockfile}"
	killproc "$DAEMON"
	RETVAL=$?
	[ "$RETVAL" -ne 0 -a "$RETVAL" -ne 7 ] && return "$RETVAL"
	echo -n "${NAME}" | grep -q '^vc-[0-9A-Za-z-]\+$' && rm -f "/var/lock/${NAME}"* "/var/run/${NAME}"* "/var/lock/vividcortex/${NAME}"* "/var/run/vividcortex/${NAME}"*

	# Can't use killproc() for individual agents: it will emit failure
	# messages when attempting to stop any that are not currently running.
	PIDLIST=()
	for a in $AGENTS; do
		pids=`pidofproc "${BIN_PATH}/${a}"`
		# pidofproc() can return more than one PID.
		for pid in ${pids}; do
			PIDLIST=("${PIDLIST[@]}" "${pid}")
		done
	done

	if [ "${#PIDLIST[@]}" -gt 0 ]; then
		# Kill all child agents in one fell swoop.
		kill -TERM "${PIDLIST[@]}" > /dev/null 2>&1

		# Wait a short bit to allow agents to exit politely...
		sleep 2
		# ...and then keep retrying for 5 seconds with -KILL if they won't.
		count=0
		TOKILL=$(ps -o pid= ${PIDLIST[@]})
		while [ -n "${TOKILL}" -a "${count}" -lt 5 ]; do
			kill -KILL "${TOKILL}" > /dev/null 2>&1
			sleep 1
			TOKILL=$(ps -o pid= ${PIDLIST[@]})
			count=$((count + 1))
		done
	fi
    
	# Just in case there's something old left behind.
	for a in $AGENTS; do
		if echo -n "${a}" | grep -q '^vc-[0-9A-Za-z-]\+$'; then
			rm -rf "/var/run/vividcortex/${a}"*
			rm -f "/var/lock/${a}"* "/var/run/${a}"* "/var/lock/vividcortex/${a}"*
		fi
	done

	echo
	return 0
}

restart() {
	stop
	start
}


case "$1" in
start)
	start
	;;
stop)
	stop
	;;
restart|force-reload)
	restart
	;;
reload)
	RETVAL=3
	;;
status)
	status "$NAME"
	RETVAL=$?
	;;
*)
	echo $"Usage: $0 {start|stop|restart|force-reload|status}"
	RETVAL=2
	;;
esac

exit $RETVAL
