#!/bin/bash # Cleanup benchmark metrics from Prometheus # This allows running benchmarks from a clean state set -e echo "=== Prometheus Benchmark Metrics Cleanup ===" echo "" # Port-forward to Prometheus echo "Setting up port-forward to Prometheus..." kubectl port-forward -n monitoring svc/prometheus-kube-prometheus-prometheus 9090:9090 > /tmp/cleanup-pf.log 2>&1 & PF_PID=$! echo "Port-forward started (PID: $PF_PID)" sleep 5 # Check if Admin API is enabled echo "" echo "Checking if Prometheus Admin API is enabled..." ADMIN_CHECK=$(curl -s -o /dev/null -w "%{http_code}" -X POST "http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=epimetheus_benchmark_cpu_usage") if [ "$ADMIN_CHECK" = "204" ] || [ "$ADMIN_CHECK" = "200" ]; then echo "✅ Admin API is enabled" echo "" echo "Deleting benchmark metrics..." # Delete all benchmark metrics METRICS=( "epimetheus_benchmark_cpu_usage" "epimetheus_benchmark_memory_bytes" "epimetheus_benchmark_disk_io_bytes" "epimetheus_benchmark_network_rx_bytes" "epimetheus_benchmark_network_tx_bytes" "epimetheus_benchmark_requests_total" "epimetheus_benchmark_errors_total" "epimetheus_benchmark_response_time_ms" "epimetheus_benchmark_active_connections" "epimetheus_benchmark_queue_depth" ) for METRIC in "${METRICS[@]}"; do echo " Deleting: $METRIC" curl -s -X POST "http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=$METRIC" > /dev/null done echo "" echo "Triggering tombstone cleanup (this removes deleted data from disk)..." curl -s -X POST "http://localhost:9090/api/v1/admin/tsdb/clean_tombstones" > /dev/null echo "" echo "✅ Cleanup complete!" elif [ "$ADMIN_CHECK" = "405" ]; then echo "❌ Admin API is NOT enabled" echo "" echo "To enable the Admin API, update your Prometheus configuration:" echo "" echo "In f3s/prometheus/persistence-values.yaml, add:" echo "" echo "prometheus:" echo " prometheusSpec:" echo " additionalArgs:" echo " - name: web.enable-admin-api" echo " value: \"\"" echo "" echo "Then upgrade Prometheus:" echo " cd /home/paul/git/conf/f3s/prometheus" echo " just upgrade" echo "" echo "WARNING: Admin API should only be enabled in development/test environments!" echo "" echo "Alternative: Delete benchmark data files manually:" echo " kubectl exec -n monitoring prometheus-prometheus-kube-prometheus-prometheus-0 -- sh -c 'rm -rf /prometheus/data/wal/*'" echo " kubectl delete pod -n monitoring prometheus-prometheus-kube-prometheus-prometheus-0" else echo "⚠️ Unexpected response: HTTP $ADMIN_CHECK" fi echo "" echo "Cleaning up port-forward..." kill $PF_PID 2>/dev/null || true echo "" echo "Done!"