#!/bin/bash
#
# uls_perfmon
####################
#
STDULSHOSTNAME='perfmon'
STDSECTION=`hostname`
STDCONFIGDIR='/etc/uls/perfmon'
STDDURATION='2h'
STDSAMPLE='15s'
#
ULS_PERFMONPIDFILE=/dev/shm/uls_perfmon.`id -u`.pid
#
if [[ "$1" = "-l" || "$1" = "-k" ]]
 then
  LSTFILE=/dev/shm/uls_perfmon.$$.lst
  if [[ -s $ULS_PERFMONPIDFILE ]]
   then
    awk 'BEGIN { i = 1 }
         { cmdfile = "/proc/"$1"/cmdline"
           if( (getline l < cmdfile) > 0 )
           { split(l, la, "\0")
             if( la[2] ~ /uls_tests/ )
               print i++, $0
           }
           close(cmdfile)
         }' $ULS_PERFMONPIDFILE | tee $LSTFILE | awk '{$2 = ""; print}'
    if [[ -s $LSTFILE ]]
     then
      if [[ "$1" = "-k" ]]
       then
        echo -n "
enter number to kill: "
        read kn
        if grep -q "^$kn " $LSTFILE
         then
          PID=`awk '$1 == "'$kn'" {print $2}' $LSTFILE`
          if [[ -n "$PID" ]]
           then
            kill $PID
          else
            echo "error on getting PID"
          fi
        else
          echo "wrong number"
        fi
      fi
    else
      echo "no sessions active"
    fi
    rm $LSTFILE
  else
    echo "no sessions active"
  fi
  exit 0
fi
#
#
unset ULSHOSTNAME SECTION TESTSTEP CONFIGDIR DURATION TIMEDIFF TMP_PATH
#
while getopts h:s:d:c:C:? op
 do
  case "$op" in
   d) [[ -n "$OPTARG" ]] && TMP_PATH="$OPTARG";;
   h) [[ -n "$OPTARG" ]] && ULSHOSTNAME="$OPTARG";;
   s) [[ -n "$OPTARG" ]] && SECTION="$OPTARG";;
   c) [[ -n "$OPTARG" ]] && CONFIGFILE="$OPTARG";;
   C) [[ -n "$OPTARG" ]] && CONFIGDIR="$OPTARG";;
   *) echo "usage: `basename $0` [-C <configdir>] [-d <path>] [-h <ulshostname>] [-s <section>] [-c <configfile>]"
      echo "       `basename $0` -l|-k"
      exit 1;;
  esac
done
shift $(( $OPTIND - 1))
#

#
if [[ -z "$ULSHOSTNAME" ]]
 then
  echo -n "ULS-Hostname [${ULSHOSTNAME:-$STDULSHOSTNAME}]: "
  read ULSHOSTNAME
fi
#
if [[ -z "$ULSHOSTNAME" ]]
 then
  echo -n "ULS-Section [${SECTION:-$STDSECTION}]: "
  read SECTION
fi
#
echo -n "Measurement duration [${DURATION:-$STDDURATION}]: "
read DURATION
#
echo -n "Sample every [${SAMPLE:-$STDSAMPLE}]: "
read SAMPLE
#
echo -n "Extended settings (y/[n]): "
read YN
#
if [[ ${YN#Y} != ${YN#y} ]]
 then
  #
  if [[ -z "$CONFIGDIR" ]]
   then
    CONFIGDIR=~/uls_perfmon
    echo -n "ULS-Configdir [$CONFIGDIR]: "
    read SETCONFIGDIR
    if [[ -n "$SETCONFIGDIR" ]]
     then
      CONFIGDIR="$SETCONFIGDIR"
    fi
  fi
  if [[ ! -d "$CONFIGDIR" ]]
   then
    if ! mkdir -p "$CONFIGDIR"
     then
      echo "Error: can't create $CONFIGDIR"
      exit
    fi
  fi
  if [[ ! -f "$CONFIGDIR/tests_perfmon.conf" ]]
   then
     sed "s|$STDCONFIGDIR/|$CONFIGDIR/|" "$STDCONFIGDIR/tests_perfmon.conf" >"$CONFIGDIR/tests_perfmon.conf"
  fi
  [[ -f "$CONFIGDIR/procmon.conf" ]] || cp "$STDCONFIGDIR/procmon.conf" "$CONFIGDIR/procmon.conf"
  #
  echo -n "edit tests_perfmon.conf (y/[n]): "
  read YN
  if [[ ${YN#Y} != ${YN#y} ]]
   then
    vi "$CONFIGDIR/tests_perfmon.conf"
  fi
  #
  echo -n "edit procmon.conf (y/[n]): "
  read YN
  if [[ ${YN#Y} != ${YN#y} ]]
   then
    vi "$CONFIGDIR/procmon.conf"
  fi
fi
#
#
if [[ ${DURATION:-$STDDURATION} == *s ]]
 then
  DURATIONSECONDS=`echo ${DURATION:-$STDDURATION} | sed 's/\([0-9]*\).*/\1/'`
elif [[ ${DURATION:-$STDDURATION} == *m ]]
 then
  DURATIONSECONDS=$(( `echo ${DURATION:-$STDDURATION} | sed 's/\([0-9]*\).*/\1/'` * 60 ))
elif [[ ${DURATION:-$STDDURATION} == *h ]]
 then
  DURATIONSECONDS=$(( `echo ${DURATION:-$STDDURATION} | sed 's/\([0-9]*\).*/\1/'` * 3600 ))
else
  DURATIONSECONDS=`echo ${DURATION:-$STDDURATION} | sed 's/\([0-9]*\).*/\1/'`
fi
#
if [[ ${SAMPLE:-$STDSAMPLE} == *s ]]
 then
  SAMPLESECONDS=`echo ${SAMPLE:-$STDSAMPLE} | sed 's/\([0-9]*\).*/\1/'`
elif [[ ${SAMPLE:-$STDSAMPLE} == *m ]]
 then
  SAMPLESECONDS=$(( `echo ${SAMPLE:-$STDSAMPLE} | sed 's/\([0-9]*\).*/\1/'` * 60 ))
elif [[ ${SAMPLE:-$STDSAMPLE} == *h ]]
 then
  SAMPLESECONDS=$(( `echo ${SAMPLE:-$STDSAMPLE} | sed 's/\([0-9]*\).*/\1/'` * 3600 ))
else
  SAMPLESECONDS=`echo ${SAMPLE:-$STDSAMPLE} | sed 's/\([0-9]*\).*/\1/'`
fi
#
if [[ $SAMPLESECONDS -lt 3 ]]
 then
  echo "Warning: On tanking samples every $SAMPLESECONDS s,"
  echo "the performance may degrade or the server may hang."
  echo "Proceed only if you restrict the processes in procmon.conf."
  echo -n "continue (y/[n]): "
  read YN
  if [[ ${YN#Y} = ${YN#y} ]]
   then
    exit 0
  fi
fi
#
NUMSAMPLES=$(( $DURATIONSECONDS / $SAMPLESECONDS ))
#
if [[ $NUMSAMPLES -gt 0 && $SAMPLESECONDS -gt 0 ]]
 then
  #
  if [[ -n "$CONFIGDIR" ]]
   then
    CONFIGTMPDIR=/dev/shm/uls_perfmon.$$.configdir
    cp -a $CONFIGDIR $CONFIGTMPDIR
  else
    unset CONFIGTMPDIR
  fi
  TESTLIST="${CONFIGTMPDIR:-/etc/uls/perfmon}/tests_perfmon.conf"
  #
  echo "starting in background"
  if [[ -n "$TMP_PATH" ]]
   then
    ( /usr/share/ulsclient/uls_tests -h "${ULSHOSTNAME:-$STDULSHOSTNAME}" -s "${SECTION:-$STDSECTION}" -t "$TESTSTEP" -C "$CONFIGTMPDIR" -d "$TMP_PATH" -S "$SAMPLESECONDS" -n "$NUMSAMPLES" "$TESTLIST" &
      CPID=$!
      echo "$CPID `date '+%T'` ${DURATION:-$STDDURATION} ${SAMPLE:-$STDSAMPLE} '${ULSHOSTNAME:-$STDULSHOSTNAME}' '${SECTION:-$STDSECTION}' '$CONFIGTMPDIR'" >>$ULS_PERFMONPIDFILE
      wait
      sed -i "/^$CPID /d" $ULS_PERFMONPIDFILE
     ) &
  else
    ( /usr/share/ulsclient/uls_tests -h "${ULSHOSTNAME:-$STDULSHOSTNAME}" -s "${SECTION:-$STDSECTION}" -t "$TESTSTEP" -C "$CONFIGTMPDIR" -S "$SAMPLESECONDS" -n "$NUMSAMPLES" "$TESTLIST" &
      CPID=$!
      echo "$CPID `date '+%T'` ${DURATION:-$STDDURATION} ${SAMPLE:-$STDSAMPLE} '${ULSHOSTNAME:-$STDULSHOSTNAME}' '${SECTION:-$STDSECTION}' '$CONFIGTMPDIR'" >>$ULS_PERFMONPIDFILE
      wait
      sed -i "/^$CPID /d" $ULS_PERFMONPIDFILE
     ) &
  fi
else
  echo "Error: number of samples ($NUMSAMPLES) or sample time ($SAMPLESECONDS s) is'nt greater 0"
fi
