Chandan Kumar (raukadah) 489d13fc17 Use get_extra_file for downloading node_exporter/prometheus tarballs
DevStack provides get_extra_file[1] to download node_exporter/prometheus tarballs
and cache them in ci for further use.

Let's reuse the same instead of wget the binary.
This CR also adds cleanup phase under unstack step.

Links:
[1]. https://github.com/openstack/devstack/blob/master/functions#L50-L79

Closes-Bug: #2102666

Change-Id: I858eea3584794fdc10e45c9f80bfacdabdd537e2
Signed-off-by: Chandan Kumar (raukadah) <chkumar@redhat.com>
2025-03-25 11:18:30 +05:30

118 lines
3.4 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 /etc/prometheus
sudo chown $(whoami):$(whoami) /etc/prometheus
# Copy User's prometheus config
sudo cp ${PROMETHEUS_CONFIG_FILE} /etc/prometheus
# 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 ${PROMETHEUS_DATA_DIRECTORY}
sudo chown $(whoami):$(whoami) ${PROMETHEUS_DATA_DIRECTORY}
}
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