#!/bin/sh

# PROVIDE: prometheus
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# prometheus_enable (bool)
#     Set it to YES to enable prometheus
#     Set to NO by default
# prometheus_user (string)
#     Set user that prometheus will run under
#     Default is "%%PROMETHEUS_USER%%"
# prometheus_group (string)
#     Set group that own prometheus files
#     Default is "%%PROMETHEUS_GROUP%%"
# prometheus_config (string)
#     Set full path to config file
#     Default is "%%PREFIX%%/etc/prometheus.yml"
# prometheus_pidfile (string)
#     Set full path to pid file
#     Default is "/var/run/prometheus.pid"
# prometheus_syslog_output_enable (bool)
#     Set it to NO to disable syslog output
#     Set to YES by default
# prometheus_syslog_output_tag (str)
#     Set syslog tag if syslog enabled
#     Default is "prometheus"
# prometheus_syslog_output_priority (string)
#     Set syslog priority if syslog enabled
#     Default is "info"
# prometheus_syslog_output_facility (string)
#     Set syslog facility if syslog enabled
#     Default is "daemon"
# prometheus_agent_mode (bool)
#     Set to "YES" to enable Prometheus Agent Mode
#     Default is "NO"
# prometheus_consoles (string)
#     Set dir that contains Prometheus consoles
#     Default is "%%PROMETHEUS_CONSOLES_DIR%%"
# prometheus_console_libraries (string)
#     Set dir containing Prometheus console libraries
#     Default is "%%PROMETHEUS_CONSOLE_LIBRARIES_DIR%%"
# prometheus_data_dir (string)
#     Set dir to run prometheus in
#     Default is "%%PROMETHEUS_DB_DIR%%"
# prometheus_loglevel (string)
#     Set one of [debug, info, warn, error]
#     Default is "info"
# prometheus_logformat (string)
#     Set one of [logfmt, json]
#     Default is "logfmt"
# prometheus_env (string)
#     Set environment variables used with prometheus
#     Default is ""
# prometheus_args (string)
#     Set additional command line arguments
#     Default is ""

. /etc/rc.subr

name=prometheus
rcvar=prometheus_enable

load_rc_config $name

: ${prometheus_enable:="NO"}
: ${prometheus_user:="%%PROMETHEUS_USER%%"}
: ${prometheus_group:="%%PROMETHEUS_GROUP%%"}
: ${prometheus_config:="%%PREFIX%%/etc/prometheus.yml"}
: ${prometheus_pidfile:="/var/run/prometheus.pid"}
: ${prometheus_syslog_output_enable:="YES"}
: ${prometheus_agent_mode:="NO"}
: ${prometheus_consoles_dir:="%%PROMETHEUS_CONSOLES_DIR%%"}
: ${prometheus_console_libraries_dir:="%%PROMETHEUS_CONSOLE_LIBRARIES_DIR%%"}
: ${prometheus_data_dir:="%%PROMETHEUS_DB_DIR%%"}
: ${prometheus_loglevel:="info"}
: ${prometheus_logformat:="logfmt"}

if checkyesno prometheus_syslog_output_enable; then
	if [ -n "${prometheus_syslog_output_tag}" ]; then
		prometheus_syslog_output_flags="-T ${prometheus_syslog_output_tag}"
	else
		prometheus_syslog_output_flags="-T ${name}"
	fi
	if [ -n "${prometheus_syslog_output_priority}" ]; then
		prometheus_syslog_output_flags="${prometheus_syslog_output_flags} -s ${prometheus_syslog_output_priority}"
	fi
	if [ -n "${prometheus_syslog_output_facility}" ]; then
		prometheus_syslog_output_flags="${prometheus_syslog_output_flags} -l ${prometheus_syslog_output_facility}"
	fi
fi

if checkyesno prometheus_agent_mode; then
    prometheus_storage_flags="--agent --storage.agent.path=${prometheus_data_dir}"
else
    prometheus_storage_flags="--storage.tsdb.path=${prometheus_data_dir}"
fi

pidfile="${prometheus_pidfile}"
required_files="${prometheus_config}"

procname="%%PREFIX%%/bin/prometheus"
command="/usr/sbin/daemon"
command_args="-f ${prometheus_syslog_output_flags} -p ${pidfile} -t ${name} \
	/usr/bin/env ${prometheus_env} ${procname} \
	--config.file=${prometheus_config} \
	--web.console.templates=${prometheus_consoles_dir} \
	--web.console.libraries=${prometheus_console_libraries_dir} \
	--log.level=${prometheus_loglevel} \
	--log.format=${prometheus_logformat} \
	${prometheus_storage_flags} \
	${prometheus_args}"

start_precmd="prometheus_start_precmd"
extra_commands="reload"

# This checks for the existence of a prometheus 1.x data at the
# $prometheus_data_dir location. If one is found, Prometheus will not start.
prometheus_check_data_version()
{
	local _version
	local _version_file="${prometheus_data_dir}/VERSION"

	if [ -f "${_version_file}" ]; then
		read _version < "${_version_file}"

		if [ "${_version}" = "1" ]; then
			return 1
		fi
	fi
}

prometheus_start_precmd()
{
	if [ ! -e "${pidfile}" ]; then
		install -m 0600 -o "${prometheus_user}" -g "${prometheus_group}" /dev/null "${pidfile}"
	fi
	if [ ! -d "${prometheus_data_dir}" ]; then
		install -d -m 750 -o "${prometheus_user}" -g "${prometheus_group}" "${prometheus_data_dir}"
	else
		# Ensure it's not a prometheus 1.x data
		if [ ! prometheus_check_data_version ]; then
			err 1 "Found \"net-mgmt/prometheus1\" data, refusing to start."
		fi
	fi
}

run_rc_command "$1"
