acaiafa/opentsdb-cookbook

View on GitHub
templates/default/etc/init.d/opentsdb_rhel.erb

Summary

Maintainability
Test Coverage
#!/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=<%= @options[:opentsdb_resource].instance %>
PROG=/usr/bin/tsdb
HOSTNAME=$(hostname --fqdn)
USER=<%= @options[:opentsdb_resource].user %>

# 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
OUT_FILE=${LOG_FILE}opentsdb.out
ERR_FILE=${LOG_FILE}opentsdb.err

# 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}"

rotate_logs()
{
   RFILE=$1
   NUM_COPIES=$2

   if [[ ! -f $RFILE ]];
   then
       echo "rotate_logs: invalid file - $RFILE"
       return
   fi

   if [[ $NUM_COPIES < 1 ]];
   then
       echo "rotate_logs: invalid number of copies - $NUM_COPIES"
       return
   fi

   i=${NUM_COPIES};
   while [[ $i > 1 ]]
   do
      j=$i;
      i=$((i-1));
      [[ -f "${RFILE}.$i" ]] && mv "${RFILE}.$i" "${RFILE}.$j"
   done
   [[ -f "${RFILE}" ]] && mv "${RFILE}" "${RFILE}.1"
}

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
  : ${LOGBACK_CONFIG_FILE:=<%= @options[:opentsdb_resource].logback_configfile %>}
  : ${JMXPORT:=<%= @options[:opentsdb_resource].jmx_port %>}
  : ${JMXARGS:=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.port=$JMXPORT}
  : ${JVMXMX:=-Xmx6000m}
  <% if @options[:opentsdb_resource].jvm_args %>
  <%= ": ${JVMADD:=" + @options[:opentsdb_resource].jvm_args + "}" %>
  : ${JVMARGS:=-Dlogback.configurationFile=${LOGBACK_CONFIG_FILE} -DLOG_FILE_PREFIX=${LOG_FILE} ${JMXARGS} -enableassertions -enablesystemassertions $JVMXMX -XX:OnOutOfMemoryError=/usr/share/opentsdb/tools/opentsdb_restart.py ${JVMADD} }
  <% else %>
  : ${JVMARGS:=-Dlogback.configurationFile=${LOGBACK_CONFIG_FILE} -DLOG_FILE_PREFIX=${LOG_FILE} ${JMXARGS} -enableassertions -enablesystemassertions $JVMXMX -XX:OnOutOfMemoryError=/usr/share/opentsdb/tools/opentsdb_restart.py}
  <% end %>
  export JVMARGS

  rotate_logs ${OUT_FILE} 5 
  rotate_logs ${ERR_FILE} 5 

  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: ${OUT_FILE} > /dev/null 2>&1
    chown $USER: ${ERR_FILE} > /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>> ${OUT_FILE} 2>> ${ERR_FILE} &"
  else
    # Don't have to change user.
    daemon --pidfile $PID_FILE "$PROG $PROG_OPTS 1>> ${OUT_FILE} 2>> ${ERR_FILE} &"
  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 $?