#!/bin/sh
#
# Copyright 2015-2023 VMware, Inc. All rights reserved.
#
# nsx-exporter:
#   Start/Stop the NSX Exporter
#
# Tell chkconfig to start us with starting rank 99 and shutdown rank 2.
# chkconfig: 2345 99 2
# description: NSX Exporter
#


# Load the LSB shell functions
[ -f /lib/lsb/init-functions ] && . /lib/lsb/init-functions

WATCHDOG=/opt/vmware/nsx-exporter/bin/watchdog.sh

PROG=/opt/vmware/nsx-exporter/bin/nsx-exporter
PROG_SOCK_DIR=/var/run/vmware/exporter
PROG_TAG=NSX-EXPORTER
PROG_ARGS=
PUBLIC_CLOUD_CONFIG=/etc/vmware/nsx/public-cloud-config
export LD_LIBRARY_PATH=/opt/vmware/nsx-exporter/lib:/opt/vmware/nsx-aggservice/lib64

nsx_exporter_user="exporter"
nsx_exporter_group="exporter"

if [ -f "$PUBLIC_CLOUD_CONFIG" ]; then
    PROG_ARGS="127.0.0.1 6666"
fi

PROG_MAX_QUICK_RESTARTS="20"
WATCHDOG_ARGS="-d -s ${PROG_TAG} -q ${PROG_MAX_QUICK_RESTARTS}"

test -f /etc/init.d/functions && . /etc/init.d/functions

# <instrumentation> #

nsx_log() {
   echo "${1}"
   logger -p daemon.info -t NSX "${1}"
}

start() {
   groupadd -f $nsx_exporter_group
   if ! getent passwd $nsx_exporter_user > /dev/null; then
      useradd --system -N -g $nsx_exporter_group \
      --shell /usr/sbin/nologin -M --comment "NSX Exporter" $nsx_exporter_user
      if [ $? -ne 0 ]; then
         logger "Failed to add $nsx_exporter_user user"
         exit 1
      fi
   fi

   # Check if it is already running
   if [ -z "$(pidof -s "${PROG}")" ]; then
      mkdir -p "${PROG_SOCK_DIR}"
      chmod 0770 "${PROG_SOCK_DIR}"
      chown -R $nsx_exporter_user:$nsx_exporter_group "${PROG_SOCK_DIR}"
      ${WATCHDOG} ${WATCHDOG_ARGS} "${PROG} ${PROG_ARGS}" >/dev/null 2>&1
      nsx_log "${PROG_TAG} started"
   else
      nsx_log "${PROG_TAG} is already running "
   fi
}

stop() {
   if [ ! -z "$(pidof -s "${PROG}")" ]; then
      # This only stops the watchdog process.
      ${WATCHDOG} -k "${PROG_TAG}"
      local TIMEOUT=50
      local COUNT=0

      # Stop for dump coverage result, need wait for dumping coverage
      if [ "${GCOV_PREFIX}" != "" ]; then
         local COVERAGE_TIMEOUT=20
         nsx_log "Terminating and waiting 20 seconds for coverage data"
         kill "$(pidof -s "${PROG}")" >/dev/null 2>&1
         while [ ! -z "$(pidof -s "${PROG}")" ] ; do
            if [ $((COVERAGE_TIMEOUT--)) -gt 0 ]; then
               sleep 1
            else
               nsx_log "Dumping coverage data may failed"
               break
            fi
         done
      fi

      while [ ! -z "$(pidof -s "${PROG}")" ] ; do
         if [ $((TIMEOUT--)) -gt 0 ]; then
            nsx_log "Attempt to terminate ${PROG_TAG}"
            kill -KILL "$(pidof -s "${PROG}")"
            COUNT=`expr ${COUNT} + 1`
            sleep 2
         else
            nsx_log "Last attempt to terminate ${PROG_TAG}"
            kill -KILL "$(pidof -s "${PROG}")"
            COUNT=`expr ${COUNT} + 1`
            sleep 2
            break
         fi
      done

      if [ ! -z "$(pidof -s "${PROG}")" ] ; then
         nsx_log "${PROG_TAG} failed to stop after ${COUNT} tries"
      else
         nsx_log "${PROG_TAG} stopped after ${COUNT} tries"
      fi
   else
      nsx_log "${PROG_TAG} is not running"
   fi
}

#
# main
#
case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        if [ -n "$(pidof -xs "${PROG}")" ] ; then
            echo "${PROG_TAG} is running"
            exit 0
        else
            echo "${PROG_TAG} is not running"
            # Returning 3 like everyone else.
            exit 3
        fi
        ;;
  restart)
        stop
        start
        ;;
  clear)
        stop
        clear
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 3
esac

