#!/bin/sh
# Startup script for collectl on distros that support update-rc.d such
# as debian & ubuintu
#

### BEGIN INIT INFO
# Provides:          collectl
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Collectl monitors system performance.
# Description:       Collectl is a light-weight performance monitoring
#                    tool capable of reporting interactively as well
#                    as logging to disk. It reports statistics on
#                    cpu, disk, infiniband, lustre, memory, network,
#                    nfs, process, quadrics, slabs and more in easy
#                    to read format.
### END INIT INFO

# description: Run data collection for a number of subsystems
#    see /etc/collectl.conf for startup options
#
# EXAMPLES:
#  run at 1 second interval and only collect cpu/disk data
#    /etc/init.d/collectl start "-i1 -scd"
#  run a second instance with instance name of 'int5', with interval of 5 seconds
#    /etc/init.d/collectl start int5 "-i5"

PERL=/usr/bin/perl
COLLECTL=/usr/bin/collectl

if [ ! -f $PERL ]; then
    echo -n "Cannot find $PERL"
    exit 0
fi

if [ ! -f $COLLECTL ]; then
    echo -n "Cannot find $COLLECTL"
    exit 0
fi

PNAME=collectl
if [ "$2" != "" ]; then
    EXT=$2
    if [ "$1" = "start" ] || [ "$1" = "restart" ] || [ "$1" = "force-reload" ]; then
        if [ "$3" = "" ]; then
            SWITCHES=$2
            EXT=""
        else
            SWITCHES=$3
        fi
    fi

    # Just to make sure nothing is different when running 'collectl', we
    # won't use --check even though it's probably ok to use all the time.
    if [ "$EXT" != "" ]; then
        PNAME="collectl-$EXT"
        PSWITCH="--pname $EXT"
        CHECK="--check $PNAME "
    fi
fi
PIDFILE="/var/run/$PNAME.pid"

case "$1" in
   start)
      echo -n "Starting collectl: $PNAME"
      start-stop-daemon --quiet --stop --exec $PERL --pidfile $PIDFILE --test >/dev/null
      if [ $? -eq 1 ]; then
         start-stop-daemon --quiet --start --exec $COLLECTL -- -D $SWITCHES $PSWITCH
	 echo "."
      else
	 echo " [already running]"
      fi
      ;;

  stop)
      echo -n "Stopping collectl: $PNAME"

      start-stop-daemon --quiet --stop --retry 2 --exec $PERL --pidfile $PIDFILE
      if [ $? -eq 0 ]; then
	 echo "."
      else
	 echo " [not running]"
      fi
      ;;

  flush)
      start-stop-daemon --quiet --stop --exec $PERL --pidfile $PIDFILE --test >/dev/null
      if [ $? -eq 0 ]; then
	  echo "Flushing buffers for $PNAME"
	  kill -s USR1 `cat $PIDFILE`
      else
	  echo "$PNAME is not running"
      fi
      ;;

  status)
      start-stop-daemon --quiet --stop --exec $PERL --pidfile $PIDFILE --test >/dev/null
      if [ $? -eq 0 ]; then
          echo "$PNAME is running..."
      else
          echo "$PNAME is not running"
	  exit 1
      fi
      ;;

  restart|force-reload)
      $0 stop $EXT
      sleep 1
      $0 start "$2" "$3"
      ;;
  *)
	echo "Usage: $0 {start|stop|flush|restart|force-reload|status}"
	exit 1
esac

exit 0

