diff --git a/.gitea/workflows/deploy.yaml b/.gitea/workflows/deploy.yaml index 1f87725..1c7ffa0 100644 --- a/.gitea/workflows/deploy.yaml +++ b/.gitea/workflows/deploy.yaml @@ -898,6 +898,8 @@ jobs: gitea_ci@"$HOST":/etc/prometheus/scrape_configs.d/quantus.yml push --mkpath --chmod=F0644 asset/grafana/quantus.json \ gitea_ci@"$HOST":/etc/grafana/provisioning/dashboards/quantus/quantus.json + push --mkpath --chmod=F0644 asset/grafana/gpu.json \ + gitea_ci@"$HOST":/etc/grafana/provisioning/dashboards/quantus/gpu.json # The PROVIDER tells Grafana the dashboards directory exists at all; # without it the JSON above is just a file nobody reads. Tracked diff --git a/asset/gpu/nvidia-textfile.sh b/asset/gpu/nvidia-textfile.sh new file mode 100755 index 0000000..ddc234f --- /dev/null +++ b/asset/gpu/nvidia-textfile.sh @@ -0,0 +1,86 @@ +#!/usr/bin/env bash +# Write NVIDIA GPU metrics for node-exporter's textfile collector. +# +# Deliberately NOT a new exporter: node-exporter already runs on every host, is +# already scraped by the fleet Prometheus (job "node", targets from cichlid's +# inventory), and already has the textfile collector enabled. A textfile drop +# therefore needs no new port, no firewalld service, no scrape config and no +# entry in the port registry. +# +# DCGM was the alternative and is the wrong tool here: it targets datacenter +# cards and its GeForce support is partial. nvidia-smi reports everything we +# actually want. +set -euo pipefail + +OUT_DIR="${OUT_DIR:-/var/lib/prometheus/node-exporter}" +OUT="${OUT_DIR}/nvidia.prom" +TMP="$(mktemp "${OUT}.XXXXXX")" +# The collector reads whole files, so a half-written one is a parse error and +# shows up as node_textfile_scrape_error. Build in a temp file, rename atomically. +trap 'rm -f "$TMP"' EXIT + +QUERY=index,uuid,name,temperature.gpu,utilization.gpu,utilization.memory,\ +memory.used,memory.total,power.draw,power.limit,clocks.current.sm,clocks.current.memory,fan.speed + +{ + echo "# HELP nvidia_gpu_info Static GPU identity, always 1." + echo "# TYPE nvidia_gpu_info gauge" + echo "# HELP nvidia_gpu_temperature_celsius Core temperature." + echo "# TYPE nvidia_gpu_temperature_celsius gauge" + echo "# HELP nvidia_gpu_utilization_ratio Fraction of time the GPU was busy, 0-1." + echo "# TYPE nvidia_gpu_utilization_ratio gauge" + echo "# HELP nvidia_gpu_memory_utilization_ratio Fraction of time memory was accessed, 0-1." + echo "# TYPE nvidia_gpu_memory_utilization_ratio gauge" + echo "# HELP nvidia_gpu_memory_used_bytes Memory in use." + echo "# TYPE nvidia_gpu_memory_used_bytes gauge" + echo "# HELP nvidia_gpu_memory_total_bytes Memory installed." + echo "# TYPE nvidia_gpu_memory_total_bytes gauge" + echo "# HELP nvidia_gpu_power_watts Current board power draw." + echo "# TYPE nvidia_gpu_power_watts gauge" + echo "# HELP nvidia_gpu_power_limit_watts Enforced power limit." + echo "# TYPE nvidia_gpu_power_limit_watts gauge" + echo "# HELP nvidia_gpu_clock_sm_hertz Current SM clock." + echo "# TYPE nvidia_gpu_clock_sm_hertz gauge" + echo "# HELP nvidia_gpu_clock_memory_hertz Current memory clock." + echo "# TYPE nvidia_gpu_clock_memory_hertz gauge" + echo "# HELP nvidia_gpu_fan_speed_ratio Fan duty cycle, 0-1." + echo "# TYPE nvidia_gpu_fan_speed_ratio gauge" + echo "# HELP nvidia_gpu_scrape_ok 1 when nvidia-smi was queried successfully." + echo "# TYPE nvidia_gpu_scrape_ok gauge" + + if ! smi=$(nvidia-smi --query-gpu="$QUERY" --format=csv,noheader,nounits 2>/dev/null); then + # Driver reloading, GPU fell off the bus, or nvidia-smi absent. Emit an + # explicit zero rather than a stale file or an empty one, so the dashboard + # shows "broken" instead of "flat". + echo "nvidia_gpu_scrape_ok 0" + exit 0 + fi + echo "nvidia_gpu_scrape_ok 1" + + while IFS= read -r line; do + [ -n "$line" ] || continue + IFS=',' read -r idx uuid name temp util umem mused mtotal pdraw plimit csm cmem fan \ + <<< "$(printf '%s' "$line" | sed 's/, */,/g')" + # A label value may contain characters Prometheus treats specially. + esc() { printf '%s' "$1" | sed 's/\\/\\\\/g; s/"/\\"/g'; } + L="gpu=\"${idx}\",uuid=\"$(esc "$uuid")\",name=\"$(esc "$name")\"" + + num() { case "$1" in ''|*[!0-9.]*) return 1 ;; *) printf '%s' "$1" ;; esac; } + + echo "nvidia_gpu_info{${L}} 1" + num "$temp" >/dev/null && echo "nvidia_gpu_temperature_celsius{${L}} ${temp}" + num "$util" >/dev/null && awk -v l="$L" -v v="$util" 'BEGIN{printf "nvidia_gpu_utilization_ratio{%s} %.4f\n", l, v/100}' + num "$umem" >/dev/null && awk -v l="$L" -v v="$umem" 'BEGIN{printf "nvidia_gpu_memory_utilization_ratio{%s} %.4f\n", l, v/100}' + num "$mused" >/dev/null && awk -v l="$L" -v v="$mused" 'BEGIN{printf "nvidia_gpu_memory_used_bytes{%s} %.0f\n", l, v*1048576}' + num "$mtotal" >/dev/null && awk -v l="$L" -v v="$mtotal" 'BEGIN{printf "nvidia_gpu_memory_total_bytes{%s} %.0f\n", l, v*1048576}' + num "$pdraw" >/dev/null && echo "nvidia_gpu_power_watts{${L}} ${pdraw}" + num "$plimit" >/dev/null && echo "nvidia_gpu_power_limit_watts{${L}} ${plimit}" + num "$csm" >/dev/null && awk -v l="$L" -v v="$csm" 'BEGIN{printf "nvidia_gpu_clock_sm_hertz{%s} %.0f\n", l, v*1000000}' + num "$cmem" >/dev/null && awk -v l="$L" -v v="$cmem" 'BEGIN{printf "nvidia_gpu_clock_memory_hertz{%s} %.0f\n", l, v*1000000}' + num "$fan" >/dev/null && awk -v l="$L" -v v="$fan" 'BEGIN{printf "nvidia_gpu_fan_speed_ratio{%s} %.4f\n", l, v/100}' + done <<< "$smi" +} > "$TMP" + +chmod 0644 "$TMP" +mv -f "$TMP" "$OUT" +trap - EXIT diff --git a/asset/grafana/gpu.json b/asset/grafana/gpu.json new file mode 100644 index 0000000..5ccb15c --- /dev/null +++ b/asset/grafana/gpu.json @@ -0,0 +1,496 @@ +{ + "uid": "gpu-fleet", + "title": "GPU fleet", + "tags": [ + "gpu", + "nvidia", + "fleet" + ], + "description": "NVIDIA GPU temperature, power and utilisation across the fleet. Fed by node-exporter's textfile collector (asset/gpu/nvidia-textfile.sh in lair/quantus) \u2014 no separate exporter. Fleet-general rather than quantus-specific; move it if a monitoring repo appears.", + "timezone": "browser", + "schemaVersion": 39, + "version": 1, + "refresh": "30s", + "time": { + "from": "now-6h", + "to": "now" + }, + "editable": false, + "graphTooltip": 1, + "panels": [ + { + "type": "row", + "title": "Temperature", + "collapsed": false, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 0 + }, + "panels": [] + }, + { + "type": "timeseries", + "title": "GPU temperature", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "h": 9, + "w": 24, + "x": 0, + "y": 1 + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "editorMode": "code", + "expr": "nvidia_gpu_temperature_celsius", + "legendFormat": "{{host}} gpu{{gpu}} \u2014 {{name}}", + "range": true, + "refId": "A" + } + ], + "fieldConfig": { + "defaults": { + "custom": { + "lineWidth": 2, + "fillOpacity": 6, + "showPoints": "never", + "thresholdsStyle": { + "mode": "dashed" + } + }, + "color": { + "mode": "palette-classic" + }, + "unit": "celsius", + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "green", + "value": null + }, + { + "color": "orange", + "value": 80 + }, + { + "color": "red", + "value": 87 + } + ] + } + }, + "overrides": [] + }, + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "description": "Thresholds mark where consumer NVIDIA cards begin thermal throttling (~83-87C depending on model). Sustained mining sits far higher than idle \u2014 the 4090 was measured at 70C under load against ~40C idle \u2014 so a rising baseline over days usually means dust or ambient, not a driver problem." + }, + { + "type": "row", + "title": "Power and utilisation", + "collapsed": false, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 10 + }, + "panels": [] + }, + { + "type": "timeseries", + "title": "Power draw vs limit", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "h": 9, + "w": 12, + "x": 0, + "y": 11 + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "editorMode": "code", + "expr": "nvidia_gpu_power_watts", + "legendFormat": "{{host}} gpu{{gpu}} \u2014 {{name}}", + "range": true, + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "editorMode": "code", + "expr": "nvidia_gpu_power_limit_watts", + "legendFormat": "{{host}} gpu{{gpu}} \u2014 {{name}} (limit)", + "range": true, + "refId": "B" + } + ], + "fieldConfig": { + "defaults": { + "custom": { + "lineWidth": 2, + "fillOpacity": 6, + "showPoints": "never" + }, + "color": { + "mode": "palette-classic" + }, + "unit": "watt" + }, + "overrides": [] + }, + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "description": "Draw pinned at the limit means the card is power-capped, which is the normal state while mining. Measured efficiency: a 5090 does 0.60 MH/s per watt at 600W and 0.70 at its 400W floor, so capping lower is 18% more hash per watt." + }, + { + "type": "timeseries", + "title": "GPU utilisation", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "h": 9, + "w": 12, + "x": 12, + "y": 11 + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "editorMode": "code", + "expr": "nvidia_gpu_utilization_ratio", + "legendFormat": "{{host}} gpu{{gpu}} \u2014 {{name}}", + "range": true, + "refId": "A" + } + ], + "fieldConfig": { + "defaults": { + "custom": { + "lineWidth": 2, + "fillOpacity": 6, + "showPoints": "never" + }, + "color": { + "mode": "palette-classic" + }, + "unit": "percentunit", + "max": 1, + "min": 0 + }, + "overrides": [] + }, + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "description": "Fraction of time the GPU was busy. Mining pins this near 1.0; inference is spiky." + }, + { + "type": "row", + "title": "Memory, clocks, fans", + "collapsed": false, + "gridPos": { + "h": 1, + "w": 24, + "x": 0, + "y": 20 + }, + "panels": [] + }, + { + "type": "timeseries", + "title": "Memory used", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "h": 8, + "w": 8, + "x": 0, + "y": 21 + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "editorMode": "code", + "expr": "nvidia_gpu_memory_used_bytes", + "legendFormat": "{{host}} gpu{{gpu}} \u2014 {{name}}", + "range": true, + "refId": "A" + }, + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "editorMode": "code", + "expr": "nvidia_gpu_memory_total_bytes", + "legendFormat": "{{host}} gpu{{gpu}} \u2014 {{name}} (total)", + "range": true, + "refId": "B" + } + ], + "fieldConfig": { + "defaults": { + "custom": { + "lineWidth": 2, + "fillOpacity": 6, + "showPoints": "never" + }, + "color": { + "mode": "palette-classic" + }, + "unit": "bytes" + }, + "overrides": [] + }, + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "description": "Mining uses almost no VRAM \u2014 Poseidon2 over Goldilocks is register-resident. A large resident figure on a mining host is an inference model still loaded." + }, + { + "type": "timeseries", + "title": "SM clock", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "h": 8, + "w": 8, + "x": 8, + "y": 21 + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "editorMode": "code", + "expr": "nvidia_gpu_clock_sm_hertz", + "legendFormat": "{{host}} gpu{{gpu}} \u2014 {{name}}", + "range": true, + "refId": "A" + } + ], + "fieldConfig": { + "defaults": { + "custom": { + "lineWidth": 2, + "fillOpacity": 6, + "showPoints": "never" + }, + "color": { + "mode": "palette-classic" + }, + "unit": "hertz" + }, + "overrides": [] + }, + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "description": "Drops under a power or thermal cap; a good cross-check against the temperature panel." + }, + { + "type": "timeseries", + "title": "Fan speed", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "h": 8, + "w": 8, + "x": 16, + "y": 21 + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "editorMode": "code", + "expr": "nvidia_gpu_fan_speed_ratio", + "legendFormat": "{{host}} gpu{{gpu}} \u2014 {{name}}", + "range": true, + "refId": "A" + } + ], + "fieldConfig": { + "defaults": { + "custom": { + "lineWidth": 2, + "fillOpacity": 6, + "showPoints": "never" + }, + "color": { + "mode": "palette-classic" + }, + "unit": "percentunit", + "max": 1, + "min": 0 + }, + "overrides": [] + }, + "options": { + "legend": { + "displayMode": "list", + "placement": "bottom", + "showLegend": true + }, + "tooltip": { + "mode": "multi", + "sort": "desc" + } + }, + "description": "0 on cards with a passive idle mode. Fans at maximum with temperature still climbing is the failure worth alerting on." + }, + { + "type": "stat", + "title": "Metric freshness", + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "gridPos": { + "h": 4, + "w": 24, + "x": 0, + "y": 29 + }, + "targets": [ + { + "datasource": { + "type": "prometheus", + "uid": "prometheus" + }, + "editorMode": "code", + "expr": "nvidia_gpu_scrape_ok", + "legendFormat": "{{host}}", + "range": true, + "refId": "A" + } + ], + "description": "0 means nvidia-smi failed on that host \u2014 driver reloading, or the GPU fell off the bus. The script emits an explicit zero rather than leaving a stale file, so this reads 'broken' instead of the graphs quietly flatlining.", + "fieldConfig": { + "defaults": { + "color": { + "mode": "thresholds" + }, + "mappings": [ + { + "type": "value", + "options": { + "0": { + "text": "nvidia-smi FAILED", + "color": "red", + "index": 0 + }, + "1": { + "text": "ok", + "color": "green", + "index": 1 + } + } + } + ], + "thresholds": { + "mode": "absolute", + "steps": [ + { + "color": "text", + "value": null + } + ] + } + }, + "overrides": [] + }, + "options": { + "colorMode": "background", + "graphMode": "none", + "textMode": "auto", + "reduceOptions": { + "calcs": [ + "lastNotNull" + ], + "fields": "", + "values": true + } + } + } + ] +} diff --git a/asset/systemd/nvidia-textfile.service b/asset/systemd/nvidia-textfile.service new file mode 100644 index 0000000..4cb1d57 --- /dev/null +++ b/asset/systemd/nvidia-textfile.service @@ -0,0 +1,18 @@ +[Unit] +Description=Write NVIDIA GPU metrics for the node-exporter textfile collector +Documentation=https://git.lair.cafe/lair/quantus +ConditionPathExists=/usr/bin/nvidia-smi + +[Service] +Type=oneshot +ExecStart=/usr/local/bin/nvidia-textfile.sh +# Runs as root only to write into node-exporter's spool; it reads nothing +# sensitive and holds no credentials. +NoNewPrivileges=true +ProtectHome=true +ProtectKernelTunables=true +ProtectKernelModules=false +ProtectControlGroups=true +RestrictSUIDSGID=true +LockPersonality=true +ReadWritePaths=/var/lib/prometheus/node-exporter diff --git a/asset/systemd/nvidia-textfile.timer b/asset/systemd/nvidia-textfile.timer new file mode 100644 index 0000000..21fa1e3 --- /dev/null +++ b/asset/systemd/nvidia-textfile.timer @@ -0,0 +1,12 @@ +[Unit] +Description=Refresh NVIDIA GPU metrics for node-exporter + +[Timer] +OnBootSec=30s +# Matched to Prometheus' 15s scrape interval: slower would show stale values, +# faster would just burn nvidia-smi invocations nobody reads. +OnUnitActiveSec=15s +AccuracySec=1s + +[Install] +WantedBy=timers.target diff --git a/script/infra-setup.sh b/script/infra-setup.sh index b33de85..bba9d93 100755 --- a/script/infra-setup.sh +++ b/script/infra-setup.sh @@ -11,6 +11,10 @@ # metrics (--metrics-hosts) the fleet Prometheus/Grafana host: a scrape # config drop-in and a provisioned dashboard # -> deploy.yaml deploy-metrics +# gpu (--gpu-hosts) a timer writing nvidia-smi metrics into +# node-exporter's textfile collector. No new exporter, +# port, firewalld service or scrape config — the fleet +# node-exporter already carries it. # edge (--edge-hosts) the site reverse proxy: an internal TLS cert and an # nginx vhost giving `wss://quantus.internal`. NOT # CI-deployed — the runner has no rights on the edge @@ -48,6 +52,10 @@ MINER_HOSTS="${MINER_HOSTS-benjy.hanzalova.internal quadbrat.hanzalova.internal} METRICS_HOSTS="${METRICS_HOSTS-golgafrinchans.kosherinata.internal}" # The site reverse proxy that fronts quantus.internal. Empty to skip the edge role. EDGE_HOSTS="${EDGE_HOSTS-hanzalova.internal}" +# Hosts with NVIDIA GPUs to expose metrics for. Fleet-general rather than +# quantus-specific — it lives here because there is no monitoring repo yet, and +# should move to one if that changes. +GPU_HOSTS="${GPU_HOSTS-beast.hanzalova.internal benjy.hanzalova.internal quadbrat.hanzalova.internal}" # Vanity name served by the edge proxy. Dot-free short name: the step@ renewal # unit is a template instance and %i cannot carry dots (internal-tls.md §1). EDGE_NAME="${EDGE_NAME:-quantus}" @@ -65,6 +73,7 @@ while [[ $# -gt 0 ]]; do --miner-hosts) MINER_HOSTS="$2"; shift 2 ;; --metrics-hosts) METRICS_HOSTS="$2"; shift 2 ;; --edge-hosts) EDGE_HOSTS="$2"; shift 2 ;; + --gpu-hosts) GPU_HOSTS="$2"; shift 2 ;; --admin) ADMIN_USER="$2"; shift 2 ;; --node-version) NODE_VERSION="$2"; shift 2 ;; *) echo "unknown arg: $1" >&2; exit 2 ;; @@ -336,6 +345,7 @@ PROM cat > "$tmp" <<'SUDO' gitea_ci ALL=(root) NOPASSWD: /usr/bin/rsync * /etc/prometheus/scrape_configs.d/quantus.yml gitea_ci ALL=(root) NOPASSWD: /usr/bin/rsync * /etc/grafana/provisioning/dashboards/quantus/quantus.json +gitea_ci ALL=(root) NOPASSWD: /usr/bin/rsync * /etc/grafana/provisioning/dashboards/quantus/gpu.json gitea_ci ALL=(root) NOPASSWD: /usr/bin/rsync * /etc/grafana/provisioning/dashboards/quantus-dashboards.yaml gitea_ci ALL=(root) NOPASSWD: /usr/bin/systemctl restart grafana.service SUDO @@ -347,6 +357,48 @@ REMOTE } + +# GPU metrics via node-exporter's textfile collector. +# +# Deliberately not a new exporter. node-exporter already runs on every host, is +# already scraped (job "node", targets from cichlid's inventory, labelled by +# `host`), and already has the textfile collector enabled with a spool at +# /var/lib/prometheus/node-exporter. So this needs no port allocation, no +# firewalld service and no scrape config — which is also why it is one-time +# provisioning rather than a deploy job. +# +# DCGM was the alternative and is the wrong tool: it targets datacenter cards +# and its GeForce support is partial. nvidia-smi reports everything wanted here. +provision_gpu() { + local host="$1" + echo "== ${host} (gpu metrics) ==" + if ! ssh -o ConnectTimeout=8 -o BatchMode=yes "${ADMIN_USER}@${host}" true; then + echo " ! unreachable as ${ADMIN_USER} — skipping" >&2 + return 1 + fi + if ! ssh "${ADMIN_USER}@${host}" "command -v nvidia-smi >/dev/null"; then + echo " ! no nvidia-smi on this host — skipping" >&2 + return 1 + fi + + rsync -az --rsync-path='sudo rsync' --chmod=F0755 \ + asset/gpu/nvidia-textfile.sh "${ADMIN_USER}@${host}:/usr/local/bin/nvidia-textfile.sh" + rsync -az --rsync-path='sudo rsync' --chmod=F0644 \ + asset/systemd/nvidia-textfile.service asset/systemd/nvidia-textfile.timer \ + "${ADMIN_USER}@${host}:/etc/systemd/system/" + + ssh "${ADMIN_USER}@${host}" " + sudo install -d -m 0755 /var/lib/prometheus/node-exporter + sudo systemctl daemon-reload + sudo systemctl enable --now nvidia-textfile.timer + sudo systemctl start nvidia-textfile.service" + + local n + n=$(ssh "${ADMIN_USER}@${host}" \ + "curl -fsS http://127.0.0.1:9100/metrics | grep -c '^nvidia_gpu_temperature_celsius' || true") + echo " = timer armed; ${n:-0} GPU(s) reporting through node-exporter" +} + # The site reverse proxy. Gives tooling a `wss://quantus.internal` endpoint, # which matters because subxt — and therefore quantus-cli — refuses plain `ws://` # to anything but localhost, so without this every client needs an ssh tunnel. @@ -473,6 +525,9 @@ done for h in $METRICS_HOSTS; do provision_metrics "$h" || rc=1 done +for h in $GPU_HOSTS; do + provision_gpu "$h" || rc=1 +done for h in $EDGE_HOSTS; do provision_edge "$h" "$EDGE_NAME" || rc=1 done