Power Li пре 1 година
родитељ
комит
11ce9e2fd4
4 измењених фајлова са 36 додато и 3 уклоњено
  1. 14 1
      cmd/ha-gpu-ccd/main.go
  2. BIN
      ha-gpu-ccd
  3. 14 1
      main.go
  4. 8 1
      pkg/nvidia/nvidia.go

+ 14 - 1
cmd/ha-gpu-ccd/main.go

@@ -1,6 +1,8 @@
 package main
 
 import (
+	"crypto/rand"
+	"encoding/hex"
 	"encoding/json"
 	"fmt"
 	"log"
@@ -81,7 +83,18 @@ func run(cmd *cobra.Command, args []string) {
 func setupMQTTClient() mqtt.Client {
 	opts := mqtt.NewClientOptions()
 	opts.AddBroker(fmt.Sprintf("tcp://%s:%d", mqttHost, mqttPort))
-	opts.SetClientID("ha-gpu-ccd")
+
+	// Generate random suffix for client ID to avoid conflicts
+	randomBytes := make([]byte, 3)
+	if _, err := rand.Read(randomBytes); err == nil {
+		clientID := fmt.Sprintf("ha-gpu-ccd-%s", hex.EncodeToString(randomBytes))
+		opts.SetClientID(clientID)
+	} else {
+		// Fallback to timestamp if random generation fails
+		clientID := fmt.Sprintf("ha-gpu-ccd-%d", time.Now().Unix())
+		opts.SetClientID(clientID)
+	}
+
 	opts.SetUsername(mqttUsername)
 	opts.SetPassword(mqttPassword)
 	opts.SetAutoReconnect(true)


+ 14 - 1
main.go

@@ -2,6 +2,8 @@ package main
 
 import (
 	"context"
+	"crypto/rand"
+	"encoding/hex"
 	"encoding/json"
 	"fmt"
 	"log"
@@ -165,7 +167,18 @@ func run(cmd *cobra.Command, args []string) {
 func setupMQTTClient() mqtt.Client {
 	opts := mqtt.NewClientOptions()
 	opts.AddBroker(fmt.Sprintf("tcp://%s:%d", cfg.MQTTHost, cfg.MQTTPort))
-	opts.SetClientID("nvml-gpu-ha")
+
+	// Generate random suffix for client ID to avoid conflicts
+	randomBytes := make([]byte, 3)
+	if _, err := rand.Read(randomBytes); err == nil {
+		clientID := fmt.Sprintf("nvml-gpu-ha-%s", hex.EncodeToString(randomBytes))
+		opts.SetClientID(clientID)
+	} else {
+		// Fallback to timestamp if random generation fails
+		clientID := fmt.Sprintf("nvml-gpu-ha-%d", time.Now().Unix())
+		opts.SetClientID(clientID)
+	}
+
 	opts.SetUsername(cfg.MQTTUsername)
 	opts.SetPassword(cfg.MQTTPassword)
 	opts.SetAutoReconnect(true)

+ 8 - 1
pkg/nvidia/nvidia.go

@@ -226,7 +226,14 @@ func GetDeviceID(device GPUDevice) string {
 	shortPCIBusID := GetShortPCIBusID(device.PCIBusID)
 	deviceID := strings.Replace(shortPCIBusID, ":", "_", -1)
 	deviceID = strings.Replace(deviceID, ".", "_", -1)
-	return strings.ToLower(deviceID)
+
+	// Add GPU UUID suffix (first 8 characters) to ensure uniqueness across different machines
+	uuidSuffix := strings.Replace(device.UUID, "-", "", -1)
+	if len(uuidSuffix) > 8 {
+		uuidSuffix = uuidSuffix[:8]
+	}
+
+	return strings.ToLower(fmt.Sprintf("%s_%s", deviceID, uuidSuffix))
 }
 
 // GetDeviceDisplayName generates a display name in the format: {HOSTNAME} {PCI ID} - NVIDIA {MODEL} {VRAM}