daily pastebin goal
46%
SHARE
TWEET

logstash_multiple

a guest Jan 22nd, 2016 2,209 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2. # Init script for logstash
  3. # Maintained by Elasticsearch
  4. # Generated by pleaserun.
  5. # Modified by Gregory GERARD.
  6. # Implemented based on LSB Core 3.1:
  7. #   * Sections: 20.2, 20.3
  8. #
  9. ### BEGIN INIT INFO
  10. # Provides:          logstash
  11. # Required-Start:    $remote_fs $syslog
  12. # Required-Stop:     $remote_fs $syslog
  13. # Default-Start:     2 3 4 5
  14. # Default-Stop:      0 1 6
  15. # Short-Description:
  16. # Description:        Starts Logstash as a daemon.
  17. ### END INIT INFO
  18.  
  19. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  20. export PATH
  21.  
  22. if [ `id -u` -ne 0 ]; then
  23.    echo "You need root privileges to run this script"
  24.    exit 1
  25. fi
  26.  
  27. name=logstash
  28. pidfile="/var/run/$name.pid"
  29.  
  30. LS_USER=logstash
  31. LS_GROUP=logstash
  32. LS_HOME=/var/lib/logstash
  33. LS_HEAP_SIZE="500m"
  34. LS_LOG_DIR=/var/log/logstash
  35. LS_LOG_FILE="${LS_LOG_DIR}/$name.log"
  36. LS_CONF_DIR=/etc/logstash/conf.d
  37. LS_OPEN_FILES=16384
  38. LS_NICE=19
  39. LS_OPTS=""
  40.  
  41.  
  42. [ -r /etc/default/$name ] && . /etc/default/$name
  43. [ -r /etc/sysconfig/$name ] && . /etc/sysconfig/$name
  44.  
  45. program=/opt/logstash/bin/logstash
  46. args="agent -f ${LS_CONF_DIR} -l ${LS_LOG_FILE} ${LS_OPTS}"
  47.  
  48. quiet() {
  49.   "$@" > /dev/null 2>&1
  50.   return $?
  51. }
  52.  
  53.  
  54.  
  55. start() {
  56.  
  57.   LS_JAVA_OPTS="${LS_JAVA_OPTS} -Djava.io.tmpdir=${LS_HOME}"
  58.   HOME=${LS_HOME}
  59.   export PATH HOME LS_HEAP_SIZE LS_JAVA_OPTS LS_USE_GC_LOGGING
  60.  
  61.   # chown doesn't grab the suplimental groups when setting the user:group - so we have to do it for it.
  62.   # Boy, I hope we're root here.
  63.   SGROUPS=$(id -Gn "$LS_USER" | tr " " "," | sed 's/,$//'; echo '')
  64.  
  65.   if [ ! -z $SGROUPS ]
  66.   then
  67.     EXTRA_GROUPS="--groups $SGROUPS"
  68.   fi
  69.  
  70.   # set ulimit as (root, presumably) first, before we drop privileges
  71.   ulimit -n ${LS_OPEN_FILES}
  72.  
  73.  
  74.   for logstash_file_full in ${LS_CONF_DIR}/*; do
  75.       logstash_file_simple=$(basename $logstash_file_full .conf)
  76.  
  77.       LS_LOG_FILE="${LS_LOG_DIR}/$(basename "$logstash_file_simple").log"
  78.  
  79.       args="agent -f ${logstash_file_full} -l ${LS_LOG_FILE} ${LS_OPTS}"
  80.       pidfile="/var/run/$logstash_file_simple.pid"
  81. #      echo ${args}             $pidfile
  82.  
  83.      # Run the program!
  84.      nice -n ${LS_NICE} chroot --userspec $LS_USER:$LS_GROUP $EXTRA_GROUPS / sh -c "
  85.     cd $LS_HOME
  86.     ulimit -n ${LS_OPEN_FILES}
  87.     exec \"$program\" $args
  88.     " > "${LS_LOG_DIR}/$logstash_file_simple.stdout" 2> "${LS_LOG_DIR}/$logstash_file_simple.err" &
  89.  
  90.  
  91.   # Generate the pidfile from here. If we instead made the forked process
  92.   # generate it there will be a race condition between the pidfile writing
  93.   # and a process possibly asking for status.
  94.     echo $! > $pidfile
  95.  
  96.     echo "$name $logstash_file_simple started."
  97.   done
  98.   return 0
  99. }
  100.  
  101.  
  102. stop() {
  103.   # Try a few times to kill TERM the program
  104.  
  105.     for logstash_file_full in ${LS_CONF_DIR}/*; do
  106.       logstash_file_simple=$(basename $logstash_file_full .conf)
  107.       pidfile="/var/run/$logstash_file_simple.pid"
  108.  
  109.       if status $logstash_file_full; then
  110.  
  111.  
  112.         pid=`cat "$pidfile"`
  113.  
  114.         echo "Killing $name (pid $pid) with SIGTERM"
  115.         kill -TERM $pid
  116.         # Wait for it to exit.
  117.         for i in 1 2 3 4 5 ; do
  118.           echo "Waiting $name (pid $pid) to die..."
  119.           status $logstash_file_full || break
  120.           sleep 1
  121.         done
  122.         if status $logstash_file_full; then
  123.           if [ "$KILL_ON_STOP_TIMEOUT" -eq 1 ] ; then
  124.             echo "Timeout reached. Killing $logstash_file_simple (pid $pid) with SIGKILL. This may result in data loss."
  125.             kill -KILL $pid
  126.             echo "$logstash_file_simple killed with SIGKILL."
  127.           else
  128.             echo "$logstash_file_simple stop failed; still running."
  129.           fi
  130.         else
  131.           echo "$logstash_file_simple stopped."
  132.         fi
  133.       fi
  134.     done
  135. }
  136.  
  137.  
  138. status() {
  139.   logstash_file_simple=$(basename $1 .conf)
  140.   pidfile="/var/run/$logstash_file_simple.pid"
  141.  
  142.   if [ -f "$pidfile" ] ; then
  143.     pid=`cat "$pidfile"`
  144.     if kill -0 $pid > /dev/null 2> /dev/null ; then
  145.       # process by this pid is running.
  146.       # It may not be our pid, but that's what you get with just pidfiles.
  147.       # TODO(sissel): Check if this process seems to be the same as the one we
  148.       # expect. It'd be nice to use flock here, but flock uses fork, not exec,
  149.       # so it makes it quite awkward to use in this case.
  150.       return 0
  151.     else
  152.       return 2 # program is dead but pid file exists
  153.     fi
  154.   else
  155.     return 3 # program is not running
  156.   fi
  157. }
  158.  
  159. force_stop() {
  160.   for logstash_file_full in ${LS_CONF_DIR}/*; do
  161.     logstash_file_simple=$(basename $logstash_file_full .conf)
  162.     status $logstash_file_full
  163.     if status ; then
  164.       stop
  165.       if status; then
  166.           pidfile="/var/run/$logstash_file_simple.pid"
  167.           kill -KILL `cat "$pidfile"`
  168.       fi
  169.     fi
  170.   done
  171. }
  172.  
  173. configtest() {
  174.   # Check if a config file exists
  175.   if [ ! "$(ls -A ${LS_CONF_DIR}/* 2> /dev/null)" ]; then
  176.     log_failure_msg "There aren't any configuration files in ${LS_CONF_DIR}"
  177.     exit 1
  178.   fi
  179.  
  180.   JAVA_OPTS=${LS_JAVA_OPTS}
  181.   HOME=${LS_HOME}
  182.   export PATH HOME JAVA_OPTS LS_HEAP_SIZE LS_JAVA_OPTS LS_USE_GC_LOGGING
  183.  
  184.   test_args="-f ${LS_CONF_DIR} --configtest ${LS_OPTS}"
  185.   $program ${test_args}
  186.   [ $? -eq 0 ] && return 0
  187.   # Program not configured
  188.   return 6
  189. }
  190.  
  191. case "$1" in
  192.   start)
  193.     status
  194.     code=$?
  195.     if [ $code -eq 0 ]; then
  196.       echo "$name is already running"
  197.     else
  198.       start
  199.       code=$?
  200.     fi
  201.     exit $code
  202.     ;;
  203.   stop) stop ;;
  204.   force-stop) force_stop ;;
  205.   status)
  206.     for logstash_file_full in ${LS_CONF_DIR}/*; do
  207.       status $logstash_file_full
  208.       code=$?
  209.       if [ $code -eq 0 ] ; then
  210.         echo "$logstash_file_full is running"
  211.       else
  212.         echo "$logstash_file_full is not running"
  213.       fi
  214.     done
  215.     exit $code
  216.     ;;
  217.   restart)
  218.  
  219.     quiet configtest
  220.     RET=$?
  221.     if [ ${RET} -ne 0 ]; then
  222.       echo "Configuration error. Not restarting. Re-run with configtest parameter for details"
  223.       exit ${RET}
  224.     fi
  225.     stop && start
  226.     ;;
  227.   configtest)
  228.     configtest
  229.     exit $?
  230.     ;;
  231.   *)
  232.     echo "Usage: $SCRIPTNAME {start|stop|force-stop|status|restart|configtest}" >&2
  233.     exit 3
  234.   ;;
  235. esac
  236.  
  237. exit $?
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top