#!/bin/sh
# Startup script for collectl on SuSE based distributions
#
# description: Run data collection for a number of subsystems
#    see /etc/collectl.conf for startup options
#    $2: process name, if other than collectl OR "" to use $3
#    $3: parameters, in quotes if more than one, to pass to daemon command
#
### BEGIN INIT INFO
# Provides:          collectl
# Required-Start:    $ALL
# Required-Stop:     $ALL
# Default-Start:     2 3 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"

[ -r /etc/rc.status ] && . /etc/rc.status
rc_reset
COLLECTL=/usr/sbin/collectl
if [ ! -f $COLLECTL ]; then
    echo -n "Cannot find $COLLECTL"
    rc_status -s
    rc_exit
fi

PATH=/bin:/usr/bin:/sbin:/usr/sbin
export PATH

# 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.
PNAME=collectl
if [ "$2" != "" ]; then
    EXT=$2
    if [ "$1" = "start" ] || [ "$1" = "restart" ] ; 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 $PNAME already running
	  exit
      fi

      # we used to start with 'startproc', but if an instance of collectl already running it
      # won't start the next one so we'll just start it this way
      echo -n "Starting $PNAME:"
      $COLLECTL -D $SWITCHES $PSWITCH
      rc_status -v
	;;

  stop)
      # Note that we need to use a pid file to identify which instance we want to stop
      if [ -f $PIDFILE ]; then
          echo -n "Shutting down $PNAME: "
          killproc -p $PIDFILE collectl
	  rc_status -v
      else
          echo "$PNAME not running"
      fi
	;;

  flush)
      if [ -f $PIDFILE ]; then
	  pid=`cat $PIDFILE`
	  echo Flushing buffers for $PNAME
	  kill -s USR1 $pid
      else
          echo "$PNAME not running"
      fi
      rc_status
      ;;

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

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

rc_exit
