#!/bin/bash
set -euo pipefail

echo "[INFO] Starting Prometheus Targets Updater under supervisor..."

# 配置变量
PROMETHEUS_BASE_PATH=${PROMETHEUS_BASE_PATH:-/private/argus/metric/prometheus}
NODES_CONFIG_FILE=${NODES_CONFIG_FILE:-${PROMETHEUS_BASE_PATH}/nodes.json}
TARGETS_DIR=${PROMETHEUS_BASE_PATH}/targets
EXPORTER_CONFIG_FILE=${EXPORTER_CONFIG_FILE:-${PROMETHEUS_BASE_PATH}/exporter_config.json}
CHECK_INTERVAL=${CHECK_INTERVAL:-30}
LOG_LEVEL=${LOG_LEVEL:-INFO}

echo "[INFO] Prometheus base path: ${PROMETHEUS_BASE_PATH}"
echo "[INFO] Nodes config file: ${NODES_CONFIG_FILE}"
echo "[INFO] Targets directory: ${TARGETS_DIR}"
echo "[INFO] Exporter config file: ${EXPORTER_CONFIG_FILE}"
echo "[INFO] Check interval: ${CHECK_INTERVAL}s"
echo "[INFO] Log level: ${LOG_LEVEL}"

mkdir -p "${TARGETS_DIR}"

exec python3 /usr/local/bin/update_targets.py \
    --config "${NODES_CONFIG_FILE}" \
    --targets-dir "${TARGETS_DIR}" \
    --exporter-config "${EXPORTER_CONFIG_FILE}" \
    --log-level "${LOG_LEVEL}" \
    --daemon \
    --check-interval "${CHECK_INTERVAL}"
