smart.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package main
  2. import (
  3. "encoding/json"
  4. "log"
  5. "net/http"
  6. "strings"
  7. "imuslab.com/bokofs/bokofsd/mod/diskinfo"
  8. "imuslab.com/bokofs/bokofsd/mod/diskinfo/smart"
  9. "imuslab.com/bokofs/bokofsd/mod/utils"
  10. )
  11. /*
  12. smart.go
  13. This file handles the SMART management and monitoring API routing
  14. Support APIs
  15. /smart/health/{diskname} - Get the health status of a disk
  16. /smart/health/all - Get the health status of all disks
  17. /smart/info/{diskname} - Get the SMART information of a disk
  18. */
  19. // Handler for SMART API calls
  20. func HandleSMARTCalls() http.Handler {
  21. return http.StripPrefix("/smart/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  22. pathParts := strings.Split(r.URL.Path, "/")
  23. if len(pathParts) < 2 {
  24. http.Error(w, "Bad Request - Missing disk name", http.StatusBadRequest)
  25. return
  26. }
  27. subPath := pathParts[0]
  28. diskName := pathParts[1]
  29. if diskName == "" {
  30. http.Error(w, "Bad Request - Missing disk name", http.StatusBadRequest)
  31. return
  32. }
  33. switch subPath {
  34. case "health":
  35. if diskName == "all" {
  36. // Get the SMART information for all disks
  37. allDisks, err := diskinfo.GetAllDisks()
  38. if err != nil {
  39. log.Println("Error getting all disks:", err)
  40. http.Error(w, "Internal Server Error", http.StatusInternalServerError)
  41. return
  42. }
  43. // Create a map to hold the SMART information for each disk
  44. diskInfoMap := []*smart.DriveHealthInfo{}
  45. for _, disk := range allDisks {
  46. diskName := disk.Name
  47. health, err := smart.GetDiskSMARTHealthSummary(diskName)
  48. if err != nil {
  49. log.Println("Error getting disk health:", err)
  50. continue
  51. }
  52. diskInfoMap = append(diskInfoMap, health)
  53. }
  54. // Convert the disk information to JSON and write it to the response
  55. js, _ := json.Marshal(diskInfoMap)
  56. utils.SendJSONResponse(w, string(js))
  57. return
  58. }
  59. // Get the health status of the disk
  60. health, err := smart.GetDiskSMARTHealthSummary(diskName)
  61. if err != nil {
  62. log.Println("Error getting disk health:", err)
  63. http.Error(w, "Internal Server Error", http.StatusInternalServerError)
  64. return
  65. }
  66. // Convert the health status to JSON and write it to the response
  67. js, _ := json.Marshal(health)
  68. utils.SendJSONResponse(w, string(js))
  69. return
  70. case "info":
  71. // Handle SMART API calls
  72. dt, err := smart.GetDiskType(diskName)
  73. if err != nil {
  74. log.Println("Error getting disk type:", err)
  75. http.Error(w, "Internal Server Error", http.StatusInternalServerError)
  76. return
  77. }
  78. if dt == smart.DiskType_SATA {
  79. // Get SATA disk information
  80. sataInfo, err := smart.GetSATAInfo(diskName)
  81. if err != nil {
  82. log.Println("Error getting SATA disk info:", err)
  83. http.Error(w, "Internal Server Error", http.StatusInternalServerError)
  84. return
  85. }
  86. // Convert the SATA info to JSON and write it to the response
  87. js, _ := json.Marshal(sataInfo)
  88. utils.SendJSONResponse(w, string(js))
  89. } else if dt == smart.DiskType_NVMe {
  90. // Get NVMe disk information
  91. nvmeInfo, err := smart.GetNVMEInfo(diskName)
  92. if err != nil {
  93. log.Println("Error getting NVMe disk info:", err)
  94. http.Error(w, "Internal Server Error", http.StatusInternalServerError)
  95. return
  96. }
  97. // Convert the NVMe info to JSON and write it to the response
  98. js, _ := json.Marshal(nvmeInfo)
  99. utils.SendJSONResponse(w, string(js))
  100. } else {
  101. log.Println("Unknown disk type:", dt)
  102. http.Error(w, "Bad Request - Unknown disk type", http.StatusBadRequest)
  103. return
  104. }
  105. return
  106. default:
  107. http.Error(w, "Not Found", http.StatusNotFound)
  108. return
  109. }
  110. }))
  111. }