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

%config
    telegraf_enable="$openpkg_rc_def"
    telegraf_flags=""
    telegraf_log_prolog="true"
    telegraf_log_epilog="true"
    telegraf_log_numfiles="10"
    telegraf_log_minsize="1M"
    telegraf_log_complevel="9"

%common
    telegraf_cfgfile="@l_prefix@/etc/telegraf/telegraf.conf"
    telegraf_pidfile="@l_prefix@/var/telegraf/run/telegraf.pid"
    telegraf_logfile="@l_prefix@/var/telegraf/log/telegraf.log"
    telegraf_signal () {
        [ -f $telegraf_pidfile ] && kill -$1 `cat $telegraf_pidfile`
    }

%status -u @l_rusr@ -o
    telegraf_usable="unknown"
    telegraf_active="no"
    rcService telegraf enable yes && \
        telegraf_signal 0 && telegraf_active="yes"
    echo "telegraf_enable=\"$telegraf_enable\""
    echo "telegraf_usable=\"$telegraf_usable\""
    echo "telegraf_active=\"$telegraf_active\""

%start -u @l_rusr@
    rcService telegraf enable yes || exit 0
    rcService telegraf active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/telegraf \
           -config "$telegraf_cfgfile" \
           $telegraf_flags \
        </dev/null >>$telegraf_logfile 2>&1 &
        echo $! >$telegraf_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_rusr@
    rcService telegraf enable yes || exit 0
    shtool rotate -f \
        -n ${telegraf_log_numfiles} -s ${telegraf_log_minsize} -d \
        -z ${telegraf_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${telegraf_log_prolog}" \
        -E "${telegraf_log_epilog}; rc telegraf restart" \
        $telegraf_logfile

