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.

Collect disks from the agent mount namespace

pulse-triage[bot]9d5cdfa08879
diff --git a/internal/hostmetrics/collector.go b/internal/hostmetrics/collector.go
index f7d6977457..557f8f6f5f 100644
--- a/internal/hostmetrics/collector.go
+++ b/internal/hostmetrics/collector.go
@@ -3,6 +3,8 @@ package hostmetrics
import (
"context"
"fmt"
+ "os"
+ "runtime"
"sort"
"strings"
"sync"
@@ -11,6 +13,7 @@ import (
agentshost "github.com/rcourtman/pulse-go-rewrite/pkg/agents/host"
"github.com/rcourtman/pulse-go-rewrite/pkg/fsfilters"
"github.com/rs/zerolog/log"
+ gopsutilcommon "github.com/shirou/gopsutil/v4/common"
gocpu "github.com/shirou/gopsutil/v4/cpu"
godisk "github.com/shirou/gopsutil/v4/disk"
goload "github.com/shirou/gopsutil/v4/load"
@@ -25,13 +28,52 @@ var (
cpuTimes = gocpu.TimesWithContext
loadAvg = goload.AvgWithContext
virtualMemory = gomem.VirtualMemoryWithContext
- diskPartitions = godisk.PartitionsWithContext
+ diskPartitions = partitionsVisibleToAgent
diskUsage = godisk.UsageWithContext
diskIOCounters = godisk.IOCountersWithContext
netInterfaces = gonet.InterfacesWithContext
netIOCounters = gonet.IOCountersWithContext
)
+// partitionsVisibleToAgent asks gopsutil to enumerate the mount namespace the
+// collector actually runs in. gopsutil otherwise prefers /proc/1/mountinfo on
+// Linux. A systemd service with PrivateTmp (as installed by Pulse) has its own
+// mount namespace, so PID 1 can omit mounts that the collector can access and
+// that an operator explicitly selected with --disk-include.
+//
+// Containerised collectors can deliberately point gopsutil at host procfs.
+// Preserve either form of that override rather than replacing it with the
+// container process's namespace.
+func partitionsVisibleToAgent(ctx context.Context, all bool) ([]godisk.PartitionStat, error) {
+ return godisk.PartitionsWithContext(mountEnumerationContext(ctx), all)
+}
+
+func mountEnumerationContext(ctx context.Context) context.Context {
+ if runtime.GOOS != "linux" || hasConfiguredHostProc(ctx) {
+ return ctx
+ }
+ env := cloneGopsutilEnv(ctx)
+ env[gopsutilcommon.HostProcMountinfo] = "/proc/self/mountinfo"
+ return context.WithValue(ctx, gopsutilcommon.EnvKey, env)
+}
+
+func hasConfiguredHostProc(ctx context.Context) bool {
+ if strings.TrimSpace(os.Getenv("HOST_PROC")) != "" || strings.TrimSpace(os.Getenv("HOST_PROC_MOUNTINFO")) != "" {
+ return true
+ }
+ env, _ := ctx.Value(gopsutilcommon.EnvKey).(gopsutilcommon.EnvMap)
+ return strings.TrimSpace(env[gopsutilcommon.HostProcEnvKey]) != "" || strings.TrimSpace(env[gopsutilcommon.HostProcMountinfo]) != ""
+}
+
+func cloneGopsutilEnv(ctx context.Context) gopsutilcommon.EnvMap {
+ cloned := make(gopsutilcommon.EnvMap)
+ env, _ := ctx.Value(gopsutilcommon.EnvKey).(gopsutilcommon.EnvMap)
+ for key, value := range env {
+ cloned[key] = value
+ }
+ return cloned
+}
+
// diskUsageTimeout bounds a single mountpoint usage syscall. statfs on a
// healthy filesystem answers in microseconds; a hard-mounted network
// filesystem whose server is unreachable blocks it in an uninterruptible
diff --git a/internal/hostmetrics/collector_test.go b/internal/hostmetrics/collector_test.go
index 6492b2deae..c93327db69 100644
--- a/internal/hostmetrics/collector_test.go
+++ b/internal/hostmetrics/collector_test.go
@@ -3,12 +3,59 @@ package hostmetrics
import (
"context"
"encoding/json"
+ "runtime"
"testing"
+ gopsutilcommon "github.com/shirou/gopsutil/v4/common"
godisk "github.com/shirou/gopsutil/v4/disk"
gomem "github.com/shirou/gopsutil/v4/mem"
)
+func TestMountEnumerationContextUsesAgentNamespaceOnNativeLinux(t *testing.T) {
+ if runtime.GOOS != "linux" {
+ t.Skip("Linux mount namespaces only")
+ }
+ t.Setenv("HOST_PROC", "")
+ t.Setenv("HOST_PROC_MOUNTINFO", "")
+
+ original := gopsutilcommon.EnvMap{gopsutilcommon.HostSysEnvKey: "/host/sys"}
+ ctx := context.WithValue(context.Background(), gopsutilcommon.EnvKey, original)
+ got, ok := mountEnumerationContext(ctx).Value(gopsutilcommon.EnvKey).(gopsutilcommon.EnvMap)
+ if !ok {
+ t.Fatal("mount enumeration context has no gopsutil environment")
+ }
+ if got[gopsutilcommon.HostProcMountinfo] != "/proc/self/mountinfo" {
+ t.Fatalf("mountinfo = %q, want collector namespace", got[gopsutilcommon.HostProcMountinfo])
+ }
+ if got[gopsutilcommon.HostSysEnvKey] != "/host/sys" {
+ t.Fatalf("existing gopsutil context was discarded: %+v", got)
+ }
+ if _, mutated := original[gopsutilcommon.HostProcMountinfo]; mutated {
+ t.Fatal("caller's gopsutil environment was mutated")
+ }
+}
+
+func TestMountEnumerationContextPreservesHostProcOverride(t *testing.T) {
+ if runtime.GOOS != "linux" {
+ t.Skip("Linux mount namespaces only")
+ }
+ t.Setenv("HOST_PROC", "")
+ t.Setenv("HOST_PROC_MOUNTINFO", "")
+
+ original := gopsutilcommon.EnvMap{gopsutilcommon.HostProcEnvKey: "/host/proc"}
+ ctx := context.WithValue(context.Background(), gopsutilcommon.EnvKey, original)
+ got := mountEnumerationContext(ctx)
+ if got != ctx {
+ t.Fatal("explicit host proc context was replaced")
+ }
+
+ t.Setenv("HOST_PROC_MOUNTINFO", "/host/proc/1/mountinfo")
+ plain := context.Background()
+ if got := mountEnumerationContext(plain); got != plain {
+ t.Fatal("explicit host mountinfo environment was replaced")
+ }
+}
+
func TestCollectDiskIO(t *testing.T) {
ctx := context.Background()