#!/bin/sh
#
# opentsdb      This shell script takes care of starting and stopping OpenTSDB.
#
# chkconfig: 35 99 01
# description: OpenTSDB is a distributed, scalable Time Series Database (TSDB) \
# written on top of HBase. OpenTSDB was written to address a common need: store, \
# index and serve metrics collected from computer systems (network gear, operating \
# systems, applications) at a large scale, and make this data easily accessible \
# and graphable.
#
# Based in part on a shell script by Jacek Masiulaniec at
# https://github.com/masiulaniec/opentsdb-rhel/blob/master/src/tsdb-server.init.

### BEGIN INIT INFO
# Provides: opentsdb
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Short-Description: start and stop opentsdb
# Description: OpenTSDB is a distributed, scalable Time Series Database (TSDB)
#              written on top of HBase. OpenTSDB was written to address a
#              common need: store, index and serve metrics collected from
#              computer systems (network gear, operating systems, applications)
#              at a large scale, and make this data easily accessible and
#              graphable.
### END INIT INFO

# Source init functions
. /etc/init.d/functions

# Maximum number of open files
MAX_OPEN_FILES=65535

# Default program options
NAME=opentsdb
PROG=/usr/bin/tsdb
HOSTNAME=$(hostname --fqdn)
USER=root

# Default directories
LOG_DIR=/var/log/opentsdb
LOCK_DIR=/var/lock/subsys
PID_DIR=/var/run/opentsdb

# Global and Local sysconfig files
[ -e /etc/sysconfig/opentsdb ] && . /etc/sysconfig/opentsdb
[ "`basename $0`" != "$NAME" ] && [ -e /etc/sysconfig/`basename $0` ] && . /etc/sysconfig/`basename $0`

# Set file names
LOG_FILE=$LOG_DIR/$NAME-$HOSTNAME-
LOCK_FILE=$LOCK_DIR/$NAME
PID_FILE=$PID_DIR/$NAME.pid
CONFIG=/etc/opentsdb/${NAME}.conf

# Create dirs if they don't exist
[ -e $LOG_DIR ] || (mkdir -p $LOG_DIR && chown $USER: $LOG_DIR)
[ -e $PID_DIR ] || mkdir -p $PID_DIR

PROG_OPTS="tsd --config=${CONFIG}"

start() {
  echo -n "Starting ${NAME}: "
  curid="`id -u -n`"
  if [ "$curid" != root ] && [ "$curid" != "$USER" ] ; then
    echo "Must be run as root or $USER, but was run as $curid"
    return 1
  fi
  # Sets the maximum number of open file descriptors allowed.
  ulimit -n $MAX_OPEN_FILES
  curulimit="`ulimit -n`"
  if [ "$curulimit" -lt $MAX_OPEN_FILES ] ; then
    echo "'ulimit -n' must be greater than or equal to $MAX_OPEN_FILES, is $curulimit"
    return 1
  fi

  # Set a default value for JVMARGS
  : ${JVMXMX:=-Xmx6000m}
  : ${JVMARGS:=-DLOG_FILE=${LOG_FILE}opentsdb.log -DQUERY_LOG=${LOG_FILE}queries.log -enableassertions -enablesystemassertions $JVMXMX -XX:OnOutOfMemoryError=/usr/share/opentsdb/tools/opentsdb_restart.py}
  export JVMARGS

  if [ "`id -u -n`" == root ] ; then
    # Changes the owner of the log directory to allow non-root OpenTSDB
    # daemons to create and rename log files.
    chown $USER: $LOG_DIR > /dev/null 2>&1
    chown $USER: ${LOG_FILE}*opentsdb.log > /dev/null 2>&1
    chown $USER: ${LOG_FILE}*queries.log > /dev/null 2>&1
    chown $USER: ${LOG_FILE}opentsdb.out > /dev/null 2>&1
    chown $USER: ${LOG_FILE}opentsdb.err > /dev/null 2>&1

    # Changes the owner of the lock, and the pid files to allow
    # non-root OpenTSDB daemons to run /usr/share/opentsdb/bin/opentsdb_restart.py.
    touch $LOCK_FILE && chown $USER: $LOCK_FILE
    touch $PID_FILE && chown $USER: $PID_FILE
    daemon --user $USER --pidfile $PID_FILE "$PROG $PROG_OPTS 1>> ${LOG_FILE}opentsdb.out 2>> ${LOG_FILE}opentsdb.err &"
  else
    # Don't have to change user.
    daemon --pidfile $PID_FILE "$PROG $PROG_OPTS 1>> ${LOG_FILE}opentsdb.out 2>> ${LOG_FILE}opentsdb.err &"
  fi
  retval=$?
  sleep 2
  echo
  [ $retval -eq 0 ] && (findproc > $PID_FILE && touch $LOCK_FILE)
  return $retval
}

stop() {
  echo -n "Stopping ${NAME}: "
  killproc -p $PID_FILE $NAME
  retval=$?
  echo
  # Non-root users don't have enough permission to remove pid and lock files.
  # So, the opentsdb_restart.py cannot get rid of the files, and the command
  # "service opentsdb status" will complain about the existing pid file.
  # Makes the pid file empty.
  echo > $PID_FILE
  [ $retval -eq 0 ] && (rm -f $PID_FILE && rm -f $LOCK_FILE)
  return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

force_reload() {
    restart
}

rh_status() {
    # run checks to determine if the service is running or use generic status
    status -p $PID_FILE -l $LOCK_FILE $NAME
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

findproc() {
    pgrep -f "java .* net.opentsdb.tools.TSDMain .*${NAME}"
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?
