#!/bin/sh
# generic Startup script for collectl, in case nothing else seems to work!
#

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

COLLECTL=/usr/bin/collectl
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
PROCNAME=$PNAME
PIDFILE="/var/run/$PNAME.pid"

# If a pidfile, make sure it's not stale and if it is, collectl not running
if [ -f $PIDFILE ]; then
    pid=`cat $PIDFILE`
    pid=`ps ax opid,cmd | grep $PROCNAME | grep $pid | grep -v grep | awk '{ print $1 }'`
fi

case "$1" in
   start)
      if [ "$pid" != "" ]; then
        echo "[already running]"
      else
	$COLLECTL -D $SWITCHES $PSWITCH
	echo "."
      fi
      ;;

  stop)
      if [ "$pid" = "" ]; then
          echo "[not running]"
      else
	  count=1
	  while [ $count -le 5 ]; do
	    if [ -f $PIDFILE ]; then
	      kill $pid
	    else
	      count=5;
	      break
            fi
	    sleep 1
	    count=$(( $count + 1 ))
	  done

          if [ -f $PIDFILE ]; then
	    echo -n "  pid $pid not responding to TERM signal.  sending sigkill"
	    kill -9 $pid
	  fi
          echo "."
      fi
      ;;

  flush)
      if [ "$pid" != "" ]; then
	  echo "Flushing buffers for $PNAME"
	  kill -s USR1 $pid
      else
	  echo "$PNAME is not running"
      fi
      ;;

  status)
      if [ "$pid" != "" ]; then
          echo "$PNAME is running..."
      else
          echo "$PNAME is not running"
	  exit 1
      fi
      ;;

  restart|force-reload)
      if [ "$pid" == "" ]; then
          echo "$PNAME does not appear to be running so will not be shut down"
      else
          $0 stop $EXT
      fi
      $0 start "$2" "$3"
      ;;
  *)
	echo "Usage: $0 {start|stop|flush|restart|force-reload|status}"
	exit 1
esac

exit 0

