A previous change (cf2d127) set SERVICE_HOST for constructing
exporter URLs, but SERVICE_HOST references to the controller
node IP. This breaks multinode deployments where exporters are
running on subnodes, as the health checks would try to reach the
controller instead of the local node.
This change properly checks SERVICE_IP_VERSION and uses HOST_IPV6
for IPv6 environments or HOST_IP for IPv4, ensuring the URL points
to the correct node where the exporter is actually running.
Additionally, IPv6 addresses are wrapped in square brackets as
required for URLs (e.g., http://[2001:db8::1]:9100/metrics).
A common get_host_ip_for_url function is introduced in lib/common
to centralize IP version handling and avoid code duplication across
all exporter modules.
Assisted-By: Claude (claude-sonnet-4-5)
Closes-Bug: #2141918
Change-Id: Icff76c7dc52d6444aa1c049dd6eada27016f4166
Signed-off-by: Douglas Viroel <viroel@gmail.com>
158 lines
4.5 KiB
Plaintext
158 lines
4.5 KiB
Plaintext
# lib/prometheus
|
|
# Functions to control the installation and configuration of prometheus
|
|
|
|
# Save trace setting
|
|
_XTRACE_PROMETHEUS=$(set +o | grep xtrace)
|
|
set +o xtrace
|
|
|
|
PROMETHEUS_SYSTEMD_SERVICE="devstack@prometheus.service"
|
|
PROMETHEUS_DATA_DIRECTORY="/var/lib/prometheus"
|
|
PROMETHEUS_PORT=${PROMETHEUS_PORT:-9090}
|
|
|
|
function pre_install_prometheus {
|
|
# Install OS packages
|
|
install_package wget tar jq
|
|
}
|
|
|
|
function install_prometheus {
|
|
|
|
local prom_tarball=prometheus-${PROMETHEUS_VERSION}.linux-amd64.tar.gz
|
|
local prom_url=https://github.com/prometheus/prometheus/releases/download/v${PROMETHEUS_VERSION}/${prom_tarball}
|
|
|
|
# Download Prometheus
|
|
local prom_dest
|
|
prom_dest=`get_extra_file ${prom_url}`
|
|
|
|
# Extract the tarball
|
|
tar xzf ${prom_dest} -C $DEST
|
|
|
|
# Move binaries to /usr/local/bin
|
|
sudo mv $DEST/prometheus-${PROMETHEUS_VERSION}.linux-amd64/prometheus /usr/local/bin/
|
|
sudo mv $DEST/prometheus-${PROMETHEUS_VERSION}.linux-amd64/promtool /usr/local/bin/
|
|
|
|
# Set ownership
|
|
sudo chown $(whoami):$(whoami) /usr/local/bin/prometheus
|
|
sudo chown $(whoami):$(whoami) /usr/local/bin/promtool
|
|
}
|
|
|
|
function configure_prometheus {
|
|
# Configure prometheus
|
|
|
|
sudo mkdir -p /etc/prometheus
|
|
sudo chown $(whoami):$(whoami) /etc/prometheus
|
|
|
|
if [[ ${PROMETHEUS_CUSTOM_SCRAPE_TARGETS} != "" ]]; then
|
|
|
|
# Generate custom config
|
|
generate_custom_prometheus_config
|
|
else
|
|
|
|
# Copy User's prometheus config
|
|
sudo cp ${PROMETHEUS_CONFIG_FILE} /etc/prometheus
|
|
fi
|
|
|
|
# Change the permission of prometheus config
|
|
sudo chmod 0644 /etc/prometheus/$(basename ${PROMETHEUS_CONFIG_FILE})
|
|
|
|
# Show User's prometheus config
|
|
sudo cat /etc/prometheus/$(basename ${PROMETHEUS_CONFIG_FILE})
|
|
|
|
# Create data directory
|
|
sudo mkdir -p ${PROMETHEUS_DATA_DIRECTORY}
|
|
sudo chown $(whoami):$(whoami) ${PROMETHEUS_DATA_DIRECTORY}
|
|
}
|
|
|
|
function generate_custom_prometheus_config {
|
|
# Generate custome prometheus config
|
|
|
|
# local base config
|
|
local BASE_CONFIG_FILE=${PROMETHEUS_DEVSTACK_DIR}/custom_prometheus.yml
|
|
|
|
# Temprory place to hold config file
|
|
local RESULT_CONFIG_FILE=${PROMETHEUS_DEVSTACK_DIR}/tmp_prometheus.yml
|
|
|
|
cat ${BASE_CONFIG_FILE} > ${RESULT_CONFIG_FILE}
|
|
|
|
# Generate custom config
|
|
if [[ ${PROMETHEUS_CUSTOM_SCRAPE_TARGETS} != "" ]]; then
|
|
echo " - job_name: 'custom'" >> ${RESULT_CONFIG_FILE}
|
|
echo " static_configs:" >> ${RESULT_CONFIG_FILE}
|
|
TARGETS=$(echo ${PROMETHEUS_CUSTOM_SCRAPE_TARGETS} | tr "," "\n")
|
|
for TARGET in ${TARGETS[@]}
|
|
do
|
|
echo " - targets: [$TARGET]" >> $RESULT_CONFIG_FILE
|
|
done
|
|
fi
|
|
|
|
# Copy custom generated
|
|
sudo cp ${RESULT_CONFIG_FILE} /etc/prometheus/prometheus.yml
|
|
|
|
# Drop tmp_prometheus.yml
|
|
rm ${PROMETHEUS_DEVSTACK_DIR}/tmp_prometheus.yml
|
|
|
|
}
|
|
|
|
function init_prometheus {
|
|
prometheus_command="/usr/local/bin/prometheus"
|
|
prometheus_command+=" --config.file=/etc/prometheus/prometheus.yml"
|
|
prometheus_command+=" --storage.tsdb.path=${PROMETHEUS_DATA_DIRECTORY}"
|
|
prometheus_command+=" --web.enable-admin-api"
|
|
prometheus_command+=" --web.enable-remote-write-receiver"
|
|
|
|
write_user_unit_file $PROMETHEUS_SYSTEMD_SERVICE "$prometheus_command" "" "$STACK_USER"
|
|
|
|
enable_service $PROMETHEUS_SYSTEMD_SERVICE
|
|
}
|
|
|
|
function start_prometheus {
|
|
start_service $PROMETHEUS_SYSTEMD_SERVICE
|
|
}
|
|
|
|
function stop_prometheus {
|
|
stop_service $PROMETHEUS_SYSTEMD_SERVICE
|
|
}
|
|
|
|
function cleanup_prometheus {
|
|
|
|
stop_prometheus
|
|
|
|
disable_service $PROMETHEUS_SYSTEMD_SERVICE
|
|
|
|
# Remove systemd unit files
|
|
local unitfile="$SYSTEMD_DIR/$PROMETHEUS_SYSTEMD_SERVICE"
|
|
sudo rm -f $unitfile
|
|
$SYSTEMCTL daemon-reload
|
|
|
|
# Remove Prometheus directories
|
|
sudo rm -rf /etc/prometheus
|
|
sudo rm -rf ${PROMETHEUS_DATA_DIRECTORY}
|
|
|
|
# Remove binaries
|
|
sudo rm /usr/local/bin/prometheus
|
|
sudo rm /usr/local/bin/promtool
|
|
|
|
# Remove untar location
|
|
sudo rm -rf $DEST/prometheus-${PROMETHEUS_VERSION}.linux-amd64
|
|
}
|
|
|
|
function wait_for_data {
|
|
# Adding sleep time so that data is scrapped by prometheus
|
|
sleep 60
|
|
}
|
|
|
|
function check_data_prometheus {
|
|
local host_ip
|
|
host_ip=$(get_host_ip_for_url)
|
|
local url="http://$host_ip:$PROMETHEUS_PORT/api/v1/label/__name__/values"
|
|
|
|
if curl -s --head --request GET "$url" | grep "200 OK" > /dev/null; then
|
|
echo "#### List of metrics names ####"
|
|
curl -s "$url" | jq -r '.data[]'
|
|
else
|
|
die $LINENO "Couldn't get data from prometheus"
|
|
fi
|
|
}
|
|
|
|
# Restore xtrace
|
|
$_XTRACE_PROMETHEUS
|