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.

feat(alerts): detect SMART CRC counter growth

pulse-triage[bot]02cfd4f7b3b8
diff --git a/internal/alerts/active_cleanup.go b/internal/alerts/active_cleanup.go
index 94c6c6a6d4..6ee7a70dd3 100644
--- a/internal/alerts/active_cleanup.go
+++ b/internal/alerts/active_cleanup.go
@@ -202,6 +202,15 @@ func (m *Manager) Cleanup(maxAge time.Duration) {
}
}
+ for resourceID, snapshot := range m.smartCounterSnapshots {
+ if snapshot.LastObserved.IsZero() || now.Sub(snapshot.LastObserved) > staleTrackerAge {
+ delete(m.smartCounterSnapshots, resourceID)
+ log.Debug().
+ Str("resourceID", resourceID).
+ Msg("Cleaned up stale SMART counter snapshot")
+ }
+ }
+
m.mu.Unlock()
for _, alert := range autoAcked {
@@ -314,7 +323,7 @@ func (m *Manager) CleanupAlertsForNodes(existingNodes map[string]bool) {
// ClearActiveAlerts removes all active and pending alerts, resetting the manager state.
func (m *Manager) ClearActiveAlerts() {
m.mu.Lock()
- if len(m.activeAlerts) == 0 && len(m.intentPending) == 0 {
+ if len(m.activeAlerts) == 0 && len(m.intentPending) == 0 && len(m.smartCounterSnapshots) == 0 {
m.mu.Unlock()
return
}
@@ -329,6 +338,7 @@ func (m *Manager) ClearActiveAlerts() {
m.dockerRestartTracking = make(map[string]*dockerRestartRecord)
m.dockerUpdateFirstSeen = make(map[string]time.Time)
m.dockerUpdateFirstSeenByIdentity = make(map[string]time.Time)
+ m.smartCounterSnapshots = make(map[string]smartCounterSnapshot)
m.ackState = make(map[string]ackRecord)
m.ackStateByCanonical = make(map[string]ackRecord)
m.mu.Unlock()
diff --git a/internal/alerts/host.go b/internal/alerts/host.go
index a74c1b5fd9..caf4542158 100644
--- a/internal/alerts/host.go
+++ b/internal/alerts/host.go
@@ -15,6 +15,11 @@ import (
const HostOfflineAlertType = "host-offline"
+type smartCounterSnapshot struct {
+ UDMACRCErrors int64
+ LastObserved time.Time
+}
+
func hostResourceID(hostID string) string {
trimmed := strings.TrimSpace(hostID)
if trimmed == "" {
@@ -1138,12 +1143,47 @@ func (m *Manager) cleanupHostDiskAlerts(host models.Host, seen map[string]struct
func (m *Manager) syncHostSMARTDiskRiskAlerts(host models.Host, disk models.HostDiskSMART, resourceID, resourceName, nodeName, instanceName string, baseMetadata map[string]interface{}) {
assessment := storagehealth.AssessHostSMARTDisk(disk)
+ assessment.Reasons = append(assessment.Reasons, m.hostSMARTCounterGrowthReasons(resourceID, disk)...)
healthReasons, wearReasons := splitSMARTAlertReasons(assessment.Reasons)
m.syncHostSMARTDiskAlert(host, disk, resourceID, resourceName, nodeName, instanceName, baseMetadata, "disk-health", healthReasons)
m.syncHostSMARTDiskAlert(host, disk, resourceID, resourceName, nodeName, instanceName, baseMetadata, "disk-wearout", wearReasons)
}
+// hostSMARTCounterGrowthReasons turns a newly increased SMART counter into an
+// alertable health reason without warning on an old, non-zero counter at first
+// observation. A stable value clears the transient growth reason on the next
+// report, while the alert and notification histories retain the event.
+func (m *Manager) hostSMARTCounterGrowthReasons(resourceID string, disk models.HostDiskSMART) []storagehealth.Reason {
+ if disk.Attributes == nil || disk.Attributes.UDMACRCErrors == nil {
+ return nil
+ }
+
+ current := *disk.Attributes.UDMACRCErrors
+ if current < 0 {
+ return nil
+ }
+
+ observedAt := m.now()
+ m.mu.Lock()
+ previous, observed := m.smartCounterSnapshots[resourceID]
+ m.smartCounterSnapshots[resourceID] = smartCounterSnapshot{
+ UDMACRCErrors: current,
+ LastObserved: observedAt,
+ }
+ m.mu.Unlock()
+
+ if !observed || current <= previous.UDMACRCErrors {
+ return nil
+ }
+
+ return []storagehealth.Reason{{
+ Code: "crc_errors_increased",
+ Severity: storagehealth.RiskWarning,
+ Summary: fmt.Sprintf("UDMA CRC error count increased from %d to %d", previous.UDMACRCErrors, current),
+ }}
+}
+
func splitSMARTAlertReasons(reasons []storagehealth.Reason) ([]storagehealth.Reason, []storagehealth.Reason) {
healthReasons := make([]storagehealth.Reason, 0, len(reasons))
wearReasons := make([]storagehealth.Reason, 0, len(reasons))
@@ -1172,6 +1212,7 @@ var (
"offline_uncorrectable",
"media_errors",
"reallocated_sectors",
+ "crc_errors_increased",
}
smartWearoutAssessmentCodes = []string{
"wearout_low",
diff --git a/internal/alerts/host_smart_growth_test.go b/internal/alerts/host_smart_growth_test.go
new file mode 100644
index 0000000000..d54a19ab6b
--- /dev/null
+++ b/internal/alerts/host_smart_growth_test.go
@@ -0,0 +1,132 @@
+package alerts
+
+import (
+ "slices"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/rcourtman/pulse-go-rewrite/internal/models"
+)
+
+func TestCheckHostAlertsWhenSMARTCRCCountIncreases(t *testing.T) {
+ m := newTestManager(t)
+ m.ClearActiveAlerts()
+
+ crcErrors := int64(7)
+ host := models.Host{
+ ID: "crc-host",
+ Hostname: "crc-host",
+ Sensors: models.HostSensorSummary{
+ SMART: []models.HostDiskSMART{{
+ Device: "/dev/sda",
+ Health: "PASSED",
+ Attributes: &models.SMARTAttributes{
+ UDMACRCErrors: &crcErrors,
+ },
+ }},
+ },
+ }
+
+ alertKey := buildCanonicalStateID("agent:crc-host/disk:sda", "agent:crc-host/disk:sda-disk-health")
+ m.CheckHost(host)
+
+ m.mu.RLock()
+ _, exists := testLookupActiveAlert(t, m, alertKey)
+ m.mu.RUnlock()
+ if exists {
+ t.Fatal("historical CRC errors must not alert on the first observation")
+ }
+
+ crcErrors = 8
+ m.CheckHost(host)
+
+ m.mu.RLock()
+ alert := testRequireActiveAlert(t, m, alertKey)
+ m.mu.RUnlock()
+ if alert.Level != AlertLevelWarning {
+ t.Fatalf("Level = %q, want %q", alert.Level, AlertLevelWarning)
+ }
+ codes, ok := alert.Metadata["riskCodes"].([]string)
+ if !ok || !slices.Contains(codes, "crc_errors_increased") {
+ t.Fatalf("riskCodes = %#v, want crc_errors_increased", alert.Metadata["riskCodes"])
+ }
+ summaries, ok := alert.Metadata["riskSummaries"].([]string)
+ if !ok || len(summaries) != 1 || !strings.Contains(summaries[0], "increased from 7 to 8") {
+ t.Fatalf("riskSummaries = %#v, want counter delta", alert.Metadata["riskSummaries"])
+ }
+
+ // Growth is an event, not a warning on the historical non-zero value. Once
+ // the next sample is stable, the active alert resolves while history keeps it.
+ m.CheckHost(host)
+ m.mu.RLock()
+ _, exists = testLookupActiveAlert(t, m, alertKey)
+ m.mu.RUnlock()
+ if exists {
+ t.Fatal("stable CRC counter should resolve the growth alert")
+ }
+}
+
+func TestCheckHostSMARTCRCCounterResetEstablishesNewBaseline(t *testing.T) {
+ m := newTestManager(t)
+ m.ClearActiveAlerts()
+
+ crcErrors := int64(10)
+ host := models.Host{
+ ID: "reset-host",
+ Sensors: models.HostSensorSummary{SMART: []models.HostDiskSMART{{
+ Device: "/dev/sdb",
+ Attributes: &models.SMARTAttributes{UDMACRCErrors: &crcErrors},
+ }}},
+ }
+
+ alertKey := buildCanonicalStateID("agent:reset-host/disk:sdb", "agent:reset-host/disk:sdb-disk-health")
+ m.CheckHost(host)
+ crcErrors = 1
+ m.CheckHost(host)
+
+ m.mu.RLock()
+ _, exists := testLookupActiveAlert(t, m, alertKey)
+ m.mu.RUnlock()
+ if exists {
+ t.Fatal("counter reset must not be treated as growth")
+ }
+
+ crcErrors = 2
+ m.CheckHost(host)
+ m.mu.RLock()
+ alert := testRequireActiveAlert(t, m, alertKey)
+ m.mu.RUnlock()
+ if alert.Level != AlertLevelWarning {
+ t.Fatalf("Level = %q, want %q", alert.Level, AlertLevelWarning)
+ }
+}
+
+func TestCleanupRemovesOnlyStaleSMARTCounterSnapshots(t *testing.T) {
+ m := newTestManager(t)
+ now := time.Now()
+
+ m.mu.Lock()
+ m.smartCounterSnapshots["agent:old/disk:sda"] = smartCounterSnapshot{
+ UDMACRCErrors: 3,
+ LastObserved: now.Add(-25 * time.Hour),
+ }
+ m.smartCounterSnapshots["agent:current/disk:sdb"] = smartCounterSnapshot{
+ UDMACRCErrors: 4,
+ LastObserved: now.Add(-time.Hour),
+ }
+ m.mu.Unlock()
+
+ m.Cleanup(time.Hour)
+
+ m.mu.RLock()
+ _, oldExists := m.smartCounterSnapshots["agent:old/disk:sda"]
+ _, currentExists := m.smartCounterSnapshots["agent:current/disk:sdb"]
+ m.mu.RUnlock()
+ if oldExists {
+ t.Fatal("stale SMART counter snapshot was not removed")
+ }
+ if !currentExists {
+ t.Fatal("current SMART counter snapshot was removed")
+ }
+}
diff --git a/internal/alerts/manager.go b/internal/alerts/manager.go
index cb0f99220b..bf7cdb1fba 100644
--- a/internal/alerts/manager.go
+++ b/internal/alerts/manager.go
@@ -77,6 +77,10 @@ type Manager struct {
dockerUpdateFirstSeenByIdentity map[string]time.Time
// PMG quarantine growth tracking
pmgQuarantineHistory map[string][]pmgQuarantineSnapshot // Track quarantine snapshots for growth detection
+ // SMART counter snapshots let alert evaluation distinguish historical
+ // counters from new disk errors. They are keyed by the canonical disk
+ // resource ID and intentionally retain only the latest observation.
+ smartCounterSnapshots map[string]smartCounterSnapshot
// PMG anomaly detection tracking
pmgAnomalyTrackers map[string]*pmgAnomalyTracker // Track mail metrics for anomaly detection per PMG instance
// Persistent acknowledgement state so quick alert rebuilds keep user acknowledgements
@@ -188,6 +192,7 @@ func NewManagerWithDataDir(dataDir string, options ...ManagerOption) *Manager {
dockerUpdateFirstSeen: make(map[string]time.Time),
dockerUpdateFirstSeenByIdentity: make(map[string]time.Time),
pmgQuarantineHistory: make(map[string][]pmgQuarantineSnapshot),
+ smartCounterSnapshots: make(map[string]smartCounterSnapshot),
pmgAnomalyTrackers: make(map[string]*pmgAnomalyTracker),
ackState: make(map[string]ackRecord),
ackStateByCanonical: make(map[string]ackRecord),
diff --git a/internal/alerts/tracking_cleanup.go b/internal/alerts/tracking_cleanup.go
index f21ef5332b..9c546f0443 100644
--- a/internal/alerts/tracking_cleanup.go
+++ b/internal/alerts/tracking_cleanup.go
@@ -82,6 +82,13 @@ func (m *Manager) cleanupStaleMaps() {
}
}
+ for resourceID, snapshot := range m.smartCounterSnapshots {
+ if snapshot.LastObserved.IsZero() || now.Sub(snapshot.LastObserved) > staleThreshold {
+ delete(m.smartCounterSnapshots, resourceID)
+ cleaned++
+ }
+ }
+
rateLimitThreshold := RateLimitCleanupWindow
for resourceID, times := range m.alertRateLimit {
var recent []time.Time