#! /bin/sh
#
# vmware-netdumper       Start/Stop the VMware network core dumper server
#
# chkconfig: 345 80 05
# description: VMware network core dump server
# processname: vmware-netdumper

### BEGIN INIT INFO
# Provides: vmware-netdumper
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: 
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: VMware Network Coredump Server
### END INIT INFO

# if the '/etc/profile' file exists source it, since it contains CIS related
# variables that were provided during CIS deployment.
[ -f /etc/profile ] && . /etc/profile

prog="/usr/lib/vmware-netdumper/webserver/vmware-netdumper-webserver"
prog_shortname="VMware ESXi dump collector"

[ -f /etc/sysconfig/netdumper ] && . /etc/sysconfig/netdumper

WATCHDOG="/usr/bin/vmware-watchdog"
NETDUMPER_TAG=netdumper
. /etc/rc.status
rc_reset
case "$1" in
    start)
	if $WATCHDOG -r $NETDUMPER_TAG >/dev/null 2>&1 ; then
	    echo "Netdumper is already started!"
	    $0 status
	else
	    echo -n "Starting $prog_shortname: "
            # workaround for PR 665296 - mkdir on nfs returns permission denied instead of already exists
	    [ -x /usr/bin/stat ] && /usr/bin/stat "$NETDUMPER_DIR" 1>/dev/null 2>&1
	    # redirecting stdout and stderror in order to prevent blocking in
	    # subprocess.Popen(cmd).communicate()
	    $WATCHDOG -s $NETDUMPER_TAG -u 3 -q 5 -i 3 -t 100000 "su -s /bin/bash netdumper -c $prog" 1>/dev/null 2>&1 &
	    rc_status -v
	fi
	;;
    stop)
	if $WATCHDOG -r $NETDUMPER_TAG >/dev/null 2>&1; then
	    echo -n "Stopping watchdog: "
	    $WATCHDOG -k $NETDUMPER_TAG
	    rc_status -v
	else
	    echo "watchdog  is not running"
	fi
	if pgrep -f netdumper-webserver >/dev/null 2>&1; then
	    echo -n stopping webserver...
	    pkill -f "$prog"
	    rc_status -v
	else
	    echo "webserver is not running"
	fi
	if pgrep -f "vmware-netdumper -d" >/dev/null 2>&1; then
	    echo -n stopping logdumper...
	    pkill -f "/usr/sbin/vmware-netdumper"
	    rc_status -v
	else
	    echo "netdumper is not running"
	fi
	;;
    restart)
	$0 stop
	$0 start
	rc_status -v
	;;
    status)
	# status should have exit status 0 when service is running -
	# see vmware-sca/scripts/vmware-netdump.sh
	if $WATCHDOG -r $NETDUMPER_TAG >/dev/null 2>&1 ; then
	    echo "watchdog  is     running"
	else
	    echo "watchdog  is not running"
	    rc_failed
	fi
	if pgrep -f netdumper-webserver >/dev/null 2>&1; then
	    echo "webserver is     running"
	else
	    echo "webserver is not running"
	    rc_failed
	fi
	if pgrep -f "vmware-netdumper -d" >/dev/null 2>&1; then
	    echo "logdupmer is     running"
	else
	    echo "logdumper is not running"
	    rc_failed
	fi
	;;
    *)
	echo "usage: $0 {start|stop|restart|status}"
	exit 1
esac

rc_exit
