|
@@ -66,6 +66,26 @@ func GetSMARTData(disk string) (*SMARTData, error) {
|
|
|
return nil, errors.New("unsupported disk type")
|
|
|
}
|
|
|
|
|
|
+// Check if the path is the disk path instead of partition path
|
|
|
+func IsRootDisk(deviceFilePath string) bool {
|
|
|
+ deviceFilename := filepath.Base(deviceFilePath)
|
|
|
+ if !strings.HasPrefix(deviceFilename, "sd") || strings.HasPrefix(deviceFilename, "nvme") {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ if strings.HasPrefix(deviceFilename, "sd") && len(deviceFilename) > 3 {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ if strings.HasPrefix(deviceFilename, "nvme") && len(deviceFilename) > 5 {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+// Check if this SMART implementation supports the disk type
|
|
|
+func IsDiskSupportedType(disk string) bool {
|
|
|
+ return IsNVMeDevice(disk) || IsSATADevice(disk)
|
|
|
+}
|
|
|
+
|
|
|
// Check if the disk is a SATA device
|
|
|
func IsSATADevice(disk string) bool {
|
|
|
if !strings.HasPrefix(disk, "/dev/") {
|
|
@@ -85,89 +105,3 @@ func IsNVMeDevice(disk string) bool {
|
|
|
_, err := smart.OpenNVMe(disk)
|
|
|
return err == nil
|
|
|
}
|
|
|
-
|
|
|
-func getSATASMART(diskpath string) (*SMARTData, error) {
|
|
|
- dev, err := smart.OpenSata(diskpath)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- defer dev.Close()
|
|
|
-
|
|
|
- c, err := dev.Identify()
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- sm, err := dev.ReadSMARTData()
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- _, capacity, _, _, _ := c.Capacity()
|
|
|
- sataAttrs := []*SATAAttrData{}
|
|
|
- for _, attr := range sm.Attrs {
|
|
|
- thisAttrID := attr.Id
|
|
|
- thisAttrName := attr.Name
|
|
|
- thisAttrType := attr.Type
|
|
|
- thisAttrRawVal := attr.ValueRaw
|
|
|
- val, low, high, _, err := attr.ParseAsTemperature()
|
|
|
- if err != nil {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- fmt.Println("Temperature: ", val, low, high)
|
|
|
- thisAttrData := SATAAttrData{
|
|
|
- Id: thisAttrID,
|
|
|
- Name: thisAttrName,
|
|
|
- Type: thisAttrType,
|
|
|
- RawVal: thisAttrRawVal,
|
|
|
- Current: attr.Current,
|
|
|
- Worst: attr.Worst,
|
|
|
- }
|
|
|
-
|
|
|
- sataAttrs = append(sataAttrs, &thisAttrData)
|
|
|
- }
|
|
|
-
|
|
|
- smartData := SMARTData{
|
|
|
- ModelNumber: c.ModelNumber(),
|
|
|
- SerialNumber: c.SerialNumber(),
|
|
|
- Size: capacity,
|
|
|
-
|
|
|
- SATAAttrs: sataAttrs,
|
|
|
- }
|
|
|
-
|
|
|
- return &smartData, nil
|
|
|
-}
|
|
|
-
|
|
|
-func getNVMESMART(diskpath string) (*SMARTData, error) {
|
|
|
- dev, err := smart.OpenNVMe(diskpath)
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
- defer dev.Close()
|
|
|
-
|
|
|
- c, nss, err := dev.Identify()
|
|
|
- if err != nil {
|
|
|
- return nil, err
|
|
|
- }
|
|
|
-
|
|
|
- NameSpaceUtilizations := []uint64{}
|
|
|
- for _, ns := range nss {
|
|
|
- NameSpaceUtilizations = append(NameSpaceUtilizations, ns.Nuse*ns.LbaSize())
|
|
|
- }
|
|
|
-
|
|
|
- sm, _ := dev.ReadSMART()
|
|
|
- smartData := SMARTData{
|
|
|
- ModelNumber: c.ModelNumber(),
|
|
|
- SerialNumber: c.SerialNumber(),
|
|
|
- Size: c.Tnvmcap.Val[0],
|
|
|
- NameSpaceUtilizations: NameSpaceUtilizations,
|
|
|
- Temperature: int(sm.Temperature),
|
|
|
- PowerOnHours: sm.PowerOnHours.Val[0],
|
|
|
- PowerCycles: sm.PowerCycles.Val[0],
|
|
|
- UnsafeShutdowns: sm.UnsafeShutdowns.Val[0],
|
|
|
- MediaErrors: sm.MediaErrors.Val[0],
|
|
|
- }
|
|
|
-
|
|
|
- return &smartData, nil
|
|
|
-}
|