#!/bin/bash
#
# NOTICE: THIS FILE IS NOT INCLUDED IN THE DEFAULT GATHER SCRIPT
#
# Can be executed by: oc adm must-gather --image quay.io/oadp/must-gather:latest -- /usr/bin/gather_metrics_dump
#

# Prepare gather setup
unset KUBECONFIG
object_collection_path="/must-gather/metrics"
mkdir ${object_collection_path}

# Setup vars
time_now=$(date +%s)
time_day_ago=$(($time_now - 24*60*60))

# Prometheus - last day data files dump
echo "Running Prometheus data files last day dump ... (might take a while)"
oc exec -n openshift-monitoring prometheus-k8s-0 -- \
  bash -c "find /prometheus -newermt \"$(date '+%Y-%m-%d %H:%M' -d @${time_day_ago})\" | tar cvzf - --no-recursion --exclude \"queries.active\" --files-from -" \
  > "${object_collection_path}/prom_data.tar.gz"

# oc exec and tar could be unhappy with file changes during its compression, however if the dump archive was created, it is success
if [ -f "${object_collection_path}/prom_data.tar.gz" ]; then
  exit 0
else
  exit $?
fi
