mirror of
https://github.com/henrygd/beszel.git
synced 2026-03-21 21:26:16 +01:00
fix(smart): handle negative ATA device statistics values (#1791)
This commit is contained in:
@@ -223,7 +223,7 @@ func (sm *SmartManager) ScanDevices(force bool) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (sm *SmartManager) parseConfiguredDevices(config string) ([]*DeviceInfo, error) {
|
func (sm *SmartManager) parseConfiguredDevices(config string) ([]*DeviceInfo, error) {
|
||||||
splitChar := os.Getenv("SMART_DEVICES_SEPARATOR")
|
splitChar, _ := utils.GetEnv("SMART_DEVICES_SEPARATOR")
|
||||||
if splitChar == "" {
|
if splitChar == "" {
|
||||||
splitChar = ","
|
splitChar = ","
|
||||||
}
|
}
|
||||||
@@ -919,7 +919,7 @@ func temperatureFromAtaDeviceStatistics(stats smart.AtaDeviceStatistics) (uint8,
|
|||||||
if entry == nil || entry.Value == nil {
|
if entry == nil || entry.Value == nil {
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
if *entry.Value > 255 {
|
if *entry.Value < 0 || *entry.Value > 255 {
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
return uint8(*entry.Value), true
|
return uint8(*entry.Value), true
|
||||||
|
|||||||
@@ -121,6 +121,43 @@ func TestParseSmartForSataDeviceStatisticsTemperature(t *testing.T) {
|
|||||||
assert.Equal(t, uint8(22), deviceData.Temperature)
|
assert.Equal(t, uint8(22), deviceData.Temperature)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseSmartForSataNegativeDeviceStatistics(t *testing.T) {
|
||||||
|
// Tests that negative values in ata_device_statistics (e.g. min operating temp)
|
||||||
|
// do not cause the entire SAT parser to fail.
|
||||||
|
jsonPayload := []byte(`{
|
||||||
|
"smartctl": {"exit_status": 0},
|
||||||
|
"device": {"name": "/dev/sdb", "type": "sat"},
|
||||||
|
"model_name": "SanDisk SSD U110 16GB",
|
||||||
|
"serial_number": "NEGATIVE123",
|
||||||
|
"firmware_version": "U21B001",
|
||||||
|
"user_capacity": {"bytes": 16013942784},
|
||||||
|
"smart_status": {"passed": true},
|
||||||
|
"temperature": {"current": 38},
|
||||||
|
"ata_smart_attributes": {"table": []},
|
||||||
|
"ata_device_statistics": {
|
||||||
|
"pages": [
|
||||||
|
{
|
||||||
|
"number": 5,
|
||||||
|
"name": "Temperature Statistics",
|
||||||
|
"table": [
|
||||||
|
{"name": "Current Temperature", "value": 38, "flags": {"valid": true}},
|
||||||
|
{"name": "Specified Minimum Operating Temperature", "value": -20, "flags": {"valid": true}}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}`)
|
||||||
|
|
||||||
|
sm := &SmartManager{SmartDataMap: make(map[string]*smart.SmartData)}
|
||||||
|
hasData, exitStatus := sm.parseSmartForSata(jsonPayload)
|
||||||
|
require.True(t, hasData)
|
||||||
|
assert.Equal(t, 0, exitStatus)
|
||||||
|
|
||||||
|
deviceData, ok := sm.SmartDataMap["NEGATIVE123"]
|
||||||
|
require.True(t, ok, "expected smart data entry for serial NEGATIVE123")
|
||||||
|
assert.Equal(t, uint8(38), deviceData.Temperature)
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseSmartForSataParentheticalRawValue(t *testing.T) {
|
func TestParseSmartForSataParentheticalRawValue(t *testing.T) {
|
||||||
jsonPayload := []byte(`{
|
jsonPayload := []byte(`{
|
||||||
"smartctl": {"exit_status": 0},
|
"smartctl": {"exit_status": 0},
|
||||||
|
|||||||
@@ -143,8 +143,8 @@ type AtaDeviceStatisticsPage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type AtaDeviceStatisticsEntry struct {
|
type AtaDeviceStatisticsEntry struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Value *uint64 `json:"value,omitempty"`
|
Value *int64 `json:"value,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AtaSmartAttribute struct {
|
type AtaSmartAttribute struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user