Lightweight server monitoring with historical data, docker stats, and alerts.
fix: handle cached NVMe failures and empty SMART responses
spatiumstas13bdd05934a7
diff --git a/agent/smart.go b/agent/smart.goindex 3490e7fb0..4dc1497f4 100644--- a/agent/smart.go+++ b/agent/smart.go@@ -513,8 +513,8 @@ func (sm *SmartManager) CollectSmart(deviceInfo *DeviceInfo) error {cmd := exec.CommandContext(ctx, sm.smartctlPath, args...)output, err := cmd.CombinedOutput()- // Check if device is in standby (exit status 2)- if exitErr, ok := errors.AsType[*exec.ExitError](err); ok && exitErr.ExitCode() == 2 {+ // NVMe has no standby check; exit status 2 may mean the controller is inaccessible.+ if exitErr, ok := errors.AsType[*exec.ExitError](err); ok && exitErr.ExitCode() == 2 && !isNvmeControllerPath(deviceInfo.Name) {if hasExistingData {// Device is in standby and we have cached data, keep using cachereturn nildiff --git a/agent/smart_test.go b/agent/smart_test.goindex 8b3071c8e..61860a725 100644--- a/agent/smart_test.go+++ b/agent/smart_test.go@@ -7,6 +7,7 @@ import ("fmt""os""path/filepath"+ "strings""testing""github.com/henrygd/beszel/internal/entities/smart"@@ -1483,3 +1484,44 @@ func TestLookupDarwinNvmeCapacityProviderError(t *testing.T) {// Cache should be initialized even on error so we don't retry (Once already fired)assert.NotNil(t, sm.darwinNvmeCapacity)}++// A child test process acts as smartctl: the controller is inaccessible,+// while its namespace returns valid NVMe data.+func init() {+ fixture := os.Getenv("BESZEL_SMART_TEST_FIXTURE")+ if fixture == "" {+ return+ }+ if isNvmeControllerPath(os.Args[len(os.Args)-1]) {+ fmt.Print(`{"smartctl":{"exit_status":2,"messages":[{"string":"Permission denied","severity":"error"}]}}`)+ os.Exit(2)+ }+ data, err := os.ReadFile(fixture)+ if err != nil {+ fmt.Fprintln(os.Stderr, err)+ os.Exit(1)+ }+ fmt.Print(strings.ReplaceAll(string(data), "/dev/nvme0", os.Args[len(os.Args)-1]))+ os.Exit(0)+}++func TestCollectSmartNvmeNamespaceFallback(t *testing.T) {+ fixture, err := filepath.Abs(filepath.Join("test-data", "smart", "nvme0.json"))+ require.NoError(t, err)+ t.Setenv("BESZEL_SMART_TEST_FIXTURE", fixture)+ executable, err := os.Executable()+ require.NoError(t, err)+ for _, cached := range []bool{false, true} {+ t.Run(fmt.Sprintf("cached=%t", cached), func(t *testing.T) {+ device := &DeviceInfo{Name: "/dev/nvme0", Type: "nvme"}+ sm := &SmartManager{smartctlPath: executable, SmartDataMap: make(map[string]*smart.SmartData)}+ if cached {+ sm.SmartDataMap["2024031600129"] = &smart.SmartData{DiskName: device.Name, DiskType: device.Type}+ }+ require.NoError(t, sm.CollectSmart(device))+ assert.Equal(t, "/dev/nvme0n1", device.Name)+ assert.Equal(t, "/dev/nvme0n1", sm.SmartDataMap["2024031600129"].DiskName)+ assert.True(t, sm.isExcludedDevice("/dev/nvme0"))+ })+ }+}diff --git a/internal/common/common-ws.go b/internal/common/common-ws.goindex d6584ccb9..91ea5490c 100644--- a/internal/common/common-ws.go+++ b/internal/common/common-ws.go@@ -47,7 +47,7 @@ type AgentResponse struct {Fingerprint *FingerprintResponse `cbor:"2,keyasint,omitempty,omitzero"` // Legacy (<= 0.17)Error string `cbor:"3,keyasint,omitempty,omitzero"`String *string `cbor:"4,keyasint,omitempty,omitzero"` // Legacy (<= 0.17)- SmartData map[string]smart.SmartData `cbor:"5,keyasint,omitempty,omitzero"` // Legacy (<= 0.17)+ SmartData map[string]smart.SmartData `cbor:"5,keyasint,omitzero"` // Legacy (<= 0.17)ServiceInfo systemd.ServiceDetails `cbor:"6,keyasint,omitempty,omitzero"` // Legacy (<= 0.17)// Data is the generic response payload for new endpoints (0.18+)Data cbor.RawMessage `cbor:"7,keyasint,omitempty,omitzero"`diff --git a/internal/hub/transport/transport_test.go b/internal/hub/transport/transport_test.goindex 12328fc8f..c4536b7fc 100644--- a/internal/hub/transport/transport_test.go+++ b/internal/hub/transport/transport_test.go@@ -3,6 +3,7 @@ package transportimport ("testing"+ "github.com/fxamacker/cbor/v2""github.com/henrygd/beszel/internal/common""github.com/henrygd/beszel/internal/entities/smart""github.com/stretchr/testify/assert"@@ -40,3 +41,15 @@ func TestUnmarshalSystemdLogsResponse(t *testing.T) {require.NoError(t, UnmarshalResponse(response, common.GetSystemdLogs, &result))assert.Equal(t, logs, result)}++func TestUnmarshalEmptySmartDataResponse(t *testing.T) {+ payload, err := cbor.Marshal(common.AgentResponse{SmartData: map[string]smart.SmartData{}})+ require.NoError(t, err)+ var response common.AgentResponse+ require.NoError(t, cbor.Unmarshal(payload, &response))++ var result smart.SmartDataResponse+ require.NoError(t, UnmarshalResponse(response, common.GetSmartData, &result))+ assert.NotNil(t, result.Data)+ assert.Empty(t, result.Data)+}