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.

3.3 KB
Raw Blame
package main

import (
	"context"
	"encoding/json"
	"fmt"
	"time"

	"github.com/rcourtman/pulse-go-rewrite/internal/agenthelper"
	"github.com/rcourtman/pulse-go-rewrite/internal/hostagent"
	agentshost "github.com/rcourtman/pulse-go-rewrite/pkg/agents/host"
)

// localSMARTProvider reuses the canonical SMART collector inside the
// network-isolated helper process. Its protocol request has no path or
// argument fields; device discovery and command construction remain entirely
// provider-owned.
type localSMARTProvider struct{}

func (localSMARTProvider) Snapshot(ctx context.Context) (json.RawMessage, error) {
	disks, err := hostagent.CollectSMARTLocal(ctx, nil)
	if err != nil {
		return nil, fmt.Errorf("collect SMART snapshot: %w", err)
	}
	result, err := json.Marshal(struct {
		Disks []hostagent.DiskSMART `json:"disks"`
	}{Disks: disks})
	if err != nil {
		return nil, fmt.Errorf("encode SMART snapshot: %w", err)
	}
	return result, nil
}

// localProxmoxProvider runs the existing bounded node-local inventory as one
// typed operation. The collector cannot select a VMID, filesystem path,
// command, or pct argument across the helper boundary.
type localProxmoxProvider struct {
	collect func(context.Context) hostagent.ProxmoxLXCFilesystemCollectionResult
}

func (p localProxmoxProvider) LXCFilesystems(ctx context.Context) (json.RawMessage, error) {
	return p.lxcFilesystems(ctx, false)
}

func (p localProxmoxProvider) LXCFilesystemsV2(ctx context.Context) (json.RawMessage, error) {
	return p.lxcFilesystems(ctx, true)
}

func (p localProxmoxProvider) lxcFilesystems(ctx context.Context, allowPartial bool) (json.RawMessage, error) {
	collect := p.collect
	if collect == nil {
		collect = hostagent.CollectProxmoxLXCFilesystemsLocalResult
	}
	collection := collect(ctx)
	if collection.Degraded && (!allowPartial || collection.Inventory == nil) {
		return nil, &agenthelper.ProviderError{
			Code:      agenthelper.ErrorProviderUnavailable,
			Message:   "Proxmox LXC filesystem inventory is incomplete",
			Retryable: true,
		}
	}
	if collection.Inventory != nil {
		if err := collection.Inventory.ValidateCollection(); err != nil ||
			(allowPartial && collection.Inventory.Status == "") ||
			(collection.Degraded != (collection.Inventory.Status == agentshost.ProxmoxLXCCollectionPartial)) ||
			collection.FailedContainers != len(collection.Inventory.OmittedVMIDs) {
			return nil, &agenthelper.ProviderError{Code: agenthelper.ErrorProviderUnavailable, Message: "Proxmox LXC filesystem inventory has invalid completeness", Retryable: true}
		}
	}
	// V1 decoders are strict: do not add even complete-status fields to their
	// response. A partial result never reaches an older collector as healthy.
	if !allowPartial && collection.Inventory != nil {
		legacy := struct {
			Containers  []agentshost.ProxmoxLXCContainer `json:"containers"`
			CollectedAt time.Time                        `json:"collectedAt"`
		}{collection.Inventory.Containers, collection.Inventory.CollectedAt}
		return json.Marshal(struct {
			Inventory any `json:"inventory"`
		}{legacy})
	}
	result, err := json.Marshal(struct {
		Inventory *agentshost.ProxmoxLXCInventory `json:"inventory"`
	}{Inventory: collection.Inventory})
	if err != nil {
		return nil, fmt.Errorf("encode Proxmox LXC filesystem inventory: %w", err)
	}
	return result, nil
}