#!/bin/bash
# Collecte les métriques GPU AMD via sysfs (pas de rocm-smi requis)
OUTPUT="{{ node_exporter_textfile_dir }}/rocm.prom"
TMPFILE="${OUTPUT}.tmp"

{
  echo "# HELP rocm_gpu_temperature_celsius GPU temperature in Celsius"
  echo "# TYPE rocm_gpu_temperature_celsius gauge"
  echo "# HELP rocm_gpu_utilization_percent GPU utilization percentage"
  echo "# TYPE rocm_gpu_utilization_percent gauge"
  echo "# HELP rocm_vram_used_bytes VRAM used in bytes"
  echo "# TYPE rocm_vram_used_bytes gauge"
  echo "# HELP rocm_vram_total_bytes VRAM total in bytes"
  echo "# TYPE rocm_vram_total_bytes gauge"
  echo "# HELP rocm_gpu_power_watts GPU power draw in watts"
  echo "# TYPE rocm_gpu_power_watts gauge"
  echo "# HELP rocm_gpu_power_cap_watts GPU power cap in watts"
  echo "# TYPE rocm_gpu_power_cap_watts gauge"

  for card in /sys/class/drm/card[0-9]; do
    [ -d "$card/device" ] || continue
    idx=$(basename "$card" | tr -d 'card')
    labels="gpu=\"${idx}\",model=\"gfx1031\""

    temp_raw=$(cat "$card/device/hwmon/hwmon"*/temp1_input 2>/dev/null | head -1)
    [ -n "$temp_raw" ] && echo "rocm_gpu_temperature_celsius{${labels}} $(echo "scale=1; ${temp_raw}/1000" | bc)"

    util=$(cat "$card/device/gpu_busy_percent" 2>/dev/null)
    [ -n "$util" ] && echo "rocm_gpu_utilization_percent{${labels}} ${util}"

    vram_used=$(cat "$card/device/mem_info_vram_used" 2>/dev/null)
    [ -n "$vram_used" ] && echo "rocm_vram_used_bytes{${labels}} ${vram_used}"

    vram_total=$(cat "$card/device/mem_info_vram_total" 2>/dev/null)
    [ -n "$vram_total" ] && echo "rocm_vram_total_bytes{${labels}} ${vram_total}"

    power_raw=$(cat "$card/device/hwmon/hwmon"*/power1_average 2>/dev/null | head -1)
    [ -n "$power_raw" ] && echo "rocm_gpu_power_watts{${labels}} $(echo "scale=2; ${power_raw}/1000000" | bc)"

    power_cap_raw=$(cat "$card/device/hwmon/hwmon"*/power1_cap 2>/dev/null | head -1)
    [ -n "$power_cap_raw" ] && echo "rocm_gpu_power_cap_watts{${labels}} $(echo "scale=2; ${power_cap_raw}/1000000" | bc)"
  done
} > "$TMPFILE" && mv "$TMPFILE" "$OUTPUT"
