#!/usr/bin/env bash
set -euo pipefail

IMAGE="${NODE_EXPORTER_IMAGE:-quay.io/prometheus/node-exporter:v1.8.2}"
CONTAINER_NAME="${NODE_EXPORTER_CONTAINER_NAME:-node-exporter}"
TEXTFILE_DIR="${NODE_EXPORTER_TEXTFILE_DIR:-/home/ollama/node_exporter_textfiles}"
PORT="${NODE_EXPORTER_PORT:-9100}"

if [ ! -d "$TEXTFILE_DIR" ]; then
  echo "TEXTFILE_DIR_MISSING $TEXTFILE_DIR" >&2
  exit 1
fi

if docker ps -a --format '{{.Names}}' | grep -qx "$CONTAINER_NAME"; then
  docker rm -f "$CONTAINER_NAME" >/dev/null
fi

docker run -d \
  --name "$CONTAINER_NAME" \
  --restart unless-stopped \
  -p "${PORT}:9100" \
  -v /:/host:ro,rslave \
  -v "${TEXTFILE_DIR}:/textfile:ro" \
  "$IMAGE" \
  --path.rootfs=/host \
  --collector.textfile.directory=/textfile \
  '--collector.filesystem.mount-points-exclude=^/(sys|proc|dev|host|etc|run/credentials|var/lib/docker|run/docker)($|/)' \
  '--collector.filesystem.fs-types-exclude=^(autofs|binfmt_misc|bpf|cgroup2?|configfs|debugfs|devpts|devtmpfs|fusectl|hugetlbfs|iso9660|mqueue|nsfs|overlay|proc|procfs|pstore|rpc_pipefs|securityfs|selinuxfs|squashfs|sysfs|tracefs|tmpfs|shm)$' \
  --no-collector.infiniband \
  --no-collector.ipvs \
  --no-collector.mdadm \
  --no-collector.nfs \
  --no-collector.nfsd \
  --no-collector.xfs \
  --no-collector.zfs \
  --no-collector.processes \
  --no-collector.arp \
  --no-collector.netclass \
  --no-collector.netdev >/dev/null

for _ in $(seq 1 20); do
  if curl -fsS --max-time 3 "http://127.0.0.1:${PORT}/metrics" \
    | grep -q '^awoooi_backup_health_monitor_up{host="188"} 1$'; then
    echo "NODE_EXPORTER_188_OK port=${PORT} textfile_dir=${TEXTFILE_DIR}"
    exit 0
  fi
  sleep 1
done

echo "NODE_EXPORTER_188_BACKUP_HEALTH_METRIC_MISSING port=${PORT}" >&2
docker logs --tail=80 "$CONTAINER_NAME" >&2 || true
exit 1
