#!/bin/sh
# fillrate-task: diagnose
# Read-only. Prints GPU, driver, kernel error, disk, memory, container, and Vast daemon state.
# This script does not change the machine.

echo "=== nvidia-smi ==="
if command -v nvidia-smi >/dev/null 2>&1; then
  nvidia-smi || echo "nvidia-smi failed"
  echo "=== driver / CUDA ==="
  nvidia-smi --query-gpu=driver_version,name,temperature.gpu --format=csv || true
else
  echo "nvidia-smi not found"
fi

echo "=== CUDA toolkit ==="
if [ -r /usr/local/cuda/version.json ]; then
  cat /usr/local/cuda/version.json
elif [ -r /usr/local/cuda/version.txt ]; then
  cat /usr/local/cuda/version.txt
else
  echo "no /usr/local/cuda version file"
fi
if command -v nvcc >/dev/null 2>&1; then
  nvcc --version || true
fi

echo "=== kernel Xid / ECC ==="
if command -v journalctl >/dev/null 2>&1; then
  journalctl -k -b --no-pager -n 500 2>/dev/null | grep -E 'Xid|ECC|NVRM|AER' || echo "no Xid/ECC lines in the kernel log"
else
  dmesg 2>/dev/null | grep -E 'Xid|ECC|NVRM|AER' || echo "no kernel log"
fi

echo "=== df ==="
df -h || true

echo "=== free ==="
free -h || true

echo "=== docker ps ==="
if command -v docker >/dev/null 2>&1; then
  docker ps --format 'table {{.ID}}\t{{.Image}}\t{{.Status}}\t{{.Names}}' || echo "docker ps failed"
else
  echo "docker not found"
fi

echo "=== vastai.service ==="
if command -v systemctl >/dev/null 2>&1; then
  systemctl status vastai.service --no-pager || true
  journalctl -u vastai.service -n 80 --no-pager || true
else
  echo "systemctl not found"
fi
