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

%config
    prometheus_statsd_enable="$openpkg_rc_def"
    prometheus_statsd_listen_statsd="127.0.0.1:9125"
    prometheus_statsd_listen_prometheus="127.0.0.1:8081"
    prometheus_statsd_log_prolog="true"
    prometheus_statsd_log_epilog="true"
    prometheus_statsd_log_numfiles="10"
    prometheus_statsd_log_minsize="1M"
    prometheus_statsd_log_complevel="9"

%common
    prometheus_statsd_cfgfile="@l_prefix@/etc/prometheus-statsd/prometheus-statsd.conf"
    prometheus_statsd_pidfile="@l_prefix@/var/prometheus-statsd/run/prometheus-statsd.pid"
    prometheus_statsd_logfile="@l_prefix@/var/prometheus-statsd/log/prometheus-statsd.log"
    prometheus_statsd_signal () {
        [ -f $prometheus_statsd_pidfile ] && kill -$1 `cat $prometheus_statsd_pidfile`
    }

%status -u @l_rusr@ -o
    prometheus_statsd_usable="unknown"
    prometheus_statsd_active="no"
    rcService prometheus-statsd enable yes && \
        prometheus_statsd_signal 0 && prometheus_statsd_active="yes"
    echo "prometheus_statsd_enable=\"$prometheus_statsd_enable\""
    echo "prometheus_statsd_usable=\"$prometheus_statsd_usable\""
    echo "prometheus_statsd_active=\"$prometheus_statsd_active\""

%start -u @l_rusr@
    rcService prometheus-statsd enable yes || exit 0
    rcService prometheus-statsd active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/prometheus-statsd \
            -statsd.listen-address="$prometheus_statsd_listen_statsd" \
            -statsd.mapping-config="$prometheus_statsd_cfgfile" \
            -web.listen-address="$prometheus_statsd_listen_prometheus" \
            -web.telemetry-path="/metrics" \
        </dev/null >>$prometheus_statsd_logfile 2>&1 &
        echo $! >$prometheus_statsd_pidfile
    ) >/dev/null 2>&1

%stop -u @l_rusr@
    rcService prometheus-statsd enable yes || exit 0
    rcService prometheus-statsd active no  && exit 0
    prometheus_statsd_signal TERM
    sleep 2
    rm -f $prometheus_statsd_pidfile >/dev/null 2>&1 || true

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

%daily -u @l_susr@
    rcService prometheus-statsd enable yes || exit 0
    shtool rotate -f \
        -n ${prometheus_statsd_log_numfiles} -s ${prometheus_statsd_log_minsize} -d \
        -z ${prometheus_statsd_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${prometheus_statsd_log_prolog}" \
        -E "${prometheus_statsd_log_epilog}; rc prometheus-statsd restart" \
        $prometheus_statsd_logfile

