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.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
fix(smart): parse raw.string instead of raw.value for SATA attributes (#1239)
rcourtman47ceffe0c208
diff --git a/internal/smartctl/collector.go b/internal/smartctl/collector.goindex bee97252ce..f240b87749 100644--- a/internal/smartctl/collector.go+++ b/internal/smartctl/collector.go@@ -354,7 +354,7 @@ func parseSMARTAttributes(data *smartctlJSON, diskType string) *SMARTAttributes// SATA / SAS — iterate the ATA attributes tablefor _, attr := range data.ATASmartAttributes.Table {hasData = true- raw := attr.Raw.Value+ raw := parseRawValue(attr.Raw.String, attr.Raw.Value)switch attr.ID {case 5: // Reallocated Sector Countv := raw@@ -384,6 +384,30 @@ func parseSMARTAttributes(data *smartctlJSON, diskType string) *SMARTAttributesreturn attrs}+// parseRawValue extracts the primary integer from a SMART attribute's raw string.+// Some drives (notably Seagate) pack vendor-specific data in the upper bytes of+// the 48-bit raw value, making raw.value unreliable. For example, Power_On_Hours+// may report raw.value=150323855943 while raw.string="16951 (223 173 0)" where+// only 16951 is the actual hours. Falls back to rawValue if string parsing fails.+func parseRawValue(rawString string, rawValue int64) int64 {+ s := strings.TrimSpace(rawString)+ if s == "" {+ return rawValue+ }+ end := 0+ for end < len(s) && s[end] >= '0' && s[end] <= '9' {+ end+++ }+ if end == 0 {+ return rawValue+ }+ v, err := strconv.ParseInt(s[:end], 10, 64)+ if err != nil {+ return rawValue+ }+ return v+}+// detectDiskType determines the disk transport type from smartctl output.func detectDiskType(data smartctlJSON) string {protocol := strings.ToLower(data.Device.Protocol)diff --git a/internal/smartctl/collector_test.go b/internal/smartctl/collector_test.goindex d7f8be7771..7f38bd01c2 100644--- a/internal/smartctl/collector_test.go+++ b/internal/smartctl/collector_test.go@@ -354,6 +354,68 @@ func TestParseSMARTAttributes_Standby(t *testing.T) {}}+func TestParseRawValue(t *testing.T) {+ tests := []struct {+ name string+ str string+ value int64+ expected int64+ }{+ {"plain number", "16951", 150323855943, 16951},+ {"seagate power-on hours", "16951 (223 173 0)", 150323855943, 16951},+ {"simple value matches", "12345", 12345, 12345},+ {"empty string fallback", "", 42, 42},+ {"whitespace only fallback", " ", 42, 42},+ {"zero", "0", 999, 0},+ {"non-numeric fallback", "unknown", 77, 77},+ }+ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ got := parseRawValue(tt.str, tt.value)+ if got != tt.expected {+ t.Errorf("parseRawValue(%q, %d) = %d, want %d", tt.str, tt.value, got, tt.expected)+ }+ })+ }+}++func TestParseSMARTAttributes_SeagateRawValues(t *testing.T) {+ // Seagate drives pack vendor-specific data in upper bytes of the 48-bit raw+ // value, making raw.value huge. raw.string contains the correct interpretation.+ data := &smartctlJSON{}+ data.ATASmartAttributes.Table = []struct {+ ID int `json:"id"`+ Name string `json:"name"`+ Value int `json:"value"`+ Worst int `json:"worst"`+ Thresh int `json:"thresh"`+ Raw struct {+ Value int64 `json:"value"`+ String string `json:"string"`+ } `json:"raw"`+ }{+ {ID: 9, Name: "Power_On_Hours", Raw: struct {+ Value int64 `json:"value"`+ String string `json:"string"`+ }{Value: 150323855943, String: "16951 (223 173 0)"}},+ {ID: 12, Name: "Power_Cycle_Count", Raw: struct {+ Value int64 `json:"value"`+ String string `json:"string"`+ }{Value: 94, String: "94"}},+ }++ attrs := parseSMARTAttributes(data, "sata")+ if attrs == nil {+ t.Fatal("expected non-nil attributes")+ }+ if attrs.PowerOnHours == nil || *attrs.PowerOnHours != 16951 {+ t.Errorf("PowerOnHours: got %v, want 16951 (should parse from raw.string, not raw.value)", attrs.PowerOnHours)+ }+ if attrs.PowerCycles == nil || *attrs.PowerCycles != 94 {+ t.Errorf("PowerCycles: got %v, want 94", attrs.PowerCycles)+ }+}+func TestMatchesDeviceExclude(t *testing.T) {tests := []struct {name string