mirror of
https://github.com/henrygd/beszel.git
synced 2025-12-17 02:36:17 +01:00
handle when power on smart attribute is a string like 0h+0m+0.000s
This commit is contained in:
@@ -453,7 +453,7 @@ func (sm *SmartManager) parseSmartForSata(output []byte) (bool, int) {
|
|||||||
Value: attr.Value,
|
Value: attr.Value,
|
||||||
Worst: attr.Worst,
|
Worst: attr.Worst,
|
||||||
Threshold: attr.Thresh,
|
Threshold: attr.Thresh,
|
||||||
RawValue: attr.Raw.Value,
|
RawValue: uint64(attr.Raw.Value),
|
||||||
RawString: attr.Raw.String,
|
RawString: attr.Raw.String,
|
||||||
WhenFailed: attr.WhenFailed,
|
WhenFailed: attr.WhenFailed,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
package smart
|
package smart
|
||||||
|
|
||||||
|
import (
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// Common types
|
// Common types
|
||||||
type VersionInfo [2]int
|
type VersionInfo [2]int
|
||||||
|
|
||||||
@@ -129,30 +134,97 @@ type AtaSmartAttributes struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type AtaSmartAttribute struct {
|
type AtaSmartAttribute struct {
|
||||||
ID uint16 `json:"id"`
|
ID uint16 `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Value uint16 `json:"value"`
|
Value uint16 `json:"value"`
|
||||||
Worst uint16 `json:"worst"`
|
Worst uint16 `json:"worst"`
|
||||||
Thresh uint16 `json:"thresh"`
|
Thresh uint16 `json:"thresh"`
|
||||||
WhenFailed string `json:"when_failed"`
|
WhenFailed string `json:"when_failed"`
|
||||||
Flags AttributeFlags `json:"flags"`
|
// Flags AttributeFlags `json:"flags"`
|
||||||
Raw RawValue `json:"raw"`
|
Raw RawValue `json:"raw"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type AttributeFlags struct {
|
// type AttributeFlags struct {
|
||||||
Value int `json:"value"`
|
// Value int `json:"value"`
|
||||||
String string `json:"string"`
|
// String string `json:"string"`
|
||||||
Prefailure bool `json:"prefailure"`
|
// Prefailure bool `json:"prefailure"`
|
||||||
UpdatedOnline bool `json:"updated_online"`
|
// UpdatedOnline bool `json:"updated_online"`
|
||||||
Performance bool `json:"performance"`
|
// Performance bool `json:"performance"`
|
||||||
ErrorRate bool `json:"error_rate"`
|
// ErrorRate bool `json:"error_rate"`
|
||||||
EventCount bool `json:"event_count"`
|
// EventCount bool `json:"event_count"`
|
||||||
AutoKeep bool `json:"auto_keep"`
|
// AutoKeep bool `json:"auto_keep"`
|
||||||
}
|
// }
|
||||||
|
|
||||||
type RawValue struct {
|
type RawValue struct {
|
||||||
Value uint64 `json:"value"`
|
Value SmartRawValue `json:"value"`
|
||||||
String string `json:"string"`
|
String string `json:"string"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SmartRawValue uint64
|
||||||
|
|
||||||
|
// handles when drives report strings like "0h+0m+0.000s" or "7344 (253d 8h)" for power on hours
|
||||||
|
func (v *SmartRawValue) UnmarshalJSON(data []byte) error {
|
||||||
|
trimmed := strings.TrimSpace(string(data))
|
||||||
|
if len(trimmed) == 0 || trimmed == "null" {
|
||||||
|
*v = 0
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if trimmed[0] != '"' {
|
||||||
|
parsed, err := strconv.ParseUint(trimmed, 0, 64)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*v = SmartRawValue(parsed)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
valueStr, err := strconv.Unquote(trimmed)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if valueStr == "" {
|
||||||
|
*v = 0
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if parsed, err := strconv.ParseUint(valueStr, 0, 64); err == nil {
|
||||||
|
*v = SmartRawValue(parsed)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if idx := strings.IndexRune(valueStr, 'h'); idx >= 0 {
|
||||||
|
hoursPart := strings.TrimSpace(valueStr[:idx])
|
||||||
|
if hoursPart == "" {
|
||||||
|
*v = 0
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if parsed, err := strconv.ParseFloat(hoursPart, 64); err == nil {
|
||||||
|
*v = SmartRawValue(uint64(parsed))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if digits := leadingDigitPrefix(valueStr); digits != "" {
|
||||||
|
if parsed, err := strconv.ParseUint(digits, 0, 64); err == nil {
|
||||||
|
*v = SmartRawValue(parsed)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
*v = 0
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func leadingDigitPrefix(value string) string {
|
||||||
|
var builder strings.Builder
|
||||||
|
for _, r := range value {
|
||||||
|
if r < '0' || r > '9' {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
builder.WriteRune(r)
|
||||||
|
}
|
||||||
|
return builder.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// type PowerOnTimeInfo struct {
|
// type PowerOnTimeInfo struct {
|
||||||
|
|||||||
30
internal/entities/smart/smart_test.go
Normal file
30
internal/entities/smart/smart_test.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package smart
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSmartRawValueUnmarshalDuration(t *testing.T) {
|
||||||
|
input := []byte(`{"value":"62312h+33m+50.907s","string":"62312h+33m+50.907s"}`)
|
||||||
|
var raw RawValue
|
||||||
|
if err := json.Unmarshal(input, &raw); err != nil {
|
||||||
|
t.Fatalf("unexpected error unmarshalling raw value: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if uint64(raw.Value) != 62312 {
|
||||||
|
t.Fatalf("expected hours to be 62312, got %d", raw.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSmartRawValueUnmarshalNumericString(t *testing.T) {
|
||||||
|
input := []byte(`{"value":"7344","string":"7344"}`)
|
||||||
|
var raw RawValue
|
||||||
|
if err := json.Unmarshal(input, &raw); err != nil {
|
||||||
|
t.Fatalf("unexpected error unmarshalling numeric string: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if uint64(raw.Value) != 7344 {
|
||||||
|
t.Fatalf("expected hours to be 7344, got %d", raw.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user