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.

Detect SAS transport and parse SCSI attributes in SMART collection

rcourtman59e6f25a65dd
diff --git a/internal/hostagent/smartctl.go b/internal/hostagent/smartctl.go
index 434f5af430..7d600de2bd 100644
--- a/internal/hostagent/smartctl.go
+++ b/internal/hostagent/smartctl.go
@@ -174,7 +174,15 @@ type smartctlJSON struct {
SmartStatus *struct {
Passed bool `json:"passed"`
} `json:"smart_status,omitempty"`
- Temperature struct {
+ SCSITransportProtocol struct {
+ Name string `json:"name"`
+ } `json:"scsi_transport_protocol"`
+ PowerOnTime *struct {
+ Hours int64 `json:"hours"`
+ } `json:"power_on_time"`
+ SCSIGrownDefectList *int64 `json:"scsi_grown_defect_list"`
+ SCSIPercentageUsedEnduranceIndicator *int `json:"scsi_percentage_used_endurance_indicator"`
+ Temperature struct {
Current int `json:"current"`
} `json:"temperature"`
ATASmartAttributes struct {
@@ -313,6 +321,9 @@ func CollectSMARTLocal(ctx context.Context, diskExclude []string) ([]DiskSMART,
continue
}
refineLinuxBlockDeviceIdentity(smart, target)
+ if smart.Type == "" {
+ smart.Type = "sata"
+ }
// The refine step can rename the device (nvme0 -> nvme0n1), so re-apply
// exclusions against the canonical name the user actually sees.
if matchesDeviceExclude(smart.Device, "/dev/"+smart.Device, diskExclude) {
@@ -862,6 +873,13 @@ func refineLinuxBlockDeviceIdentity(smart *DiskSMART, target smartctlTarget) {
smart.Device = block
smart.Controller, smart.Target = linuxBlockDeviceTopology(block)
ensureControllerCollectionStatus(smart, "sysfs")
+ // smartctl labels SAS members with the generic SCSI protocol when the
+ // transport descriptor is absent; sysfs knows the real link type.
+ if smart.Type == "" || smart.Type == "scsi" {
+ if evidence := linuxBlockDeviceTransportEvidence(block); evidence != "" {
+ smart.Type = evidence
+ }
+ }
if size := blockDeviceSizeBytes(block); size > 0 {
smart.SizeBytes = size
}
@@ -921,7 +939,9 @@ func isSCSITargetAddress(value string) bool {
return true
}
-func linuxBlockDeviceTransport(block string, target smartctlTarget) string {
+// linuxBlockDeviceTransportEvidence returns the transport type only when
+// sysfs states it explicitly, and empty when the kernel supplies no evidence.
+func linuxBlockDeviceTransportEvidence(block string) string {
for _, candidate := range []string{
readTrimmedFile(filepath.Join("/sys/block", block, "device", "protocol")),
readTrimmedFile(filepath.Join("/sys/block", block, "device", "transport")),
@@ -937,6 +957,13 @@ func linuxBlockDeviceTransport(block string, target smartctlTarget) string {
return "usb"
}
}
+ return ""
+}
+
+func linuxBlockDeviceTransport(block string, target smartctlTarget) string {
+ if evidence := linuxBlockDeviceTransportEvidence(block); evidence != "" {
+ return evidence
+ }
if strings.HasPrefix(block, "nvme") {
return "nvme"
}
@@ -1616,7 +1643,7 @@ func applySMARTTextFallback(result *DiskSMART, fallback smartTextFallback) {
if result.Serial == "" && fallback.Serial != "" {
result.Serial = fallback.Serial
}
- if result.Type == "" && fallback.Type != "" {
+ if (result.Type == "" || result.Type == "scsi") && fallback.Type != "" {
result.Type = fallback.Type
}
if result.Health == "" && fallback.Health != "" {
@@ -1731,6 +1758,24 @@ func parseSMARTAttributes(data *smartctlJSON, diskType string) *SMARTAttributes
us := nvmeLog.UnsafeShutdowns
attrs.UnsafeShutdowns = &us
}
+ } else if diskType == "sas" || diskType == "scsi" {
+ // SCSI drives report no ATA attribute table; their counters live in
+ // dedicated log pages smartctl surfaces as top-level JSON fields.
+ if data.PowerOnTime != nil {
+ hasData = true
+ poh := data.PowerOnTime.Hours
+ attrs.PowerOnHours = &poh
+ }
+ if data.SCSIGrownDefectList != nil {
+ hasData = true
+ defects := *data.SCSIGrownDefectList
+ attrs.ReallocatedSectors = &defects
+ }
+ if data.SCSIPercentageUsedEnduranceIndicator != nil {
+ hasData = true
+ used := *data.SCSIPercentageUsedEnduranceIndicator
+ attrs.PercentageUsed = &used
+ }
} else {
for _, attr := range data.ATASmartAttributes.Table {
hasData = true
@@ -1789,21 +1834,40 @@ func parseRawValue(rawString string, rawValue int64) int64 {
}
// detectDiskType determines the disk transport type from smartctl output.
+// SAS drives report device protocol "SCSI"; their SAS transport is only
+// visible in the SCSI transport descriptor. An empty return means smartctl
+// gave no transport evidence, so the text-output and sysfs refinements decide
+// before the legacy sata default applies.
func detectDiskType(data smartctlJSON) string {
protocol := strings.ToLower(data.Device.Protocol)
+ transport := strings.ToLower(data.SCSITransportProtocol.Name)
switch {
case strings.Contains(protocol, "nvme"):
return "nvme"
case strings.Contains(protocol, "sas"):
return "sas"
- case strings.Contains(protocol, "ata"), strings.Contains(protocol, "sata"):
+ case strings.Contains(protocol, "scsi"):
+ if strings.Contains(transport, "sas") {
+ return "sas"
+ }
+ return "scsi"
+ case strings.Contains(protocol, "ata"):
return "sata"
default:
devType := strings.ToLower(data.Device.Type)
- if strings.Contains(devType, "nvme") {
+ switch {
+ case strings.Contains(devType, "nvme"):
return "nvme"
+ case strings.Contains(devType, "scsi"):
+ if strings.Contains(transport, "sas") {
+ return "sas"
+ }
+ return "scsi"
+ case strings.HasPrefix(devType, "sat"):
+ return "sata"
+ default:
+ return ""
}
- return "sata"
}
}
diff --git a/internal/hostagent/smartctl_test.go b/internal/hostagent/smartctl_test.go
index 65bea3da72..19e4d869aa 100644
--- a/internal/hostagent/smartctl_test.go
+++ b/internal/hostagent/smartctl_test.go
@@ -67,7 +67,26 @@ func TestDetectDiskType(t *testing.T) {
expected: "nvme",
},
{
- name: "Empty defaults to sata",
+ name: "SCSI protocol with SAS transport descriptor",
+ data: func() smartctlJSON {
+ var data smartctlJSON
+ data.Device.Protocol = "SCSI"
+ data.SCSITransportProtocol.Name = "SAS (SPL-4)"
+ return data
+ }(),
+ expected: "sas",
+ },
+ {
+ name: "SCSI protocol without transport descriptor stays scsi",
+ data: func() smartctlJSON {
+ var data smartctlJSON
+ data.Device.Protocol = "SCSI"
+ return data
+ }(),
+ expected: "scsi",
+ },
+ {
+ name: "Empty leaves type undecided for refinement",
data: smartctlJSON{
Device: struct {
Name string `json:"name"`
@@ -75,7 +94,7 @@ func TestDetectDiskType(t *testing.T) {
Protocol string `json:"protocol"`
}{},
},
- expected: "sata",
+ expected: "",
},
}
@@ -480,6 +499,71 @@ func TestParseSMARTOutputFallsBackToOriginalTextTemperature(t *testing.T) {
}
}
+func TestParseSMARTOutputHandlesSASDrive(t *testing.T) {
+ payload := []byte(`{
+ "device": {"name": "/dev/sda", "type": "scsi", "protocol": "SCSI"},
+ "scsi_transport_protocol": {"name": "SAS (SPL-4)"},
+ "model_name": "SEAGATE ST18000NM019J",
+ "serial_number": "ZR5DQ4N30000W317UKPL",
+ "user_capacity": {"bytes": 18000207937536},
+ "smart_status": {"passed": true},
+ "temperature": {"current": 34},
+ "power_on_time": {"hours": 16951},
+ "scsi_grown_defect_list": 0
+ }`)
+
+ result, err := parseSMARTOutput(payload, smartctlTarget{Path: "/dev/sda"})
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if result == nil {
+ t.Fatal("expected result")
+ }
+ if result.Type != "sas" {
+ t.Fatalf("expected type sas, got %#v", result)
+ }
+ if result.Health != "PASSED" {
+ t.Fatalf("expected health PASSED, got %#v", result)
+ }
+ if result.Serial != "ZR5DQ4N30000W317UKPL" {
+ t.Fatalf("expected smartctl serial, got %#v", result)
+ }
+ if result.Temperature != 34 {
+ t.Fatalf("expected temperature 34, got %#v", result)
+ }
+ if result.Attributes == nil {
+ t.Fatalf("expected SCSI attributes, got %#v", result)
+ }
+ if result.Attributes.PowerOnHours == nil || *result.Attributes.PowerOnHours != 16951 {
+ t.Fatalf("expected power-on hours 16951, got %#v", result.Attributes)
+ }
+ if result.Attributes.ReallocatedSectors == nil || *result.Attributes.ReallocatedSectors != 0 {
+ t.Fatalf("expected grown defect count 0, got %#v", result.Attributes)
+ }
+}
+
+func TestParseSMARTOutputUsesTextTransportForSCSIProtocol(t *testing.T) {
+ payload := []byte(`{
+ "device": {"name": "/dev/sda", "type": "scsi", "protocol": "SCSI"},
+ "model_name": "SEAGATE ST18000NM019J",
+ "serial_number": "ZR5DQ4N30000W317UKPL",
+ "smart_status": {"passed": true},
+ "temperature": {"current": 34},
+ "smartctl": {"output": ["Transport protocol: SAS (SPL-4)", "SMART Health Status: OK"]}
+ }`)
+
+ result, err := parseSMARTOutput(payload, smartctlTarget{Path: "/dev/sda"})
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if result == nil {
+ t.Fatal("expected result")
+ }
+ if result.Type != "sas" {
+ t.Fatalf("expected text transport to refine scsi to sas, got %#v", result)
+ }
+}
+
func TestParseSMARTOutputRejectsOutOfRangeATARawTemperature(t *testing.T) {
payload := []byte(`{
"device": {"protocol": "ATA"},
diff --git a/internal/monitoring/monitor.go b/internal/monitoring/monitor.go
index bb1d9536d1..425b3e8141 100644
--- a/internal/monitoring/monitor.go
+++ b/internal/monitoring/monitor.go
@@ -685,6 +685,12 @@ func shouldPromoteHostAgentDiskType(existing, incoming string) bool {
if existing == "" || existing == "unknown" || existing == "scsi" {
return true
}
+ // Proxmox reports coarse form-factor types (hdd/ssd) or misreads SAS
+ // members as sata; the agent's smartctl transport evidence is
+ // authoritative for the link type.
+ if incoming == "sas" && (existing == "sata" || existing == "hdd" || existing == "ssd") {
+ return true
+ }
return false
}