Skip to content

Instantly share code, notes, and snippets.

@cody0704
Last active December 7, 2022 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cody0704/dddcca120b160b0681479e7b121afcaa to your computer and use it in GitHub Desktop.
Save cody0704/dddcca120b160b0681479e7b121afcaa to your computer and use it in GitHub Desktop.
Grafana + Prometheus + Node_Exporter
#!/bin/sh
# PROVIDE: node_exporter
# REQUIRE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
# node_exporter_enable (bool): Set to NO by default.
# Set it to YES to enable node_exporter.
# node_exporter_user (string): Set user that node_exporter will run under
# Default is "nobody".
# node_exporter_group (string): Set group that node_exporter will run under
# Default is "nobody".
# node_exporter_args (string): Set extra arguments to pass to node_exporter
# Default is "".
# node_exporter_listen_address (string):Set ip:port that node_exporter will listen on
# Default is ":9100".
# node_exporter_textfile_dir (string): Set directory that node_exporter will watch
# Default is "/var/tmp/node_exporter".
. /etc/rc.subr
name=node_exporter
rcvar=node_exporter_enable
load_rc_config $name
: ${node_exporter_enable:="NO"}
: ${node_exporter_user:="nobody"}
: ${node_exporter_group:="nobody"}
: ${node_exporter_args:=""}
: ${node_exporter_listen_address:=":9100"}
: ${node_exporter_textfile_dir:="/var/tmp/node_exporter"}
pidfile=/var/run/node_exporter.pid
command="/usr/sbin/daemon"
procname="/usr/local/bin/node_exporter"
command_args="-p ${pidfile} -T ${name} \
/usr/bin/env ${procname} \
--web.listen-address=${node_exporter_listen_address} \
--collector.textfile.directory=${node_exporter_textfile_dir} \
${node_exporter_args}"
start_precmd=node_exporter_startprecmd
node_exporter_startprecmd()
{
if [ ! -e ${pidfile} ]; then
install \
-o ${node_exporter_user} \
-g ${node_exporter_group} \
/dev/null ${pidfile};
fi
if [ ! -d ${node_exporter_textfile_dir} ]; then
install \
-d \
-o ${node_exporter_user} \
-g ${node_exporter_group} \
-m 1755 \
${node_exporter_textfile_dir}
fi
}
load_rc_config $name
run_rc_command "$1"
#!/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 to NO by default
# Set it to YES to enable prometheus
# prometheus_user (string)
# Set user to run prometheus
# Default is "prometheus"
# prometheus_group (string)
# Set group to run prometheus
# Default is "prometheus"
# prometheus_data_dir (string)
# Set dir to run prometheus in
# Default is "/var/db/prometheus"
# prometheus_consoles (string)
# Set dir that contains Prometheus consoles
# Default is "/usr/local/share/prometheus/consoles"
# prometheus_console_libraries (string)
# Set dir containing Prometheus console libraries
# Default is "/usr/local/share/prometheus/console_libraries"
# 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"}
: ${prometheus_group:="prometheus"}
: ${prometheus_config:="/usr/local/etc/prometheus.yml"}
: ${prometheus_data_dir:="/var/db/prometheus"}
: ${prometheus_consoles_dir:="/usr/local/share/prometheus/consoles"}
: ${prometheus_console_libraries_dir:="/usr/local/share/prometheus/console_libraries"}
: ${prometheus_args:=""}
pidfile="/var/run/${name}.pid"
required_files="${prometheus_config}"
command="/usr/sbin/daemon"
procname="/usr/local/bin/${name}"
sig_reload="HUP"
extra_commands="reload"
command_args="-p ${pidfile} -m 3 -T ${name} \
/usr/bin/env ${procname} \
--config.file=${prometheus_config} \
--storage.tsdb.path=${prometheus_data_dir} \
--web.console.templates=${prometheus_consoles_dir} \
--web.console.libraries=${prometheus_console_libraries_dir} \
${prometheus_args}"
start_precmd=prometheus_startprecmd
# This checks for the existence of a prometheus 1.x data dir at the
# $prometheus_data_dir location. If one is found, Prometheus will not start.
prometheus_check_data_dir_version()
{
local data_dir_version_file="${prometheus_data_dir}/VERSION"
if [ -f "${data_dir_version_file}" ]; then
local data_dir_version="0"
read data_dir_version < "${data_dir_version_file}"
if [ "${data_dir_version}" = "1" ]; then
return 1
fi
fi
}
prometheus_startprecmd()
{
if [ ! -e ${pidfile} ]; then
install \
-o ${prometheus_user} \
-g ${prometheus_group} \
/dev/null ${pidfile};
fi
if [ ! -d ${prometheus_data_dir} ]; then
install \
-d \
-o ${prometheus_user} \
-g ${prometheus_group} \
-m 750 \
${prometheus_data_dir}
else
# The directory already existed. Ensure it's not a prometheus 1.x
# data dir.
if ! prometheus_check_data_dir_version; then
err 1 "Found net-mgmt/prometheus1 data dir, refusing to start."
fi
fi
}
load_rc_config $name
run_rc_command "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment