nvidia_gpu_exporter

repository·main·Indexed 23 days ago

https://github.com/utkuozdemir/nvidia_gpu_exporter

An NVIDIA GPU exporter for Prometheus that gathers metrics using the nvidia-smi binary for compatibility with Windows, Linux, and MacOS, and offers an experimental native NVML backend for Linux. It includes a Helm chart for Kubernetes deployment, support for per-process GPU metrics, MIG-enabled GPU configuration, and integration with Prometheus Operator and Grafana dashboards.

Tokens
23.1K
Snippets
42
Records
74
Agent score
78%

What's inside nvidia_gpu_exporter

  1. Overview of nvidia_gpu_exporter

    main

    The nvidia_gpu_exporter is a Prometheus exporter designed to collect, parse, and export NVIDIA GPU metrics. It primarily uses the nvidia-smi binary to gather data, making it highly portable across Windows, Linux, and MacOS without requiring C bindings.

    Key features include:

    • Multi-platform support: Works anywhere nvidia-smi(.exe) is available.
    • Remote execution: Can be configured to run nvidia-smi on a remote machine.
    • NVML Backend: An experimental Linux-only backend that reads directly from the driver for deeper metrics (MIG, XID, PCIe throughput).
    • Flexible collection: Supports optional per-process metrics and background collection on a timer.
  2. Simulate GPU states using machine configuration files

    main

    The behavior of the simulated machines is driven by YAML files in machines/*.yaml. Both the exec and nvml flavors re-read these files on every scrape cycle, meaning you can change the simulated hardware state without restarting the containers.

    Key Configuration Concepts

    • fluctuate: true: Enables jitter for metrics like utilization, temperature, power, clocks, fan, and memory.
    • gpus:: Replicates the capture into multiple cards with stable identities and per-GPU overrides.
    • overrides:: Pins specific fields to static values to bypass jitter. Field names must match nvidia-smi query fields.

    Example: Pinning field values

    To preview a specific state in a dashboard panel, use the overrides block:

    overrides:
      gpu_recovery_action: "Reset"   # drives the health tile
      temperature.gpu: 95            # drives the temperature threshold color
    overrides:
      gpu_recovery_action: "Reset"
      temperature.gpu: 95
  3. How GPU capture files are structured

    main

    A capture is a single .txt file named using the pattern: <os>-<arch>__<model>__<driver>.txt.

    Each file contains:

    1. A minimal metadata header (collection time, masking status, OS/arch, and load state).
    2. Multiple sections separated by headers, containing the raw output of various nvidia-smi commands (e.g., --version, -h, -L, topo -m, nvlink -s, and the --query-gpu CSV output).

    Privacy/Masking: By default, the collector masks sensitive identifiers to prevent machine fingerprinting:

    • GPU UUID $\rightarrow$ GPU-00000000-0000-0000-0000-000000000000
    • Serial $\rightarrow$ 0000000000000
    • Hostname $\rightarrow$ redacted-host

    Non-sensitive data like PCI bus ID, VBIOS version, model name, and process names are preserved. To disable masking, use the --no-mask flag (not recommended for public submissions).

  4. Monitor MIG (Multi-Instance GPU) instances

    main

    On GPUs with MIG mode enabled, the NVML backend automatically exports per-MIG-instance metrics. No additional configuration is required for inventory and memory, but activity metrics require Hopper generation or later GPUs.

    MIG Metrics:

    • nvidia_smi_mig_info{uuid, mig_uuid, gpu_instance_id, compute_instance_id, profile}: Identity of each MIG device (constant 1).
    • nvidia_smi_mig_memory_{total,used,free,reserved}_bytes{uuid, gpu_instance_id}: Memory metrics for the GPU instance.
    • nvidia_smi_mig_{graphics_activity,sm_activity,sm_occupancy,tensor_activity}_ratio{uuid, gpu_instance_id}: Activity ratios (Hopper+ only).
    • nvidia_smi_mig_pcie_throughput_{tx,rx}_bytes_per_second{uuid, gpu_instance_id}: PCIe throughput (Hopper+ only).

    Attribution: Activity and throughput are reported per gpu_instance_id. To map these back to specific MIG devices, join with nvidia_smi_mig_info using (uuid, gpu_instance_id).

    Process Attribution: To include gpu_instance_id and compute_instance_id labels in per-process metrics, use the --collect.compute-apps-mig flag (requires --collect.compute-apps and the NVML backend).

  5. Dashboard compatibility constraints

    main

    The dashboards follow strict compatibility rules to maintain support for a large user base:

    • Metric Contract: Metric names and labels are considered a frozen contract. Renaming series will silently break existing dashboards.
    • Template Variables: The instance-selector template variable is a fixed requirement. It cannot be renamed to instance, as bookmarked URLs rely on its specific name.
  6. Understand the core GPU metrics and identity

    main

    The exporter provides several core metrics for monitoring GPU health and utilization. Most per-GPU metrics are labeled with a uuid, which allows you to join them with the nvidia_smi_gpu_info metric. This identity metric contains static metadata such as name, driver_version, cuda_version, pci_bus_id, and serial.

    Key core metrics include:

    • nvidia_smi_utilization_gpu_ratio: GPU utilization as a percentage.
    • nvidia_smi_memory_used_bytes: Memory currently in use (in bytes).
    • nvidia_smi_power_draw_watts: Current power draw in Watts.
    • nvidia_smi_temperature_gpu: GPU temperature.
    # HELP nvidia_smi_gpu_info A metric with a constant '1' value labeled by gpu uuid, name, driver_model_current, driver_model_pending, vbios_version, driver_version, pci_bus_id, serial, compute_cap, pci_sub_device_id, index, cuda_version.
    # TYPE nvidia_smi_gpu_info gauge
    nvidia_smi_gpu_info{uuid="df6e7a7c-7314-46f8-abc4-b88b36dcf3aa",name="NVIDIA GeForce RTX 2080 SUPER",driver_version="471.11",cuda_version="11.4",pci_bus_id="00000000:71:00.0",index="0",...} 1
  7. Use the experimental native NVML backend

    main

    The --collect.backend=nvml backend reads metrics directly from the driver library (libnvidia-ml.so.1) instead of executing nvidia-smi. This is an experimental feature available in specific -nvml release artifacts.

    Advantages:

    • Efficiency: Spawns no processes; collection is cheaper than nvidia-smi.
    • No binary required: Only requires the NVIDIA driver library (provided by NVIDIA container runtime).
    • Exclusive Metrics: Provides access to driver-only families like energy_joules_total, PCIe throughput (via --collect.pcie-throughput), MIG-specific metrics, and XID error counters.

    Limitations and Constraints:

    • Platform: Linux x86_64, glibc-based systems only.
    • Artifacts: Requires using -nvml flavored images/binaries.
    • Isolation: Unlike the exec backend, a wedged driver call cannot be easily killed by the exporter.
    • Incompatibility: Cannot be used with --nvidia-smi-command or remote scraping via SSH.
    • Status Reporting: Failures are reported via nvidia_smi_nvml_return_code (0 for success, -1 for abandoned/failed).
    nvidia_gpu_exporter --collect.backend nvml
  8. Configure Prometheus scraping (ServiceMonitor vs PodMonitor)

    main

    The chart supports two ways to integrate with the Prometheus Operator. You must enable either serviceMonitor or podMonitor, but not both, otherwise targets will be scraped twice.

    • ServiceMonitor: Creates a ServiceMonitor resource (default behavior if enabled).
    • PodMonitor: Creates a PodMonitor resource. This requires the Prometheus Operator CRDs to be present in your cluster.
  9. Understand enum-valued metrics mapping

    main

    The exporter converts non-numeric nvidia-smi states into integers for Prometheus compatibility.

    Two-state fields (mapped to 1/0): Covers fields like persistence_mode, ecc.mode.current, mig.mode.current, and various throttle flags (e.g., gpu_idle, hw_slowdown).

    Multi-state fields:

    • nvidia_smi_pstate: Performance state (0 = P0/Max performance, 15 = P15/Min performance).
    • nvidia_smi_gpu_recovery_action: 0 (None), 1 (GPU Reset), 2 (Node Reboot), 3 (Drain P2P), 4 (Drain and Reset).
    • nvidia_smi_fabric_state: 0 (Not Supported), 1 (Not Started), 2 (In Progress), 3 (Completed).
    • nvidia_smi_compute_mode: 0 (Default), 1 (Exclusive Thread), 2 (Prohibited), 3 (Exclusive Process).
  10. Follow query and panel conventions for custom dashboards

    main

    When creating or modifying Grafana panels for nvidia_gpu_exporter, follow these technical conventions to avoid silent failures or incorrect data rendering:

    • Legend Enrichment Joins: Use parentheses around the expression to ensure correct operator precedence when joining with nvidia_smi_gpu_info. Example: (expr) * on(uuid) group_left(index, name) nvidia_smi_gpu_info{...}
    • Table Targets: Always include an absence sentinel using an OR arm like ... nvidia_smi_gpu_info * 0 - 1. This ensures that if a series is absent, the join doesn't drop entire columns. The sentinel must be aggregated to match the labels of the left side.
    • MIG (Multi-Instance GPU) Joins: To avoid hard-errors when joining on GPUs with heterogeneous compute instances, strip the compute-instance prefix using label_replace before aggregating.
    • NVML-only Families: Use an unless-guarded uuid-only arm to prevent panels from going blank if a collection fails.
    • State Timelines: Use fixed/text color mode without a thresholds key; use value mappings to define band colors.
    • Multi-GPU Timeseries: Use the palette-classic palette, as dynamically-named series do not support custom palettes.
  11. Run nvidia_gpu_exporter in Docker

    main

    To run the exporter in Docker, you must have the NVIDIA driver and the NVIDIA Container Toolkit installed on the host. The container image does not bundle NVIDIA components; instead, the toolkit injects the GPU devices, driver libraries, and the nvidia-smi binary at runtime.

    Use the --gpus all flag to enable NVIDIA integration and set NVIDIA_DRIVER_CAPABILITIES=utility to indicate the container only requires the nvidia-smi/NVML tier.

    Note: Without --gpus, a device reservation, or runtime: nvidia, the container will run on the default runtime without GPU access. In this state, the exporter will only serve health metrics, and nvidia_smi_last_collect_success will be 0.

    docker run -d \
      --name nvidia_gpu_exporter \
      --restart unless-stopped \
      --gpus all \
      -e NVIDIA_DRIVER_CAPABILITIES=utility \
      -p 9835:9835 \
      utkuozdemir/nvidia_gpu_exporter:latest
  12. Verify and test alert rules

    main

    The development stack includes a mechanism to test Prometheus alert rules. The render-rules.sh script renders the Helm chart's PrometheusRule into prometheus/rules/ with all rules force-enabled and for: durations shortened to 30s for rapid evaluation.

    Testing specific alerts

    • To test rule changes: Re-run ./hack/compose/render-rules.sh and then restart Prometheus: docker compose restart prometheus prometheus-demo.
    • To simulate a failing exporter: Use the broken profile: docker compose --profile broken up -d exec-broken.
    • To simulate a stale collection: Edit a machine file (e.g., machines/consumer.yaml) and add exit: 15 at the top level.
    • To simulate a slow collection: Add delay: 2s to the machine configuration.
    • To simulate thermal/power throttling: Flip clocks_event_reasons.sw_thermal_slowdown or .hw_power_brake_slowdown from 0 to 1 in the machine YAML.