#!@l_prefix@/bin/openpkg rc
##
##  rc.logstash -- Run-Commands
##

%config
    logstash_enable="$openpkg_rc_def"
    logstash_flags=""
    logstash_log_prolog="true"
    logstash_log_epilog="true"
    logstash_log_numfiles="10"
    logstash_log_minsize="1M"
    logstash_log_complevel="9"

%common
    logstash_cfgfile="@l_prefix@/etc/logstash/logstash.conf"
    logstash_pidfile="@l_prefix@/var/logstash/run/logstash.pid"
    logstash_logfile="@l_prefix@/var/logstash/log/logstash.log"
    logstash_signal () {
        [ -f $logstash_pidfile ] && kill -$1 `cat $logstash_pidfile`
    }

%status -u @l_rusr@ -o
    logstash_usable="unknown"
    logstash_active="no"
    rcService logstash enable yes && \
        logstash_signal 0 && logstash_active="yes"
    echo "logstash_enable=\"$logstash_enable\""
    echo "logstash_usable=\"$logstash_usable\""
    echo "logstash_active=\"$logstash_active\""

%start -u @l_rusr@
    rcService logstash enable yes || exit 0
    rcService logstash active yes && exit 0
    (   nohup @l_prefix@/sbin/logstash agent \
           -f "$logstash_cfgfile" \
           $logstash_flags \
        </dev/null >>$logstash_logfile 2>&1 &
        echo $! >$logstash_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService logstash enable yes || exit 0
    rcService logstash active no  && exit 0
    logstash_signal TERM
    sleep 2
    rm -f $logstash_pidfile >/dev/null 2>&1 || true

%restart -u @l_rusr@
    rcService logstash enable yes || exit 0
    rcService logstash active no  && exit 0
    rc logstash stop start

%daily -u @l_rusr@
    rcService logstash enable yes || exit 0
    shtool rotate -f \
        -n ${logstash_log_numfiles} -s ${logstash_log_minsize} -d \
        -z ${logstash_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${logstash_log_prolog}" \
        -E "${logstash_log_epilog}; rc logstash restart" \
        $logstash_logfile

