#!/bin/bash
# nvidia-gpu-monitor.sh — writes GPU stats to files every 5 seconds
# Install: sudo cp to /usr/local/bin/, chmod +x
# Service: systemd unit at /etc/systemd/system/nvidia-gpu-monitor.service
#
# Output files:
#   /usr/share/cockpit/nvidia-gpu/gpu.txt       — model,driver,temp,util,mem,power,pstate,fan
#   /usr/share/cockpit/nvidia-gpu/cuda.txt      — CUDA version
#   /usr/share/cockpit/nvidia-gpu/processes.txt — pid,process_name,used_memory

PKG="/usr/share/cockpit/nvidia-gpu"

while true; do
  # GPU info (name, driver, temp, util, mem used, mem total, power, perf state, fan)
  nvidia-smi --query-gpu=name,driver_version,temperature.gpu,utilization.gpu,memory.used,memory.total,power.draw,pstate,fan.speed --format=csv,noheader,nounits 2>/dev/null | head -1 > "$PKG/gpu.txt"

  # GPU processes
  nvidia-smi --query-compute-apps=pid,process_name,used_memory --format=csv,noheader,nounits 2>/dev/null > "$PKG/processes.txt"

  # CUDA version
  nvidia-smi 2>/dev/null | grep "CUDA Version" | sed 's/.*CUDA Version: //' | head -1 > "$PKG/cuda.txt"

  sleep 5
done
