mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-30 01:32:00 +01:00
Disk-only snapshots already record their source and original age. Use that evidence during the one-time compatibility import, so known linked-agent, expired or unknown-origin readings cannot borrow a refreshed VM receipt. Change-source: pulse-maintainer Contract-Neutral: Corrects the compatibility import to the existing original-source-age eligibility contract; no public wire, configuration, agent-report or guest-command admission contract delta.
64 lines
2.2 KiB
Go
64 lines
2.2 KiB
Go
package monitoring
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/models"
|
|
"github.com/rcourtman/pulse-go-rewrite/pkg/proxmox"
|
|
)
|
|
|
|
const recentGuestAgentEvidenceMaxAge = 10 * time.Minute
|
|
|
|
func hasRecentGuestAgentEvidence(prev *models.VM, now time.Time) bool {
|
|
observedAt := guestAgentEvidenceTime(prev)
|
|
return !observedAt.IsZero() && !observedAt.After(now) && now.Sub(observedAt) <= recentGuestAgentEvidenceMaxAge
|
|
}
|
|
|
|
func guestAgentEvidenceTime(prev *models.VM) time.Time {
|
|
if prev == nil || prev.Type != "qemu" {
|
|
return time.Time{}
|
|
}
|
|
if prev.GuestAgentEvidence.Explicit || !prev.GuestAgentEvidence.ObservedAt.IsZero() {
|
|
return prev.GuestAgentEvidence.ObservedAt
|
|
}
|
|
// Import a legacy direct snapshot's receipt once. Modern producers mark
|
|
// evidence explicitly, including its absence, before preserving identity.
|
|
// Its subsequent LastSeen can therefore never refresh this original time.
|
|
if prev.AgentVersion != "" ||
|
|
prev.GuestAgentStatus == "available" ||
|
|
len(prev.IPAddresses) > 0 ||
|
|
len(prev.NetworkInterfaces) > 0 ||
|
|
prev.OSName != "" ||
|
|
prev.OSVersion != "" {
|
|
return prev.LastSeen
|
|
}
|
|
|
|
if len(prev.Disks) > 0 && !strings.HasPrefix(prev.DiskStatusReason, "prev-") {
|
|
// Disk provenance was available before query eligibility had its own
|
|
// annotation. Do not import a known linked-agent or unknown-age reading
|
|
// as fresh QGA evidence just because the legacy VM row was refreshed.
|
|
return guestDiskObservationTime(prev)
|
|
}
|
|
return time.Time{}
|
|
}
|
|
|
|
func retainGuestAgentEvidence(prev *models.VM, now time.Time) models.GuestAgentEvidence {
|
|
evidence := models.GuestAgentEvidence{Explicit: true}
|
|
renewGuestAgentEvidence(&evidence, guestAgentEvidenceTime(prev), now)
|
|
return evidence
|
|
}
|
|
|
|
func renewGuestAgentEvidence(evidence *models.GuestAgentEvidence, observedAt, now time.Time) {
|
|
if !observedAt.IsZero() && !observedAt.After(now) && observedAt.After(evidence.ObservedAt) {
|
|
evidence.ObservedAt = observedAt
|
|
}
|
|
}
|
|
|
|
func shouldQueryGuestAgent(vmStatus *proxmox.VMStatus, prev *models.VM, now time.Time) bool {
|
|
if vmStatus != nil {
|
|
return vmStatus.Lock == "" && vmStatus.Agent.IsAvailable()
|
|
}
|
|
return hasRecentGuestAgentEvidence(prev, now)
|
|
}
|