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
Parse smartctl 7.5 power_mode object on guarded probes
rcourtmana39935182de6
diff --git a/internal/hostagent/issue1690_smartctl75_power_mode_test.go b/internal/hostagent/issue1690_smartctl75_power_mode_test.gonew file mode 100644index 0000000000..cb6204a11b--- /dev/null+++ b/internal/hostagent/issue1690_smartctl75_power_mode_test.go@@ -0,0 +1,111 @@+package hostagent++// Discussion #1690: smartmontools 7.5 started emitting power_mode as a JSON+// object whenever the -n power guard runs, which is every rotational-disk+// probe. The parser declared the field as a string, so json.Unmarshal failed+// for the entire document and healthy spinning disks behind a SAS HBA were+// reported as having no usable SMART data while guard-free SSD probes kept+// working. These tests pin the smartctl 7.5 shapes end to end.++import (+ "errors"+ "testing"+)++func TestIssue1690PowerModeObjectActiveDiskParsesFully(t *testing.T) {+ result, err := parseSMARTOutput([]byte(`{+ "device":{"name":"/dev/sda","type":"sat","protocol":"ATA"},+ "model_name":"Hitachi HUA722020ALA330",+ "serial_number":"JK11A1YAJNX41V",+ "power_mode":{"ata_value":255,"name":"ACTIVE or IDLE"},+ "smart_status":{"passed":true},+ "temperature":{"current":60},+ "ata_smart_attributes":{"table":[+ {"id":194,"raw":{"value":60,"string":"60 (Min/Max 22/68)"}}+ ]}+ }`), smartctlTarget{Path: "/dev/sda", DeviceType: "sat"})+ if err != nil {+ t.Fatalf("parse smartctl 7.5 output with power_mode object: %v", err)+ }+ if result.Health != "PASSED" || result.Temperature != 60 {+ t.Fatalf("active disk lost SMART data behind power_mode object: %+v", result)+ }+ if result.Standby {+ t.Fatalf("ACTIVE or IDLE misread as standby: %+v", result)+ }+ if result.Serial != "JK11A1YAJNX41V" {+ t.Fatalf("serial lost behind power_mode object: %+v", result)+ }+}++func TestIssue1690PowerModeObjectStandbyAbortReportsStandby(t *testing.T) {+ for _, name := range []string{"STANDBY", "STANDBY_Y", "SLEEP"} {+ result, err := parseSMARTOutput([]byte(`{+ "device":{"name":"/dev/sda","type":"sat","protocol":"ATA"},+ "power_mode":{"ata_value":0,"name":"`+name+`"}+ }`), smartctlTarget{Path: "/dev/sda", DeviceType: "sat"})+ if err != nil {+ t.Fatalf("power_mode %s: aborted probe dropped instead of standby: %v", name, err)+ }+ if !result.Standby {+ t.Fatalf("power_mode %s not reported as standby: %+v", name, result)+ }+ }+}++func TestIssue1690PowerModeLegacyStringStillAccepted(t *testing.T) {+ result, err := parseSMARTOutput([]byte(`{+ "device":{"name":"/dev/sda","type":"sat","protocol":"ATA"},+ "power_mode":"STANDBY"+ }`), smartctlTarget{Path: "/dev/sda", DeviceType: "sat"})+ if err != nil {+ t.Fatalf("legacy string power_mode rejected: %v", err)+ }+ if !result.Standby {+ t.Fatalf("legacy string power_mode not reported as standby: %+v", result)+ }+}++func TestIssue1690PowerModeUnknownShapeCostsOnlyTheField(t *testing.T) {+ result, err := parseSMARTOutput([]byte(`{+ "device":{"name":"/dev/sda","type":"sat","protocol":"ATA"},+ "model_name":"Hitachi HUA722020ALA330",+ "serial_number":"JK11A1YAJNX41V",+ "power_mode":42,+ "smart_status":{"passed":true},+ "temperature":{"current":60}+ }`), smartctlTarget{Path: "/dev/sda", DeviceType: "sat"})+ if err != nil {+ t.Fatalf("numeric power_mode failed the whole document: %v", err)+ }+ if result.Health != "PASSED" || result.Temperature != 60 || result.Standby {+ t.Fatalf("unknown power_mode shape corrupted the result: %+v", result)+ }+}++func TestIssue1690TextFallbackCatchesEPCStandbyVariants(t *testing.T) {+ for _, line := range []string{+ "Device is in STANDBY mode, exit(3)",+ "Device is in STANDBY_Y mode, exit(3)",+ "Device is in STANDBY (OS) mode, exit(3)",+ "Device is in SLEEP mode, exit(3)",+ } {+ fallback := parseSMARTTextFallback(line)+ if !fallback.Standby {+ t.Fatalf("text fallback missed standby line %q", line)+ }+ }++ if parseSMARTTextFallback("SMART overall-health self-assessment test result: PASSED").Standby {+ t.Fatal("text fallback fabricated standby from a healthy line")+ }+}++func TestIssue1690EmptyDocumentStillUnavailable(t *testing.T) {+ _, err := parseSMARTOutput([]byte(`{+ "device":{"name":"/dev/sda","type":"sat","protocol":"ATA"}+ }`), smartctlTarget{Path: "/dev/sda", DeviceType: "sat"})+ if !errors.Is(err, errSMARTDataUnavailable) {+ t.Fatalf("document with no SMART evidence must stay unavailable, got %v", err)+ }+}diff --git a/internal/hostagent/smartctl.go b/internal/hostagent/smartctl.goindex 30f2a813b3..80db514ab7 100644--- a/internal/hostagent/smartctl.go+++ b/internal/hostagent/smartctl.go@@ -206,7 +206,38 @@ type smartctlJSON struct {} `json:"current"`} `json:"ata_sct_status"`NVMeSmartHealthInformationLog *nvmeSmartHealthInformationLogJSON `json:"nvme_smart_health_information_log"`- PowerMode string `json:"power_mode"`+ PowerMode smartctlPowerModeJSON `json:"power_mode"`+}++// smartctlPowerModeJSON absorbs every shape smartctl has used for the JSON+// power_mode field. smartmontools 7.5 introduced it as an object,+// {"ata_value": 255, "name": "ACTIVE or IDLE"}, emitted whenever the -n+// guard runs CHECK POWER MODE — which is every rotational-disk probe here.+// Decoding it into a plain string failed json.Unmarshal for the whole+// document, so healthy output from spinning disks degraded to the lossy text+// fallback and was dropped as "no usable SMART data" (discussion #1690). The+// decoder never propagates an error: an unrecognized future shape must cost+// only this field, never the document.+type smartctlPowerModeJSON struct {+ Name string `json:"name"`+ ATAValue int `json:"ata_value"`+}++func (p *smartctlPowerModeJSON) UnmarshalJSON(data []byte) error {+ var object struct {+ Name string `json:"name"`+ ATAValue int `json:"ata_value"`+ }+ if err := json.Unmarshal(data, &object); err == nil {+ p.Name = object.Name+ p.ATAValue = object.ATAValue+ return nil+ }+ var name string+ if err := json.Unmarshal(data, &name); err == nil {+ p.Name = name+ }+ return nil}type smartTextFallback struct {@@ -1862,7 +1893,7 @@ func parseSMARTOutput(output []byte, target smartctlTarget) (*DiskSMART, error)Model: smartData.ModelName,Serial: smartData.SerialNumber,Type: detectDiskType(smartData),- Standby: isStandbyPowerMode(smartData.PowerMode),+ Standby: isStandbyPowerMode(smartData.PowerMode.Name),LastUpdated: timeNow(),Collection: &diskinventory.CollectionStatus{},}@@ -2044,8 +2075,12 @@ func parseSMARTTextFallback(text string) smartTextFallback {}case strings.HasPrefix(lower, "serial number:"):fallback.Serial = strings.TrimSpace(line[len("Serial Number:"):])- case strings.Contains(lower, "device is in standby mode"),+ case strings.Contains(lower, "device is in standby"),+ strings.Contains(lower, "device is in sleep"),strings.Contains(lower, "standby (os)"):+ // "device is in standby" deliberately drops the " mode" suffix:+ // smartctl names EPC states STANDBY_Y / STANDBY (OS), and the+ // suffixed match silently missed both (discussion #1690).fallback.Standby = truecase strings.HasPrefix(lower, "smart overall-health self-assessment test result:"):fallback.Health = parseSMARTHealthText(line)diff --git a/internal/hostagent/smartctl_test.go b/internal/hostagent/smartctl_test.goindex 2c4380d478..5705f813f2 100644--- a/internal/hostagent/smartctl_test.go+++ b/internal/hostagent/smartctl_test.go@@ -423,7 +423,7 @@ func TestParseSMARTOutputStandbyPowerMode(t *testing.T) {payload := smartctlJSON{ModelName: "WDC WD40EFRX",SerialNumber: "WD-123",- PowerMode: "STANDBY",+ PowerMode: smartctlPowerModeJSON{Name: "STANDBY"},}payload.Device.Protocol = "ATA"