#! /bin/sh
#  ------------------------------------------------------------
#  Company: Software- und Organisations-Service GmbH
#  Purpose: Java metrics by jstat for JS7 Controller
#  ------------------------------------------------------------

set -e

##########################
# jstat metrics
# see https://docs.oracle.com/en/java/javase/17/docs/specs/man/jstat.html
# set this value of JS7_METRICS to 1 if jstat is to be called when the Controller starts
# Set JAVA_HOME in controller_instance.sh if jstat is not in the path 
JS7_METRICS=0

call_jstat()
{
  JSTATLOG="${JS7_CONTROLLER_LOGS}/jstat.log"
  JSTATBIN=$(which jstat 2>/dev/null || echo "")
  test -n "$JAVA_HOME" && test -x "$JAVA_HOME/bin/jstat" && JSTATBIN="$JAVA_HOME/bin/jstat"
  if [ -z "${JSTATBIN}" ]
  then
    echo "...could not find jstat" >>"${JSTATLOG}"
    exit 1
  fi
  
  printf "\n------------ %s - PID %s ------------------------------------------------------------------------------------\n" "$(date +"%F %T")" "${JS7_CONTROLLER_PID}" >>"${JSTATLOG}"
  "${JSTATBIN}" -gc -t ${JS7_CONTROLLER_PID} 60000 >>"${JSTATLOG}" 2>&1
}

test "${JS7_METRICS}" -eq 1 && call_jstat

set +e
exit 0
