#!/bin/sh
# Startup script for collectl
#

### 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

# chkconfig: 345 99 99
# description: Run data collection for a number of subsystems
#    see /etc/collectl.conf for startup options
#      $2: process name extension, if other than collectl [optional for start/restart/reload]
#      $3: parameters, in quotes if more than one, to pass to daemon command
#
# 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"

RETVAL=0
COLLECTL=/usr/bin/collectl
. /etc/rc.d/init.d/functions

# older distros do not support "status -p" and we need to know if this one does
STATUS=`status` 
if [[ $STATUS = *-p* ]]; then IFLAG=1; else IFLAG=0; fi
if [ $IFLAG -eq 0 ] && [ "$3" != '' ]; then 
    echo "starting multiple instances with this distro is not supported";
    exit
fi

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

PNAME=collectl
if [ "$2" != "" ]; then
    EXT=$2
    if [ "$1" = "start" ] || [ "$1" = "restart" ] || [ "$1" = "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)
      COMMAND="$COLLECTL -D $SWITCHES $PSWITCH"
      echo -n "Starting $PNAME:"
      daemon $CHECK$COMMAND
      RETVAL=$?
      echo
      [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$PNAME
      ;;

  stop)
      if [ -f $PIDFILE ]; then
          echo -n "Shutting down $PNAME: "
          killproc $PNAME
          RETVAL=$?
          echo
          [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$PNAME
      else
          echo "$PNAME does not appear to be running so will not be shut down"
      fi
      ;;

  flush)
      if [ -f $PIDFILE ]; then
	  pid=`cat $PIDFILE`
	  echo Flushing buffers for $PNAME
	  kill -s USR1 $pid
      else
	  echo "Can't find pid file $PIDFILE"
      fi
      ;;

  restart|reload)
   	$0 stop $EXT
   	$0 start "$2" "$3"
   	RETVAL=$?
	;;
  status)
        # need to use pid file since there can be multiple instances, but only if
        # this distro supports it
        if [ $IFLAG -eq 1 ]; then
	    status -p $PIDFILE
	else
	    status $PNAME
        fi 
   	RETVAL=$?
	;;
  *)
	echo "Usage: $0 {start|stop|flush|restart|status}"
	exit 1
esac

exit $RETVAL
