#!/bin/sh
#
# start/stop the probe monitor daemon.
#
### BEGIN INIT INFO
# Provides:          probe_monitor
# Required-Start:    $syslog $time
# Required-Stop:     $syslog $time
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts/stops the probe monitor daemon.
# Description:       Starts and stops the probe monitor daemon so that
#                    condor class ads can updated with resource performance
#                    data
### END INIT INFO

# Source function library.
if [ -f /etc/init.d/functions ]; then
   . /etc/init.d/functions
fi

if ! [ -x /opt/submit/monitorProbe.py ]; then
   exit 1
fi

case "$1" in
   start)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Starting probe monitor:"
         echo -n " probemon" ; pgrep -fl '/opt/submit/monitorProbe.py' > /dev/null 2>&1 || /sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec /opt/submit/monitorProbe.py
      else
         echo -n " probemon" ; daemon --check monitorProbe --user=gridman /opt/submit/monitorProbe.py
      fi
      echo "."
      ;;
    stop)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Stopping probe monitor:"
      fi
      echo -n " probemon" ; pkill -TERM -u gridman -f '/opt/submit/monitorProbe.py'
      echo "."
      ;;
    restart|force-reload)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Restarting probe monitor:"
      fi
      echo -n " probemon"
      pkill -TERM -u gridman -f '/opt/submit/monitorProbe.py'
      if [ -f /sbin/start-stop-daemon ]; then
         /sbin/start-stop-daemon --start --quiet --chdir /opt/submit --chuid gridman --exec /opt/submit/monitorProbe.py
      else
         daemon --check monitorProbe --user=gridman /opt/submit/monitorProbe.py
      fi
      echo "."
      ;;
       *)
      echo "Usage: /etc/init.d/probemon {start|stop|restart|force-reload}"
      exit 1
      ;;
esac

exit 0
