|
|
1 year ago | |
|---|---|---|
| cmd | 1 year ago | |
| pkg | 1 year ago | |
| .gitignore | 1 year ago | |
| Dockerfile | 1 year ago | |
| LICENSE | 1 year ago | |
| Makefile | 1 year ago | |
| README.md | 1 year ago | |
| go.mod | 1 year ago | |
| go.sum | 1 year ago | |
| ha-gpu-ccd | 1 year ago | |
| main.go | 1 year ago | |
| nvrm-gpu-ha.conf.example | 1 year ago |
A lightweight Go application that monitors NVIDIA GPU metrics using NVML (NVIDIA Management Library) and publishes them to Home Assistant via MQTT with automatic discovery support.
For each GPU, the following sensors are created in Home Assistant:
GPUs appear in Home Assistant with the format: {HOSTNAME} {PCI ID} - NVIDIA {MODEL} {VRAM}
Example: MY-SERVER 00:01:00.0 - NVIDIA GeForce RTX 3080 10GB
# Download the latest release
wget https://github.com/pccr10001/nvml-gpu-ha/releases/latest/download/nvml-gpu-ha-linux-amd64
chmod +x nvml-gpu-ha-linux-amd64
sudo mv nvml-gpu-ha-linux-amd64 /usr/local/bin/nvml-gpu-ha
# Clone the repository
git clone https://github.com/pccr10001/nvml-gpu-ha.git
cd nvml-gpu-ha
# Build and install
make build-linux
sudo cp nvml-gpu-ha-linux-amd64 /usr/local/bin/nvml-gpu-ha
# Pull the image
docker pull ghcr.io/pccr10001/nvml-gpu-ha:latest
# Or build locally
docker build -t nvml-gpu-ha .
The application reads configuration from /etc/nvml-gpu-ha.conf in TOML format. Command line flags can override config file settings.
# /etc/nvml-gpu-ha.conf
# Hostname prefix for GPU names (optional, uses system hostname if not specified)
hostname = "MY-SERVER"
# MQTT Broker Configuration
mqtt_host = "192.168.1.100"
mqtt_port = 1883
mqtt_username = "homeassistant"
mqtt_password = "your_secure_password"
# MQTT Options
mqtt_lwt_enable = true
mqtt_retain = true
# Monitoring Settings
polling_period = 30
# Copy example config
sudo cp nvml-gpu-ha.conf.example /etc/nvml-gpu-ha.conf
# Edit configuration
sudo nano /etc/nvml-gpu-ha.conf
# Set appropriate permissions
sudo chmod 600 /etc/nvml-gpu-ha.conf
sudo chown root:root /etc/nvml-gpu-ha.conf
Command line flags override configuration file settings:
nvml-gpu-ha [flags]
Flags:
--config string Configuration file path (default "/etc/nvml-gpu-ha.conf")
--hostname string Hostname prefix for GPU names (default: system hostname)
--mqtt-host string MQTT broker host (default "localhost")
--mqtt-port int MQTT broker port (default 1883)
--mqtt-username string MQTT username
--mqtt-password string MQTT password
--mqtt-lwt-enable Enable MQTT Last Will and Testament (default true)
--mqtt-retain Retain MQTT messages (default true)
--polling-period int GPU polling period in seconds (default 30)
-h, --help help for nvml-gpu-ha
# Using configuration file (recommended)
nvml-gpu-ha
# Using custom hostname
nvml-gpu-ha --hostname=MY-SERVER
# Using custom config file location
nvml-gpu-ha --config=/path/to/custom.conf
# Override config file with command line flags
nvml-gpu-ha --mqtt-host=192.168.1.200 --polling-period=10
# Pure command line usage (no config file)
nvml-gpu-ha \
--hostname=GAMING-PC \
--mqtt-host=192.168.1.100 \
--mqtt-port=1883 \
--mqtt-username=homeassistant \
--mqtt-password=secretpassword \
--polling-period=10
# Minimal override
nvml-gpu-ha --mqtt-host=mqtt.local --hostname=SERVER-01
Configuration is loaded in the following order (later sources override earlier ones):
/etc/nvml-gpu-ha.conf or specified via --config)This allows you to set base configuration in a file and override specific values via command line as needed.
# Generate service file
make systemd-service
# Install the service
sudo cp nvml-gpu-ha.service /etc/systemd/system/
sudo systemctl daemon-reload
# Enable and start the service
sudo systemctl enable nvml-gpu-ha
sudo systemctl start nvml-gpu-ha
# Check status
sudo systemctl status nvml-gpu-ha
Create /etc/systemd/system/nvml-gpu-ha.service:
[Unit]
Description=NVIDIA GPU Monitoring for Home Assistant
After=network.target
Wants=network.target
[Service]
Type=simple
User=nobody
Group=nogroup
ExecStart=/usr/local/bin/nvml-gpu-ha
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
Note: With configuration file support, the service file is much cleaner. All settings are read from /etc/nvml-gpu-ha.conf automatically.
version: '3.8'
services:
nvml-gpu-ha:
image: ghcr.io/pccr10001/nvml-gpu-ha:latest
container_name: nvml-gpu-ha
restart: unless-stopped
runtime: nvidia
environment:
- NVIDIA_VISIBLE_DEVICES=all
command: [
"--mqtt-host=192.168.1.100",
"--mqtt-username=homeassistant",
"--mqtt-password=secretpassword",
"--polling-period=30"
]
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: all
capabilities: [gpu]
docker run -d \
--name nvml-gpu-ha \
--restart unless-stopped \
--runtime nvidia \
-e NVIDIA_VISIBLE_DEVICES=all \
ghcr.io/pccr10001/nvml-gpu-ha:latest \
--mqtt-host=192.168.1.100 \
--mqtt-username=homeassistant \
--mqtt-password=secretpassword
Ensure MQTT integration is configured in Home Assistant:
# configuration.yaml
mqtt:
broker: localhost # Your MQTT broker
username: homeassistant
password: secretpassword
discovery: true # Enable MQTT Discovery
Once running, sensors will automatically appear in Home Assistant under:
{HOSTNAME} {PCI ID} - NVIDIA {MODEL} {VRAM}sensor.{pci_id}_nvidia_{model}_{vram}_power_drawsensor.{pci_id}_nvidia_{model}_{vram}_performance_levelsensor.{pci_id}_nvidia_{model}_{vram}_vram_usagesensor.{pci_id}_nvidia_{model}_{vram}_gpu_utilizationsensor.{pci_id}_nvidia_{model}_{vram}_temperature# Install dependencies
make deps
# Build for current platform
make build
# Build for all platforms
make build-linux
make build-linux-arm64
make build-windows
# Run tests
make test
This version includes several performance improvements:
The application displays NVML and driver version information at startup for debugging:
2024/08/18 11:00:00 Hostname: MY-SERVER
2024/08/18 11:00:00 NVML Version: 12.535.133.00
2024/08/18 11:00:00 NVIDIA Driver Version: 535.133.00
2024/08/18 11:00:00 Found 2 NVIDIA GPU(s)
2024/08/18 11:00:00 GPU 0: GeForce RTX 3080 (00:01:00.0, 10.0GB)
2024/08/18 11:00:00 GPU 1: GeForce RTX 4090 (00:02:00.0, 24.0GB)
"Failed to initialize NVML"
/usr/lib/x86_64-linux-gnu/libnvidia-ml.so.1 existsnvidia-smi first to verify GPU access"No NVIDIA GPUs found"
nvidia-smi -LMQTT connection issues
Sensors not appearing in Home Assistant
Enable verbose logging by checking the application logs:
# Systemd logs
sudo journalctl -u nvml-gpu-ha -f
# Docker logs
docker logs -f nvml-gpu-ha
Test MQTT connectivity:
# Subscribe to all topics
mosquitto_sub -h YOUR_MQTT_HOST -u YOUR_USERNAME -P YOUR_PASSWORD -t "homeassistant/sensor/nvml-gpu/+"
# Check discovery topics
mosquitto_sub -h YOUR_MQTT_HOST -u YOUR_USERNAME -P YOUR_PASSWORD -t "homeassistant/sensor/nvml-gpu/+/config"
This project is licensed under the MIT License - see the LICENSE file for details.