smart.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package smart
  2. import (
  3. "errors"
  4. "fmt"
  5. "os"
  6. "path/filepath"
  7. "strings"
  8. "github.com/anatol/smart.go"
  9. )
  10. // SMART data structure for SATA disks
  11. type SATAAttrData struct {
  12. Id uint8
  13. Name string
  14. Type int
  15. RawVal uint64
  16. Current uint8
  17. Worst uint8
  18. }
  19. type SMARTData struct {
  20. ModelNumber string
  21. SerialNumber string
  22. Size uint64
  23. Temperature int
  24. /* NVME specific fields */
  25. NameSpaceUtilizations []uint64
  26. PowerOnHours uint64
  27. PowerCycles uint64
  28. UnsafeShutdowns uint64
  29. MediaErrors uint64
  30. /* SATA specific fields */
  31. SATAAttrs []*SATAAttrData
  32. }
  33. // Get SMART data of a particular disk / device
  34. func GetSMARTData(disk string) (*SMARTData, error) {
  35. if !strings.HasPrefix(disk, "/dev/") {
  36. disk = filepath.Join("/dev", disk)
  37. }
  38. //Check if the disk exists
  39. if _, err := os.Stat(disk); os.IsNotExist(err) {
  40. return nil, fmt.Errorf("disk %s does not exist", disk)
  41. }
  42. // Check if the disk is NVMe or SATA
  43. isNVMe := strings.HasPrefix(disk, "/dev/nvme")
  44. isSATA := strings.HasPrefix(disk, "/dev/sd")
  45. // If the disk is not NVMe or SATA, return an error
  46. if !isNVMe && !isSATA {
  47. return nil, fmt.Errorf("disk %s is not an NVMe or SATA disk", disk)
  48. }
  49. if isNVMe {
  50. return getNVMESMART(disk)
  51. } else if isSATA {
  52. return getSATASMART(disk)
  53. }
  54. return nil, errors.New("unsupported disk type")
  55. }
  56. // Check if the disk is a SATA device
  57. func IsSATADevice(disk string) bool {
  58. if !strings.HasPrefix(disk, "/dev/") {
  59. disk = filepath.Join("/dev", disk)
  60. }
  61. _, err := smart.OpenSata(disk)
  62. return err == nil
  63. }
  64. // Check if the disk is a NVMe device
  65. func IsNVMeDevice(disk string) bool {
  66. if !strings.HasPrefix(disk, "/dev/") {
  67. disk = filepath.Join("/dev", disk)
  68. }
  69. _, err := smart.OpenNVMe(disk)
  70. return err == nil
  71. }
  72. func getSATASMART(diskpath string) (*SMARTData, error) {
  73. dev, err := smart.OpenSata(diskpath)
  74. if err != nil {
  75. return nil, err
  76. }
  77. defer dev.Close()
  78. c, err := dev.Identify()
  79. if err != nil {
  80. return nil, err
  81. }
  82. sm, err := dev.ReadSMARTData()
  83. if err != nil {
  84. return nil, err
  85. }
  86. _, capacity, _, _, _ := c.Capacity()
  87. sataAttrs := []*SATAAttrData{}
  88. for _, attr := range sm.Attrs {
  89. thisAttrID := attr.Id
  90. thisAttrName := attr.Name
  91. thisAttrType := attr.Type
  92. thisAttrRawVal := attr.ValueRaw
  93. val, low, high, _, err := attr.ParseAsTemperature()
  94. if err != nil {
  95. continue
  96. }
  97. fmt.Println("Temperature: ", val, low, high)
  98. thisAttrData := SATAAttrData{
  99. Id: thisAttrID,
  100. Name: thisAttrName,
  101. Type: thisAttrType,
  102. RawVal: thisAttrRawVal,
  103. Current: attr.Current,
  104. Worst: attr.Worst,
  105. }
  106. sataAttrs = append(sataAttrs, &thisAttrData)
  107. }
  108. smartData := SMARTData{
  109. ModelNumber: c.ModelNumber(),
  110. SerialNumber: c.SerialNumber(),
  111. Size: capacity,
  112. SATAAttrs: sataAttrs,
  113. }
  114. return &smartData, nil
  115. }
  116. func getNVMESMART(diskpath string) (*SMARTData, error) {
  117. dev, err := smart.OpenNVMe(diskpath)
  118. if err != nil {
  119. return nil, err
  120. }
  121. defer dev.Close()
  122. c, nss, err := dev.Identify()
  123. if err != nil {
  124. return nil, err
  125. }
  126. NameSpaceUtilizations := []uint64{}
  127. for _, ns := range nss {
  128. NameSpaceUtilizations = append(NameSpaceUtilizations, ns.Nuse*ns.LbaSize())
  129. }
  130. sm, _ := dev.ReadSMART()
  131. smartData := SMARTData{
  132. ModelNumber: c.ModelNumber(),
  133. SerialNumber: c.SerialNumber(),
  134. Size: c.Tnvmcap.Val[0],
  135. NameSpaceUtilizations: NameSpaceUtilizations,
  136. Temperature: int(sm.Temperature),
  137. PowerOnHours: sm.PowerOnHours.Val[0],
  138. PowerCycles: sm.PowerCycles.Val[0],
  139. UnsafeShutdowns: sm.UnsafeShutdowns.Val[0],
  140. MediaErrors: sm.MediaErrors.Val[0],
  141. }
  142. return &smartData, nil
  143. }