Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

fix(agent): collect temperature on FreeBSD via sysctl (#1254)

rcourtmandacf0f86c4f2
diff --git a/internal/hostagent/agent.go b/internal/hostagent/agent.go
index 07ba5c66f6..0d05356038 100644
--- a/internal/hostagent/agent.go
+++ b/internal/hostagent/agent.go
@@ -9,8 +9,10 @@ import (
"fmt"
"net"
"net/http"
+ "os/exec"
"runtime"
"sort"
+ "strconv"
"strings"
"time"
@@ -499,8 +501,12 @@ func normalisePlatform(platform string) string {
// collectTemperatures attempts to collect temperature data from the local system.
// Returns an empty Sensors struct if collection fails (best-effort).
func (a *Agent) collectTemperatures(ctx context.Context) agentshost.Sensors {
- // Only collect on Linux for now (lm-sensors is Linux-specific)
- if a.collector.GOOS() != "linux" {
+ switch a.collector.GOOS() {
+ case "linux":
+ // Continue below with lm-sensors path
+ case "freebsd":
+ return a.collectFreeBSDTemperatures(ctx)
+ default:
return agentshost.Sensors{}
}
@@ -594,6 +600,79 @@ func (a *Agent) collectTemperatures(ctx context.Context) agentshost.Sensors {
return result
}
+// collectFreeBSDTemperatures collects temperature data on FreeBSD via sysctl.
+// FreeBSD exposes CPU temperatures as dev.cpu.N.temperature and ACPI thermal
+// zones as hw.acpi.thermal.tzN.temperature.
+func (a *Agent) collectFreeBSDTemperatures(ctx context.Context) agentshost.Sensors {
+ cmdCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
+ defer cancel()
+
+ // Query all sysctl values and filter for temperature in Go
+ // (avoids sh -c and handles grep exit-code-1 on no matches)
+ cmd := exec.CommandContext(cmdCtx, "sysctl", "-a")
+ output, err := cmd.Output()
+ if err != nil {
+ a.logger.Debug().Err(err).Msg("Failed to collect FreeBSD temperature data via sysctl")
+ return agentshost.Sensors{}
+ }
+
+ result := parseFreeBSDSysctlTemperatures(string(output))
+
+ if len(result.TemperatureCelsius) > 0 {
+ a.logger.Debug().
+ Int("temperatureCount", len(result.TemperatureCelsius)).
+ Msg("Collected FreeBSD temperature data via sysctl")
+ }
+
+ return result
+}
+
+// parseFreeBSDSysctlTemperatures parses FreeBSD sysctl output and extracts
+// temperature readings. Expects lines like:
+//
+// dev.cpu.0.temperature: 45.0C
+// hw.acpi.thermal.tz0.temperature: 38.0C
+func parseFreeBSDSysctlTemperatures(sysctlOutput string) agentshost.Sensors {
+ result := agentshost.Sensors{
+ TemperatureCelsius: make(map[string]float64),
+ }
+
+ for _, line := range strings.Split(sysctlOutput, "\n") {
+ if !strings.Contains(line, ".temperature:") {
+ continue
+ }
+ parts := strings.SplitN(line, ":", 2)
+ if len(parts) != 2 {
+ continue
+ }
+ key := strings.TrimSpace(parts[0])
+ valStr := strings.TrimSpace(parts[1])
+ valStr = strings.TrimSuffix(valStr, "C")
+
+ temp, err := strconv.ParseFloat(valStr, 64)
+ if err != nil || temp <= 0 {
+ continue
+ }
+
+ // Convert sysctl keys to sensor names matching lm-sensors conventions
+ keyParts := strings.Split(key, ".")
+ switch {
+ case strings.HasPrefix(key, "dev.cpu.") && len(keyParts) >= 3:
+ // dev.cpu.0.temperature → cpu_core_0
+ result.TemperatureCelsius["cpu_core_"+keyParts[2]] = temp
+ case strings.HasPrefix(key, "hw.acpi.thermal.") && len(keyParts) >= 4:
+ // hw.acpi.thermal.tz0.temperature → acpi_tz0
+ result.TemperatureCelsius["acpi_"+keyParts[3]] = temp
+ default:
+ normalized := strings.ReplaceAll(key, ".", "_")
+ normalized = strings.TrimSuffix(normalized, "_temperature")
+ result.TemperatureCelsius[normalized] = temp
+ }
+ }
+
+ return result
+}
+
// collectRAIDArrays attempts to collect mdadm RAID array information.
// Returns an empty slice if collection fails (best-effort).
func (a *Agent) collectRAIDArrays(ctx context.Context) []agentshost.RAIDArray {
diff --git a/internal/hostagent/agent_sensors_test.go b/internal/hostagent/agent_sensors_test.go
index 420b9c892c..c7cd24ebcc 100644
--- a/internal/hostagent/agent_sensors_test.go
+++ b/internal/hostagent/agent_sensors_test.go
@@ -85,3 +85,57 @@ func TestAgent_collectTemperatures_SkipsNonLinux(t *testing.T) {
t.Fatalf("expected empty sensors on non-linux, got %#v", got.TemperatureCelsius)
}
}
+
+func TestParseFreeBSDSysctlTemperatures(t *testing.T) {
+ input := `kern.ostype: FreeBSD
+kern.osrelease: 14.0-RELEASE-p6
+dev.cpu.0.temperature: 45.0C
+dev.cpu.1.temperature: 47.5C
+dev.cpu.2.temperature: 44.0C
+dev.cpu.3.temperature: 46.0C
+hw.acpi.thermal.tz0.temperature: 38.0C
+hw.acpi.thermal.tz1.temperature: 42.5C
+hw.machine: amd64
+`
+ got := parseFreeBSDSysctlTemperatures(input)
+
+ want := map[string]float64{
+ "cpu_core_0": 45.0,
+ "cpu_core_1": 47.5,
+ "cpu_core_2": 44.0,
+ "cpu_core_3": 46.0,
+ "acpi_tz0": 38.0,
+ "acpi_tz1": 42.5,
+ }
+
+ if len(got.TemperatureCelsius) != len(want) {
+ t.Fatalf("got %d temperatures, want %d: %v", len(got.TemperatureCelsius), len(want), got.TemperatureCelsius)
+ }
+ for k, v := range want {
+ if gotVal, ok := got.TemperatureCelsius[k]; !ok || gotVal != v {
+ t.Fatalf("TemperatureCelsius[%q] = %v, want %v", k, gotVal, v)
+ }
+ }
+}
+
+func TestParseFreeBSDSysctlTemperatures_Empty(t *testing.T) {
+ got := parseFreeBSDSysctlTemperatures("kern.ostype: FreeBSD\n")
+ if len(got.TemperatureCelsius) != 0 {
+ t.Fatalf("expected empty result for no temperature data, got %v", got.TemperatureCelsius)
+ }
+}
+
+func TestParseFreeBSDSysctlTemperatures_MalformedLines(t *testing.T) {
+ input := `dev.cpu.0.temperature: badvalue
+dev.cpu.1.temperature: -5.0C
+dev.cpu.2.temperature: 0.0C
+dev.cpu.3.temperature: 55.0C
+`
+ got := parseFreeBSDSysctlTemperatures(input)
+ if len(got.TemperatureCelsius) != 1 {
+ t.Fatalf("expected 1 valid temperature, got %d: %v", len(got.TemperatureCelsius), got.TemperatureCelsius)
+ }
+ if got.TemperatureCelsius["cpu_core_3"] != 55.0 {
+ t.Fatalf("expected cpu_core_3=55.0, got %v", got.TemperatureCelsius["cpu_core_3"])
+ }
+}