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.

Keep incomplete TrueNAS pool readings out of usage and History

pulse-triage[bot]c5cee05f87e7
diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.md
index 14f3d6003a..a1018f117b 100644
--- a/docs/release-control/v6/internal/subsystems/monitoring.md
+++ b/docs/release-control/v6/internal/subsystems/monitoring.md
@@ -1665,6 +1665,7 @@ cleanup so readers cannot retain orphaned runtime or alert projections.
46. `internal/monitoring/temperature.go`
47. `internal/truenas/client.go`
47a. `internal/truenas/transport.go`
+47b. `internal/truenas/pool_capacity.go`
48. `internal/truenas/disk_health.go`
49. `internal/truenas/provider.go`
50. `internal/models/ceph_cluster_identity.go`
@@ -4000,6 +4001,44 @@ drift in non-identity display fields such as `buildtime`, including structured
date/value wrappers, and still preserve the canonical hostname, version,
machine ID, capacity, and poll-health path instead of failing connection tests
or background refreshes during JSON decoding.
+### TrueNAS pool capacity is an observation, not a default zero
+
+`internal/truenas/pool_capacity.go` owns validation of the existing pool-specific
+byte sources. Legacy REST consumes only its flat `size`, `allocated`, and
+optional `free` fields. RPC retains its existing pool-state/property envelope;
+the property envelope is also used by the separate boot-pool reader. Neither
+that boot source nor a hypothetical nested REST shape establishes a supported
+CORE data-pool capacity fallback. Missing native capacity remains unavailable;
+datasets, quotas, physical disks, and vdevs must not be summed to invent it.
+
+A usable reading requires exact non-negative integer total and allocated bytes,
+with a positive total and allocated no greater than total. Optional free bytes
+are derived from those same two pool bytes only when absent; an explicitly
+reported free value must equal their difference. Null, malformed, fractional,
+overflowing, negative, or contradictory fields invalidate the capacity reading,
+not the pool's identity, health, topology, scan, or the rest of the snapshot.
+Numeric strings are accepted without float conversion. RPC pool results use
+number-preserving decoding, and flat/property readings never mix or use a real
+zero as a fallback trigger. Transport, permission and invalid JSON-envelope
+errors retain their existing failure behaviour; there is no extra method call.
+
+The provider emits no pool usage metric for an unavailable or out-of-bounds
+reading. A host aggregate is available only if every inventoried pool has valid
+usage and its byte sum fits int64; a known boot pool is not a whole-host reading
+when a data pool is unknown. Available per-pool and dataset readings remain
+independent. Unknown polls must not append a zero or refresh an old observation
+in either memory or persistent storage History. Existing historical readings
+remain intact, and a later valid poll resumes at that poll's observation time.
+
+`internal/truenas/pool_capacity_test.go` covers the TLS legacy snapshot through
+provider/registry ingestion, real empty/full pools, absent/malformed fields,
+exact RPC bytes, source separation and incomplete/overflowing aggregates.
+`internal/monitoring/truenas_pool_capacity_test.go` connects TLS collection,
+provider refresh, two same-named connection-scoped estates, canonical ingestion,
+memory and persistent History over known/unknown/malformed/recovered polls.
+Its deterministic polling clock and API bodies are declared fixtures, not native
+CORE acceptance, a containing release or an alternative capacity source.
+
That same monitoring boundary now also owns live TrueNAS disk temperatures.
`internal/truenas/client.go` and `internal/truenas/provider.go` must ingest
legacy `disk.temperatures` from the REST API or `reporting.get_data` `disktemp`
diff --git a/docs/release-control/v6/internal/subsystems/registry.json b/docs/release-control/v6/internal/subsystems/registry.json
index 6af2bc1871..9bfa79e4d7 100644
--- a/docs/release-control/v6/internal/subsystems/registry.json
+++ b/docs/release-control/v6/internal/subsystems/registry.json
@@ -6195,6 +6195,7 @@
"internal/truenas/core_reporting.go",
"internal/truenas/disk_health.go",
"internal/truenas/fixtures.go",
+ "internal/truenas/pool_capacity.go",
"internal/truenas/provider.go",
"internal/truenas/subscription.go",
"internal/truenas/telemetry.go",
@@ -6208,6 +6209,7 @@
"internal/api/resourceapi/resources_test.go",
"internal/api/truenas_disk_history_integration_test.go",
"internal/monitoring/monitor_polling_test.go",
+ "internal/monitoring/truenas_pool_capacity_test.go",
"internal/truenas/alert_args_test.go",
"internal/truenas/client_api_shapes_test.go",
"internal/truenas/client_test.go",
@@ -6215,6 +6217,7 @@
"internal/truenas/core_reporting_test.go",
"internal/truenas/disk_history_identity_test.go",
"internal/truenas/disk_history_netdata_test.go",
+ "internal/truenas/pool_capacity_test.go",
"internal/truenas/provider_pool_health_contract_test.go",
"internal/truenas/provider_test.go",
"internal/truenas/transport_test.go"
diff --git a/internal/monitoring/truenas_pool_capacity_test.go b/internal/monitoring/truenas_pool_capacity_test.go
new file mode 100644
index 0000000000..6724db6f35
--- /dev/null
+++ b/internal/monitoring/truenas_pool_capacity_test.go
@@ -0,0 +1,178 @@
+package monitoring
+
+import (
+ "context"
+ "crypto/sha256"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "sync/atomic"
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/mock"
+ "github.com/rcourtman/pulse-go-rewrite/internal/models"
+ "github.com/rcourtman/pulse-go-rewrite/internal/truenas"
+ "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
+ "github.com/rcourtman/pulse-go-rewrite/pkg/metrics"
+)
+
+// Only the polling clock is deterministic. Inventory/capacity is fetched by
+// the real TLS client. This is not a capture from a native appliance.
+type poolCapacityClockFetcher struct {
+ truenas.APIFetcher
+ at time.Time
+}
+
+func (f *poolCapacityClockFetcher) Fetch(ctx context.Context) (*truenas.FixtureSnapshot, error) {
+ snapshot, err := f.APIFetcher.Fetch(ctx)
+ if err == nil {
+ snapshot.CollectedAt = f.at
+ }
+ return snapshot, err
+}
+
+func TestTrueNASPoolCapacityCollectionHistory(t *testing.T) {
+ previous := truenas.IsFeatureEnabled()
+ truenas.SetFeatureEnabled(true)
+ t.Cleanup(func() { truenas.SetFeatureEnabled(previous) })
+ previousMock := mock.IsMockEnabled()
+ mustSetMockEnabled(t, false)
+ t.Cleanup(func() { mustSetMockEnabled(t, previousMock) })
+
+ var cycle atomic.Int32
+ var poolReads atomic.Int32
+ fields := []string{
+ `,"size":1000,"allocated":400,"free":600`,
+ `,"size":1000`, // Must not refresh the last reading as zero.
+ `,"size":1000,"allocated":"unknown","free":600`,
+ `,"size":1000,"allocated":1000,"free":0`,
+ }
+ server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path == "/api/current" {
+ http.NotFound(w, r)
+ return
+ }
+ if r.Header.Get("Authorization") != "Bearer synthetic-a" && r.Header.Get("Authorization") != "Bearer synthetic-b" {
+ t.Error("collection lost its connection authentication")
+ w.WriteHeader(http.StatusUnauthorized)
+ return
+ }
+ w.Header().Set("Content-Type", "application/json")
+ var body string
+ switch r.URL.Path {
+ case "/api/v2.0/system/info":
+ body = `{"hostname":"same-host","version":"TrueNAS-13.0-U6.8","cores":4,"physmem":4096}`
+ case "/api/v2.0/pool":
+ poolReads.Add(1)
+ current := fields[cycle.Load()]
+ if r.Header.Get("Authorization") == "Bearer synthetic-b" {
+ current = `,"size":2000,"allocated":500,"free":1500`
+ }
+ body = `[{"id":1,"name":"tank","status":"DEGRADED","scan":{"function":"SCRUB","state":"FINISHED"},"topology":{"data":[{"type":"DISK","disk":"da0","status":"DEGRADED"}]}` + current + `}]`
+ case "/api/v2.0/boot/get_state":
+ body = `{"name":"boot-pool","status":"ONLINE","properties":{"size":{"parsed":200},"allocated":{"parsed":50},"free":{"parsed":150}}}`
+ case "/api/v2.0/pool/dataset":
+ body = `[{"id":"tank/quota","name":"tank/quota","pool":"tank","used":{"parsed":30},"available":{"parsed":70}}]`
+ case "/api/v2.0/disk":
+ body = `[{"identifier":"disk-1","name":"da0","size":10000,"rotationrate":"unknown","type":"UNKNOWN"}]`
+ case "/api/v2.0/alert/list", "/api/v2.0/service", "/api/v2.0/app", "/api/v2.0/vm", "/api/v2.0/sharing/smb", "/api/v2.0/sharing/nfs", "/api/v2.0/zfs/snapshot", "/api/v2.0/replication", "/api/v2.0/reporting/graphs", "/api/v2.0/reporting/get_data":
+ body = `[]`
+ default:
+ http.NotFound(w, r)
+ return
+ }
+ _, _ = w.Write([]byte(body))
+ }))
+ t.Cleanup(server.Close)
+ fingerprint := fmt.Sprintf("%x", sha256.Sum256(server.Certificate().Raw))
+ start := time.Now().UTC().Truncate(time.Second).Add(-5 * time.Minute)
+ providers := make([]*truenas.Provider, 2)
+ fetchers := make([]*poolCapacityClockFetcher, 2)
+ for i, name := range []string{"a", "b"} {
+ // Trust only this synthetic certificate's pin. The self-signed lab
+ // certificate is not a host trust-store or production TLS change.
+ client, err := truenas.NewClient(truenas.ClientConfig{Host: server.URL, UseHTTPS: true, InsecureSkipVerify: true, Fingerprint: fingerprint, APIKey: "synthetic-" + name})
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(client.Close)
+ fetchers[i] = &poolCapacityClockFetcher{APIFetcher: truenas.APIFetcher{Client: client}, at: start}
+ providers[i] = truenas.NewLiveProviderForConnection(fetchers[i], "connection-"+name)
+ }
+ persistent, err := metrics.NewStore(metrics.DefaultConfig(t.TempDir()))
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Cleanup(func() { _ = persistent.Close() })
+ adapter := unifiedresources.NewMonitorAdapter(nil)
+ monitor := &Monitor{resourceStore: adapter, metricsHistory: NewMetricsHistory(100, time.Hour), metricsStore: persistent}
+ hostA := unifiedresources.SourceSpecificID(unifiedresources.ResourceTypeAgent, unifiedresources.SourceTrueNAS, "system:connection-a")
+ hostB := unifiedresources.SourceSpecificID(unifiedresources.ResourceTypeAgent, unifiedresources.SourceTrueNAS, "system:connection-b")
+ targets := map[string]string{}
+ for step := range fields {
+ cycle.Store(int32(step))
+ var records []unifiedresources.IngestRecord
+ for i, provider := range providers {
+ fetchers[i].at = start.Add(time.Duration(step) * time.Minute)
+ if err := provider.Refresh(context.Background()); err != nil {
+ t.Fatalf("capacity metadata aborted collection: %v", err)
+ }
+ records = append(records, provider.Records()...)
+ }
+ adapter.PopulateSnapshotAndSupplemental(models.StateSnapshot{}, map[unifiedresources.DataSource][]unifiedresources.IngestRecord{unifiedresources.SourceTrueNAS: records})
+ for _, resource := range adapter.GetAll() {
+ if resource.Storage != nil && resource.Name == "tank" {
+ if resource.ParentID == nil {
+ t.Fatal("pool lost its owning connection")
+ }
+ targets[*resource.ParentID] = resource.ID
+ if resource.Status != unifiedresources.StatusWarning || resource.Storage.ZFSPool == nil || resource.Storage.ZFSPool.ScanDetails == nil {
+ t.Fatal("capacity availability lost pool health/scan")
+ }
+ }
+ if resource.Type == unifiedresources.ResourceTypeAgent && resource.ID == hostA {
+ if (resource.Metrics.Disk != nil) != (step == 0 || step == 3) {
+ t.Fatal("known boot pool was presented as whole-host capacity")
+ }
+ }
+ }
+ monitor.syncUnifiedStorageMetrics(adapter)
+ }
+ if poolReads.Load() != 8 || len(targets) != 2 {
+ t.Fatalf("reads=%d independent pool identities=%d", poolReads.Load(), len(targets))
+ }
+ persistent.Flush()
+ for _, resource := range adapter.GetAll() {
+ if resource.Storage == nil {
+ continue
+ }
+ wantValues := []float64{40, 100}
+ wantTimes := []time.Time{start, start.Add(3 * time.Minute)}
+ if resource.Name == "tank/quota" {
+ wantValues = []float64{30, 30, 30, 30}
+ wantTimes = []time.Time{start, start.Add(time.Minute), start.Add(2 * time.Minute), start.Add(3 * time.Minute)}
+ } else if resource.Name == "boot-pool" {
+ continue
+ } else if resource.ParentID != nil && *resource.ParentID == hostB {
+ wantValues = []float64{25, 25, 25, 25}
+ wantTimes = []time.Time{start, start.Add(time.Minute), start.Add(2 * time.Minute), start.Add(3 * time.Minute)}
+ }
+ // Metrics target resolution, not a guessed cross-host pool name, owns
+ // both the memory series and persistent History identity.
+ target := adapter.MetricsTargetForResource(resource.ID)
+ if target == nil || target.ResourceType != "storage" {
+ t.Fatal("pool/dataset lost its storage target")
+ }
+ memory := monitor.GetStorageMetrics(target.ResourceID, time.Hour)["usage"]
+ stored, err := persistent.Query("storage", target.ResourceID, "usage", start.Add(-time.Second), time.Now(), 0)
+ if err != nil || len(memory) != len(wantValues) || len(stored) != len(wantValues) {
+ t.Fatalf("%s History points memory=%d persisted=%d want=%d err=%v", resource.ID, len(memory), len(stored), len(wantValues), err)
+ }
+ for i, value := range wantValues {
+ if memory[i].Value != value || stored[i].Value != value || !memory[i].Timestamp.Equal(wantTimes[i]) || !stored[i].Timestamp.Equal(wantTimes[i]) {
+ t.Fatal("unknown capacity became zero, stale refresh, wrong units, timestamp or connection")
+ }
+ }
+ }
+}
diff --git a/internal/truenas/client.go b/internal/truenas/client.go
index a9a1a4fa06..7e2d59cc7a 100644
--- a/internal/truenas/client.go
+++ b/internal/truenas/client.go
@@ -545,7 +545,7 @@ func (c *Client) GetPools(ctx context.Context) ([]Pool, error) {
}
func (c *Client) getPoolsRPC(ctx context.Context) ([]Pool, error) {
- var response []map[string]any
+ var response []poolStateResponse
if err := c.queryRPC(ctx, "pool.query", &response); err != nil {
return nil, err
}
@@ -563,7 +563,7 @@ func (c *Client) getPoolsRPC(ctx context.Context) ([]Pool, error) {
// GetBootPool returns the boot pool from boot.get_state. The endpoint is
// separate from pool.query on supported CORE and SCALE releases.
func (c *Client) GetBootPool(ctx context.Context) (*Pool, error) {
- var response map[string]any
+ var response poolStateResponse
legacy, err := c.useLegacyREST(ctx)
if err != nil {
return nil, err
@@ -619,6 +619,7 @@ func parsePoolState(item map[string]any, isBoot bool) (Pool, bool) {
}
vdevs, diskMembers := poolTopologyFromTopology(topology)
readErrors, writeErrors, checksumErrors := poolErrorTotals(topology)
+ total, used, free := poolCapacity(item, properties)
pool := Pool{
ID: id,
@@ -627,9 +628,9 @@ func parsePoolState(item map[string]any, isBoot bool) (Pool, bool) {
Status: status,
StatusCode: strings.TrimSpace(readStringAny(item, "status_code", "statusCode")),
StatusDetail: strings.TrimSpace(readStringAny(item, "status_detail", "statusDetail")),
- TotalBytes: readInt64Any(item, "size", "total", "total_bytes", "totalBytes"),
- UsedBytes: readInt64Any(item, "allocated", "used", "used_bytes", "usedBytes"),
- FreeBytes: readInt64Any(item, "free", "free_bytes", "freeBytes", "available"),
+ TotalBytes: total,
+ UsedBytes: used,
+ FreeBytes: free,
ReadErrors: readErrors,
WriteErrors: writeErrors,
ChecksumErrors: checksumErrors,
@@ -638,15 +639,6 @@ func parsePoolState(item map[string]any, isBoot bool) (Pool, bool) {
IsBoot: isBoot,
DiskMembers: diskMembers,
}
- if pool.TotalBytes == 0 {
- pool.TotalBytes = readInt64Any(properties, "size", "total", "total_bytes", "totalBytes")
- }
- if pool.UsedBytes == 0 {
- pool.UsedBytes = readInt64Any(properties, "allocated", "used", "used_bytes", "usedBytes")
- }
- if pool.FreeBytes == 0 {
- pool.FreeBytes = readInt64Any(properties, "free", "free_bytes", "freeBytes", "available")
- }
return pool, true
}
@@ -973,6 +965,7 @@ func (c *Client) getPoolsREST(ctx context.Context) ([]Pool, error) {
}
vdevs, diskMembers := poolTopologyFromTopology(topology)
readErrors, writeErrors, checksumErrors := poolErrorTotals(topology)
+ total, used, free := poolRESTCapacity(item)
pools = append(pools, Pool{
ID: id,
@@ -981,9 +974,9 @@ func (c *Client) getPoolsREST(ctx context.Context) ([]Pool, error) {
Status: strings.TrimSpace(item.Status),
StatusCode: strings.TrimSpace(item.StatusCode),
StatusDetail: strings.TrimSpace(item.StatusDetail),
- TotalBytes: item.Size,
- UsedBytes: item.Allocated,
- FreeBytes: item.Free,
+ TotalBytes: total,
+ UsedBytes: used,
+ FreeBytes: free,
ReadErrors: readErrors,
WriteErrors: writeErrors,
ChecksumErrors: checksumErrors,
@@ -4885,9 +4878,9 @@ type poolResponse struct {
Status string `json:"status"`
StatusCode string `json:"status_code"`
StatusDetail string `json:"status_detail"`
- Size int64 `json:"size"`
- Allocated int64 `json:"allocated"`
- Free int64 `json:"free"`
+ Size json.RawMessage `json:"size"`
+ Allocated json.RawMessage `json:"allocated"`
+ Free json.RawMessage `json:"free"`
Topology json.RawMessage `json:"topology"`
Scan json.RawMessage `json:"scan"`
}
diff --git a/internal/truenas/pool_capacity.go b/internal/truenas/pool_capacity.go
new file mode 100644
index 0000000000..6ea9fb6dd3
--- /dev/null
+++ b/internal/truenas/pool_capacity.go
@@ -0,0 +1,127 @@
+package truenas
+
+import (
+ "bytes"
+ "encoding/json"
+ "math"
+ "strconv"
+ "strings"
+)
+
+// poolStateResponse preserves integer bytes when the RPC envelope decodes its
+// result. An intermediate float64 cannot represent every native int64 value.
+// Other RPC result shapes retain their existing decoder.
+type poolStateResponse map[string]any
+
+func (p *poolStateResponse) UnmarshalJSON(raw []byte) error {
+ decoder := json.NewDecoder(bytes.NewReader(raw))
+ decoder.UseNumber()
+ return decoder.Decode((*map[string]any)(p))
+}
+
+var poolTotalKeys = []string{"size", "total", "total_bytes", "totalBytes"}
+var poolUsedKeys = []string{"allocated", "used", "used_bytes", "usedBytes"}
+var poolFreeKeys = []string{"free", "free_bytes", "freeBytes", "available"}
+
+// poolCapacity accepts a coherent pool-specific reading, not a dataset/disk
+// sum. Missing or invalid total/allocated bytes mean unknown usage, not an
+// empty pool. Free may be derived from those two pool bytes only when absent;
+// a supplied free reading must agree. Never mix flat and property sources,
+// or replace an explicitly supplied zero/null/invalid field via a fallback.
+func poolCapacity(item, properties map[string]any) (total, used, free int64) {
+ source := item
+ nested := false
+ if !hasPoolCapacityFields(item) {
+ source = properties
+ nested = true
+ }
+ total, totalOK, _ := poolByteField(source, poolTotalKeys, nested)
+ used, usedOK, _ := poolByteField(source, poolUsedKeys, nested)
+ if !totalOK || !usedOK || total <= 0 || used > total {
+ return 0, 0, 0
+ }
+ free, freeOK, supplied := poolByteField(source, poolFreeKeys, nested)
+ if supplied && (!freeOK || free != total-used) {
+ return 0, 0, 0
+ }
+ return total, used, total - used
+}
+
+func hasPoolCapacityFields(item map[string]any) bool {
+ for _, keys := range [][]string{poolTotalKeys, poolUsedKeys, poolFreeKeys} {
+ for _, key := range keys {
+ if _, ok := item[key]; ok {
+ return true
+ }
+ }
+ }
+ return false
+}
+
+func poolByteField(source map[string]any, keys []string, nested bool) (int64, bool, bool) {
+ for _, key := range keys {
+ value, present := source[key]
+ if !present {
+ continue
+ }
+ if nested {
+ if property, ok := value.(map[string]any); ok {
+ value, _ = firstAny(property, "parsed", "value", "rawvalue", "raw")
+ }
+ }
+ parsed, valid := poolByteValue(value)
+ return parsed, valid, true
+ }
+ return 0, false, false
+}
+
+func poolByteValue(value any) (int64, bool) {
+ var parsed int64
+ switch value := value.(type) {
+ case int64:
+ parsed = value
+ case int:
+ parsed = int64(value)
+ case json.Number:
+ var err error
+ parsed, err = value.Int64()
+ if err != nil {
+ return 0, false
+ }
+ case string:
+ var err error
+ parsed, err = strconv.ParseInt(strings.TrimSpace(value), 10, 64)
+ if err != nil {
+ return 0, false
+ }
+ case float64:
+ // Retain safe direct-map callers, without rounding an unsafe float,
+ // truncating fractions or accepting NaN/infinity as bytes.
+ if math.IsNaN(value) || value < 0 || value > 1<<53 || math.Trunc(value) != value {
+ return 0, false
+ }
+ parsed = int64(value)
+ default:
+ return 0, false
+ }
+ return parsed, parsed >= 0
+}
+
+func poolRESTCapacity(item poolResponse) (int64, int64, int64) {
+ fields := make(map[string]any, 3)
+ for key, raw := range map[string]json.RawMessage{"size": item.Size, "allocated": item.Allocated, "free": item.Free} {
+ if len(raw) == 0 {
+ continue
+ }
+ var value any
+ decoder := json.NewDecoder(bytes.NewReader(raw))
+ decoder.UseNumber()
+ // The enclosing JSON decoder has already validated syntax. If that
+ // ever changes, an invalid field still cannot become a measurement.
+ if err := decoder.Decode(&value); err != nil {
+ return 0, 0, 0
+ }
+ fields[key] = value
+ }
+ return poolCapacity(fields, nil)
+}
diff --git a/internal/truenas/pool_capacity_test.go b/internal/truenas/pool_capacity_test.go
index fbbc5aa5c4..ed82548faf 100644
--- a/internal/truenas/pool_capacity_test.go
+++ b/internal/truenas/pool_capacity_test.go
@@ -177,3 +177,32 @@ func TestTrueNASPoolCapacityAggregate(t *testing.T) {
})
}
}
+
+func TestTrueNASPoolCapacitySourcesDoNotMix(t *testing.T) {
+ properties := map[string]any{
+ "size": map[string]any{"parsed": int64(2000)}, "allocated": map[string]any{"parsed": int64(900)}, "free": map[string]any{"parsed": int64(1100)},
+ }
+ for _, tc := range []struct {
+ name string
+ flat map[string]any
+ want [3]int64
+ }{
+ {"property-only boot shape", nil, [3]int64{2000, 900, 1100}},
+ {"flat idle does not inherit property usage", map[string]any{"size": 1000, "allocated": 0, "free": 1000}, [3]int64{1000, 0, 1000}},
+ {"flat full does not inherit property free", map[string]any{"size": 1000, "allocated": 1000, "free": 0}, [3]int64{1000, 1000, 0}},
+ {"partial flat does not join a second source", map[string]any{"size": 1000}, [3]int64{}},
+ {"invalid flat does not try a second source", map[string]any{"size": "unknown", "allocated": 400}, [3]int64{}},
+ {"null free is not absent", map[string]any{"size": 1000, "allocated": 400, "free": nil}, [3]int64{}},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ item := map[string]any{"name": "tank", "status": "ONLINE", "properties": properties}
+ for key, value := range tc.flat {
+ item[key] = value
+ }
+ pool, ok := parseBootPoolState(item)
+ if !ok || !pool.IsBoot || [3]int64{pool.TotalBytes, pool.UsedBytes, pool.FreeBytes} != tc.want {
+ t.Fatalf("mixed or fabricated capacity: %+v", pool)
+ }
+ })
+ }
+}
diff --git a/internal/truenas/provider.go b/internal/truenas/provider.go
index e6363ad69e..8397f6c276 100644
--- a/internal/truenas/provider.go
+++ b/internal/truenas/provider.go
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
+ "math"
"os"
"strings"
"sync"
@@ -616,7 +617,7 @@ func truenasRecordsFromSnapshot(snapshot *FixtureSnapshot, connectionID string,
LastSeen: collectedAt,
UpdatedAt: collectedAt,
Metrics: &unifiedresources.ResourceMetrics{
- Disk: diskMetric(pool.TotalBytes, pool.UsedBytes),
+ Disk: poolDiskMetric(pool),
},
Storage: &unifiedresources.StorageMeta{
Type: "zfs-pool",
@@ -2089,12 +2090,29 @@ func aggregatePoolUsage(pools []Pool) (int64, int64) {
var total int64
var used int64
for _, pool := range pools {
+ // A known boot pool or one available data pool is not the complete
+ // host's storage usage. Keep per-pool readings, but omit the host
+ // aggregate if any pool is unknown or the byte sum would overflow.
+ if !poolUsageKnown(pool) || total > math.MaxInt64-pool.TotalBytes {
+ return 0, 0
+ }
total += pool.TotalBytes
used += pool.UsedBytes
}
return total, used
}
+func poolUsageKnown(pool Pool) bool {
+ return pool.TotalBytes > 0 && pool.UsedBytes >= 0 && pool.UsedBytes <= pool.TotalBytes
+}
+
+func poolDiskMetric(pool Pool) *unifiedresources.MetricValue {
+ if !poolUsageKnown(pool) {
+ return nil
+ }
+ return diskMetric(pool.TotalBytes, pool.UsedBytes)
+}
+
// systemSourceID keys an API-added TrueNAS system by the configured
// connection. The reported hostname is not an identity: two systems that
// report the same hostname (a DR clone, the default "truenas" name) must not