Files
devstack-plugin-prometheus/devstack/lib/prometheus
Chandan Kumar (raukadah) 0de1434ed3 Add support for PROMETHEUS_CUSTOM_SCRAPE_TARGETS
sg-core devstack plugin used to provide passing custom scrape
targets to prometheus config.

This cr brings back that functionality to d-p-p.

Change-Id: I05c6ad0d0cd84e9e6a2b0d3be21cce48fbd11478
Signed-off-by: Chandan Kumar (raukadah) <chkumar@redhat.com>
2025-05-21 10:33:16 +05:30

156 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 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