Skip to content

Instantly share code, notes, and snippets.

@beanieboi
Last active December 26, 2021 09:02
Show Gist options
  • Save beanieboi/f0471b3471325ce8c8c1b0356c8b920d to your computer and use it in GitHub Desktop.
Save beanieboi/f0471b3471325ce8c8c1b0356c8b920d to your computer and use it in GitHub Desktop.
Cortex Metrics FreeBSD rc.d script
#!/bin/sh
# PROVIDE: cortex
# REQUIRE: DAEMON
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf to enable cortex:
#
# cortex_enable (bool): Set to "NO" by default.
# Set it to "YES" to enable cortex
# cortex_config (path): Set to "/usr/local/etc/cortex" by default
# Set to directory to store cortex config
# cortex_logfile (str): Set to "/var/log/cortex.log" by default.
# Set it to file where stdout/stderr are logged.
# cortex_syslog_enable (bool): Set to YES by default
# Set it to NO to disable syslog output
# cortex_syslog_tag (str): Set to "cortex" by default.
# Set syslog tag if syslog enabled
# cortex_syslog_priority (str): Set to "info" by default.
# Set syslog priority if syslog enabled
# cortex_syslog_facility (str): Set to "daemon" by default.
# Set syslog facility if syslog enabled
# cortex_user (str): Set to "cortex" by default.
# Set it to user to run cortex under
# cortex_group (str): Set to "cortex" by default.
# Set it to group to run cortex under
#
. /etc/rc.subr
name=cortex
rcvar=cortex_enable
load_rc_config ${name}
: ${cortex_enable:="NO"}
: ${cortex_syslog_enable:="YES"}
: ${cortex_config:="/usr/local/etc/cortex/config.yml"}
: ${cortex_logfile:="/var/log/cortex.log"}
: ${cortex_user:="cortex"}
: ${cortex_group:="cortex"}
start_precmd="cortex_start_precmd"
if checkyesno cortex_syslog_enable; then
if [ -n "${cortex_syslog_tag}" ]; then
cortex_syslog_output_flags="-T ${cortex_syslog_tag}"
else
cortex_syslog_output_flags="-T ${name}"
fi
if [ -n "${cortex_syslog_priority}" ]; then
cortex_syslog_output_flags="${cortex_syslog_output_flags} -s ${cortex_syslog_priority}"
fi
if [ -n "${cortex_syslog_facility}" ]; then
cortex_syslog_output_flags="${cortex_syslog_output_flags} -l ${cortex_syslog_facility}"
fi
else
cortex_syslog_output_flags="-o ${cortex_logfile}"
fi
# legacy compatibility
if [ -n "${cortex_config}" ]; then
cortex_config_flag="-config.file=\"${cortex_config}\""
fi
pidfile="/var/run/${name}.pid"
procname="/usr/local/bin/cortex"
command="/usr/sbin/daemon"
command_args="-f -t ${name} ${cortex_syslog_output_flags} -p ${pidfile} ${procname} ${cortex_config_flag}"
cortex_start_precmd()
{
if [ ! -e "${pidfile}" ]; then
install -m 0600 -o "${cortex_user}" -g "${cortex_group}" /dev/null "${pidfile}"
fi
if [ -n "${cortex_config}" -a ! -d "${cortex_config}" ]; then
install -d -m 0750 -o "${cortex_user}" -g "${cortex_group}" "${cortex_config}"
fi
if ! checkyesno cortex_syslog_enable && [ ! -e "${cortex_logfile}" ]; then
install -m 0640 -o "${cortex_user}" -g "${cortex_group}" /dev/null "${cortex_logfile}"
fi
}
run_rc_command "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment