#!/bin/sh

# PROVIDE: ping_exporter
# REQUIRE: LOGIN
# KEYWORD: shutdown

# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# ping_exporter_enable (bool):                Set to NO by default.
#               Set it to YES to enable ping_exporter.
# ping_exporter_config (str):                 Set configuration file of ping_exporter
#               Default is "%%PREFIX%%/etc/ping_exporter.yml".
# ping_exporter_syslog_output_enable (bool):  Set to YES to enable syslog output
#               Default is "YES". See daemon(8).
# ping_exporter_syslog_output_tag (str):      Set syslog tag if syslog enabled.
#               Default is "ping_exporter". See daemon(8).
# ping_exporter_syslog_output_priority (str): Set syslog priority if syslog enabled.
#               Default is "notice". See daemon(8).
# ping_exporter_syslog_output_facility (str): Set to YES to enable syslog output
#               Default is "daemon". See daemon(8).
# ping_exporter_listen_address (string):      Set ip:port that ping_exporter will listen on
#               Default is "localhost:9427".
# ping_exporter_args (str):                   Set extra arguments to pass to ping_exporter
#               Default is "".

. /etc/rc.subr

name=ping_exporter
desc="Ping exporter for use with Prometheus"
rcvar=ping_exporter_enable

load_rc_config $name

: ${ping_exporter_enable:=NO}
: ${ping_exporter_config="%%PREFIX%%/etc/ping_exporter.yml"}
: ${ping_exporter_syslog_output_enable:="YES"}
: ${ping_exporter_listen_address=localhost:9427}

if checkyesno ping_exporter_syslog_output_enable; then
	if [ -n "${ping_exporter_syslog_output_tag}" ]; then
		ping_exporter_syslog_output_flags="-T ${ping_exporter_syslog_output_tag}"
	else
		ping_exporter_syslog_output_flags="-T ${name}"
	fi
	if [ -n "${ping_exporter_syslog_output_priority}" ]; then
		ping_exporter_syslog_output_flags="${ping_exporter_syslog_output_flags} -s ${ping_exporter_syslog_output_priority}"
	fi
	if [ -n "${ping_exporter_syslog_output_facility}" ]; then
		ping_exporter_syslog_output_flags="${ping_exporter_syslog_output_flags} -l ${ping_exporter_syslog_output_facility}"
	fi
fi

pidfile=/var/run/${name}.pid

required_files="${ping_exporter_config}"
command=/usr/sbin/daemon
command_args="-f -P ${pidfile} -t ${name} ${ping_exporter_syslog_output_flags} \
					/usr/bin/env ${ping_exporter_env} %%PREFIX%%/bin/ping_exporter \
					--config.path=${ping_exporter_config} \
					--web.listen-address=${ping_exporter_listen_address} \
					${ping_exporter_args}"

run_rc_command "$1"
