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.

Align TrueNAS protection with native queries

rcourtman6293d6b312f4
diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.md
index b765d2c29b..72526816e3 100644
--- a/docs/release-control/v6/internal/subsystems/monitoring.md
+++ b/docs/release-control/v6/internal/subsystems/monitoring.md
@@ -128,7 +128,10 @@ truth for live infrastructure data.
`vm` resources, while SMB/NFS share data from `sharing.smb.query` and
`sharing.nfs.query` publishes native `TrueNASData.Share` on canonical
`network-share` resources parented to the owning dataset or pool when the
- API/path supplies that evidence.
+ API/path supplies that evidence. TrueNAS protection inventory follows the
+ same native-query rule: ZFS snapshots prefer `zfs.resource.snapshot.query`
+ with older `pool.snapshot.query`/REST compatibility fallback, and
+ replication tasks prefer `replication.query`.
## Forbidden Paths
diff --git a/internal/truenas/client.go b/internal/truenas/client.go
index d27e3db62c..4c8637d868 100644
--- a/internal/truenas/client.go
+++ b/internal/truenas/client.go
@@ -609,6 +609,10 @@ func (c *Client) getNetworkSharesREST(ctx context.Context, path, protocol string
}
func (c *Client) queryRPC(ctx context.Context, method string, result any) error {
+ return c.callRPC(ctx, method, []any{[]any{}, map[string]any{}}, result)
+}
+
+func (c *Client) callRPC(ctx context.Context, method string, params any, result any) error {
conn, err := c.dialRPC(ctx)
if err != nil {
return err
@@ -622,7 +626,7 @@ func (c *Client) queryRPC(ctx context.Context, method string, result any) error
if err := rpc.authenticate(ctx, c.config); err != nil {
return err
}
- return rpc.call(ctx, method, []any{[]any{}, map[string]any{}}, result)
+ return rpc.call(ctx, method, params, result)
}
// GetApps returns the best-effort TrueNAS app inventory as canonical workload
@@ -827,16 +831,46 @@ func (c *Client) executeAppAction(ctx context.Context, method, appID string) err
return nil
}
-// GetZFSSnapshots returns a best-effort list of ZFS snapshots.
-//
-// NOTE: TrueNAS exposes multiple snapshot APIs across versions. We intentionally parse the
-// response loosely and fall back to parsing dataset/snapshot names from the snapshot ID.
+// GetZFSSnapshots returns a best-effort list of ZFS snapshots. Modern TrueNAS
+// exposes snapshots through JSON-RPC; legacy REST remains a compatibility
+// fallback for older deployments.
func (c *Client) GetZFSSnapshots(ctx context.Context) ([]ZFSSnapshot, error) {
+ snapshots, err := c.getZFSSnapshotsRPC(ctx)
+ if err == nil {
+ return snapshots, nil
+ }
+ restSnapshots, restErr := c.getZFSSnapshotsREST(ctx)
+ if restErr != nil {
+ return nil, fmt.Errorf("fetch truenas zfs snapshots via rpc and rest: rpc=%w rest=%v", err, restErr)
+ }
+ return restSnapshots, nil
+}
+
+func (c *Client) getZFSSnapshotsRPC(ctx context.Context) ([]ZFSSnapshot, error) {
+ var response []map[string]any
+ if err := c.callRPC(ctx, "zfs.resource.snapshot.query", []any{map[string]any{
+ "paths": []string{},
+ "recursive": true,
+ "properties": []string{"creation", "used", "referenced"},
+ }}, &response); err == nil {
+ return parseZFSSnapshots(response), nil
+ }
+
+ if err := c.queryRPC(ctx, "pool.snapshot.query", &response); err != nil {
+ return nil, err
+ }
+ return parseZFSSnapshots(response), nil
+}
+
+func (c *Client) getZFSSnapshotsREST(ctx context.Context) ([]ZFSSnapshot, error) {
var response []map[string]any
if err := c.getJSON(ctx, http.MethodGet, "/zfs/snapshot", &response); err != nil {
return nil, err
}
+ return parseZFSSnapshots(response), nil
+}
+func parseZFSSnapshots(response []map[string]any) []ZFSSnapshot {
snapshots := make([]ZFSSnapshot, 0, len(response))
for _, item := range response {
full := readStringAny(item, "id", "name", "snapshot", "snapshot_name", "snapshotName")
@@ -855,6 +889,7 @@ func (c *Client) GetZFSSnapshots(ctx context.Context) ([]ZFSSnapshot, error) {
}
var createdAt *time.Time
+ properties, _ := item["properties"].(map[string]any)
if t := readTimeAny(item,
"created_at",
"createdAt",
@@ -865,12 +900,20 @@ func (c *Client) GetZFSSnapshots(ctx context.Context) ([]ZFSSnapshot, error) {
"datetime",
); t != nil {
createdAt = t
- } else if props, ok := item["properties"].(map[string]any); ok {
- createdAt = readTimeAny(props, "creation", "created", "datetime")
+ } else if properties != nil {
+ createdAt = readTimeAny(properties, "creation", "created", "datetime")
}
usedBytes := readInt64PtrAny(item, "used", "used_bytes", "usedBytes")
referenced := readInt64PtrAny(item, "referenced", "referenced_bytes", "referencedBytes")
+ if properties != nil {
+ if usedBytes == nil {
+ usedBytes = readInt64PtrAny(properties, "used", "used_bytes", "usedBytes")
+ }
+ if referenced == nil {
+ referenced = readInt64PtrAny(properties, "referenced", "referenced_bytes", "referencedBytes")
+ }
+ }
if full == "" && dataset != "" && snapName != "" {
full = dataset + "@" + snapName
@@ -887,16 +930,39 @@ func (c *Client) GetZFSSnapshots(ctx context.Context) ([]ZFSSnapshot, error) {
})
}
- return snapshots, nil
+ return snapshots
}
// GetReplicationTasks returns a best-effort list of replication tasks including last-run state.
func (c *Client) GetReplicationTasks(ctx context.Context) ([]ReplicationTask, error) {
+ tasks, err := c.getReplicationTasksRPC(ctx)
+ if err == nil {
+ return tasks, nil
+ }
+ restTasks, restErr := c.getReplicationTasksREST(ctx)
+ if restErr != nil {
+ return nil, fmt.Errorf("fetch truenas replication tasks via rpc and rest: rpc=%w rest=%v", err, restErr)
+ }
+ return restTasks, nil
+}
+
+func (c *Client) getReplicationTasksRPC(ctx context.Context) ([]ReplicationTask, error) {
+ var response []map[string]any
+ if err := c.queryRPC(ctx, "replication.query", &response); err != nil {
+ return nil, err
+ }
+ return parseReplicationTasks(response), nil
+}
+
+func (c *Client) getReplicationTasksREST(ctx context.Context) ([]ReplicationTask, error) {
var response []map[string]any
if err := c.getJSON(ctx, http.MethodGet, "/replication", &response); err != nil {
return nil, err
}
+ return parseReplicationTasks(response), nil
+}
+func parseReplicationTasks(response []map[string]any) []ReplicationTask {
tasks := make([]ReplicationTask, 0, len(response))
for _, item := range response {
id := readStringAny(item, "id")
@@ -943,7 +1009,7 @@ func (c *Client) GetReplicationTasks(ctx context.Context) ([]ReplicationTask, er
})
}
- return tasks, nil
+ return tasks
}
// FetchSnapshot collects a complete fixture-compatible snapshot.
@@ -3464,7 +3530,7 @@ func readStringAny(record map[string]any, keys ...string) string {
return strconv.FormatInt(typed, 10)
case map[string]any:
// Try common wrapper shapes: { "rawvalue": "...", "parsed": ... }
- if s := readStringAny(typed, "rawvalue", "value", "parsed"); s != "" {
+ if s := readStringAny(typed, "rawvalue", "value", "parsed", "raw"); s != "" {
return s
}
}
@@ -3568,6 +3634,9 @@ func readInt64PtrAny(record map[string]any, keys ...string) *int64 {
if v, ok := parseInt64Any(nested["rawvalue"]); ok {
return &v
}
+ if v, ok := parseInt64Any(nested["raw"]); ok {
+ return &v
+ }
}
}
return nil
@@ -3659,6 +3728,11 @@ func readTimeAny(record map[string]any, keys ...string) *time.Time {
return t
}
}
+ if rawValue, ok := nested["raw"]; ok {
+ if t := parseTimeAny(rawValue); t != nil {
+ return t
+ }
+ }
}
}
return nil
diff --git a/internal/truenas/client_test.go b/internal/truenas/client_test.go
index b35c4bc678..104e76fe5f 100644
--- a/internal/truenas/client_test.go
+++ b/internal/truenas/client_test.go
@@ -463,6 +463,113 @@ func TestGetAppsEnrichesStatsFromRPC(t *testing.T) {
}
}
+func TestGetZFSSnapshotsUsesNativeSnapshotQueryShape(t *testing.T) {
+ server := newMockServerWithRPC(t, map[string]apiResponse{
+ "/api/v2.0/zfs/snapshot": {status: http.StatusInternalServerError, body: `{"error":"legacy snapshot endpoint should not be used"}`},
+ }, nil, func(t *testing.T, conn *websocket.Conn) {
+ authReq := readRPCRequest(t, conn)
+ if authReq.Method != "auth.login_with_api_key" {
+ t.Fatalf("expected api-key auth method, got %q", authReq.Method)
+ }
+ writeRPCResult(t, conn, authReq.ID, true)
+
+ request := readRPCRequest(t, conn)
+ if request.Method != "zfs.resource.snapshot.query" {
+ t.Fatalf("expected zfs.resource.snapshot.query, got %q", request.Method)
+ }
+ params, ok := request.Params.([]any)
+ if !ok || len(params) != 1 {
+ t.Fatalf("unexpected snapshot query params: %#v", request.Params)
+ }
+ writeRPCResult(t, conn, request.ID, []map[string]any{{
+ "name": "tank/apps@auto-20260331-0600",
+ "dataset": "tank/apps",
+ "snapshot_name": "auto-20260331-0600",
+ "properties": map[string]any{
+ "creation": map[string]any{"raw": "1710000000"},
+ "used": map[string]any{"raw": "1024"},
+ "referenced": map[string]any{"raw": "2048"},
+ },
+ }})
+ })
+ t.Cleanup(server.Close)
+
+ client := mustClientForServer(t, server.URL, ClientConfig{APIKey: "api-key"})
+ snapshots, err := client.GetZFSSnapshots(context.Background())
+ if err != nil {
+ t.Fatalf("GetZFSSnapshots() error = %v", err)
+ }
+ if len(snapshots) != 1 {
+ t.Fatalf("expected 1 snapshot, got %d", len(snapshots))
+ }
+ snapshot := snapshots[0]
+ if snapshot.FullName != "tank/apps@auto-20260331-0600" || snapshot.Dataset != "tank/apps" || snapshot.Name != "auto-20260331-0600" {
+ t.Fatalf("unexpected snapshot identity: %+v", snapshot)
+ }
+ if snapshot.CreatedAt == nil || !snapshot.CreatedAt.Equal(time.Unix(1710000000, 0).UTC()) {
+ t.Fatalf("unexpected snapshot creation time: %+v", snapshot.CreatedAt)
+ }
+ if snapshot.UsedBytes == nil || *snapshot.UsedBytes != 1024 || snapshot.Referenced == nil || *snapshot.Referenced != 2048 {
+ t.Fatalf("unexpected snapshot byte properties: %+v", snapshot)
+ }
+}
+
+func TestGetReplicationTasksUsesNativeReplicationQueryShape(t *testing.T) {
+ server := newMockServerWithRPC(t, map[string]apiResponse{
+ "/api/v2.0/replication": {status: http.StatusInternalServerError, body: `{"error":"legacy replication endpoint should not be used"}`},
+ }, nil, func(t *testing.T, conn *websocket.Conn) {
+ authReq := readRPCRequest(t, conn)
+ if authReq.Method != "auth.login_with_api_key" {
+ t.Fatalf("expected api-key auth method, got %q", authReq.Method)
+ }
+ writeRPCResult(t, conn, authReq.ID, true)
+
+ request := readRPCRequest(t, conn)
+ if request.Method != "replication.query" {
+ t.Fatalf("expected replication.query, got %q", request.Method)
+ }
+ params, ok := request.Params.([]any)
+ if !ok || len(params) != 2 {
+ t.Fatalf("expected replication.query filters/options params, got %#v", request.Params)
+ }
+ writeRPCResult(t, conn, request.ID, []map[string]any{{
+ "id": 7,
+ "name": "Offsite apps",
+ "direction": "PUSH",
+ "source_datasets": []string{"tank/apps"},
+ "target_dataset": "backup/apps",
+ "state": map[string]any{
+ "state": "SUCCESS",
+ "datetime": "2026-03-31T06:30:00Z",
+ "last_snapshot": "tank/apps@auto-20260331-0600",
+ },
+ }})
+ })
+ t.Cleanup(server.Close)
+
+ client := mustClientForServer(t, server.URL, ClientConfig{APIKey: "api-key"})
+ tasks, err := client.GetReplicationTasks(context.Background())
+ if err != nil {
+ t.Fatalf("GetReplicationTasks() error = %v", err)
+ }
+ if len(tasks) != 1 {
+ t.Fatalf("expected 1 task, got %d", len(tasks))
+ }
+ task := tasks[0]
+ if task.ID != "7" || task.Name != "Offsite apps" || task.Direction != "PUSH" {
+ t.Fatalf("unexpected replication identity: %+v", task)
+ }
+ if len(task.SourceDatasets) != 1 || task.SourceDatasets[0] != "tank/apps" || task.TargetDataset != "backup/apps" {
+ t.Fatalf("unexpected replication datasets: %+v", task)
+ }
+ if task.LastState != "SUCCESS" || task.LastSnapshot != "tank/apps@auto-20260331-0600" {
+ t.Fatalf("unexpected replication state: %+v", task)
+ }
+ if task.LastRun == nil || task.LastRun.Format(time.RFC3339) != "2026-03-31T06:30:00Z" {
+ t.Fatalf("unexpected replication last run: %+v", task.LastRun)
+ }
+}
+
func TestStartAndStopAppUseRPCMethods(t *testing.T) {
var rpcCalls int
server := newMockServerWithRPC(t, defaultAPIResponses(), nil, func(t *testing.T, conn *websocket.Conn) {