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

%config
    prometheus_enable="$openpkg_rc_def"
    prometheus_listen="127.0.0.1:9090"
    prometheus_flags=""
    prometheus_log_prolog="true"
    prometheus_log_epilog="true"
    prometheus_log_numfiles="10"
    prometheus_log_minsize="1M"
    prometheus_log_complevel="9"

%common
    prometheus_cfgfile="@l_prefix@/etc/prometheus/prometheus.conf"
    prometheus_datadir="@l_prefix@/var/prometheus/data"
    prometheus_pidfile="@l_prefix@/var/prometheus/run/prometheus.pid"
    prometheus_logfile="@l_prefix@/var/prometheus/log/prometheus.log"
    prometheus_logdir="@l_prefix@/var/prometheus/log"
    prometheus_signal () {
        [ -f $prometheus_pidfile ] && kill -$1 `cat $prometheus_pidfile`
    }

%status -u @l_rusr@ -o
    prometheus_usable="unknown"
    prometheus_active="no"
    rcService prometheus enable yes && \
        prometheus_signal 0 && prometheus_active="yes"
    echo "prometheus_enable=\"$prometheus_enable\""
    echo "prometheus_usable=\"$prometheus_usable\""
    echo "prometheus_active=\"$prometheus_active\""

%start -u @l_rusr@
    rcService prometheus enable yes || exit 0
    rcService prometheus active yes && exit 0
    (   GOMAXPROCS=32
        export GOMAXPROCS
        nohup @l_prefix@/sbin/prometheus \
            --config.file="$prometheus_cfgfile" \
            --storage.tsdb.path="$prometheus_datadir" \
            --web.listen-address="$prometheus_listen" \
            $prometheus_flags \
        </dev/null >>$prometheus_logfile 2>&1 &
        echo $! >$prometheus_pidfile
    ) >/dev/null 2>&1

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

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

%daily -u @l_susr@
    rcService prometheus enable yes || exit 0
    shtool rotate -f \
        -n ${prometheus_log_numfiles} -s ${prometheus_log_minsize} -d \
        -z ${prometheus_log_complevel} -m 664 -o @l_rusr@ -g @l_rgrp@ \
        -P "${prometheus_log_prolog}" \
        -E "${prometheus_log_epilog}; rc prometheus restart" \
        $prometheus_logfile

