mdstat.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // Copyright 2018 The Prometheus Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. package procfs
  14. import (
  15. "fmt"
  16. "os"
  17. "regexp"
  18. "strconv"
  19. "strings"
  20. )
  21. var (
  22. statusLineRE = regexp.MustCompile(`(\d+) blocks .*\[(\d+)/(\d+)\] \[([U_]+)\]`)
  23. recoveryLineBlocksRE = regexp.MustCompile(`\((\d+/\d+)\)`)
  24. recoveryLinePctRE = regexp.MustCompile(`= (.+)%`)
  25. recoveryLineFinishRE = regexp.MustCompile(`finish=(.+)min`)
  26. recoveryLineSpeedRE = regexp.MustCompile(`speed=(.+)[A-Z]`)
  27. componentDeviceRE = regexp.MustCompile(`(.*)\[\d+\]`)
  28. )
  29. // MDStat holds info parsed from /proc/mdstat.
  30. type MDStat struct {
  31. // Name of the device.
  32. Name string
  33. // activity-state of the device.
  34. ActivityState string
  35. // Number of active disks.
  36. DisksActive int64
  37. // Total number of disks the device requires.
  38. DisksTotal int64
  39. // Number of failed disks.
  40. DisksFailed int64
  41. // Number of "down" disks. (the _ indicator in the status line)
  42. DisksDown int64
  43. // Spare disks in the device.
  44. DisksSpare int64
  45. // Number of blocks the device holds.
  46. BlocksTotal int64
  47. // Number of blocks on the device that are in sync.
  48. BlocksSynced int64
  49. // Number of blocks on the device that need to be synced.
  50. BlocksToBeSynced int64
  51. // progress percentage of current sync
  52. BlocksSyncedPct float64
  53. // estimated finishing time for current sync (in minutes)
  54. BlocksSyncedFinishTime float64
  55. // current sync speed (in Kilobytes/sec)
  56. BlocksSyncedSpeed float64
  57. // Name of md component devices
  58. Devices []string
  59. }
  60. // MDStat parses an mdstat-file (/proc/mdstat) and returns a slice of
  61. // structs containing the relevant info. More information available here:
  62. // https://raid.wiki.kernel.org/index.php/Mdstat
  63. func (fs FS) MDStat() ([]MDStat, error) {
  64. data, err := os.ReadFile(fs.proc.Path("mdstat"))
  65. if err != nil {
  66. return nil, err
  67. }
  68. mdstat, err := parseMDStat(data)
  69. if err != nil {
  70. return nil, fmt.Errorf("%w: Cannot parse %v: %w", ErrFileParse, fs.proc.Path("mdstat"), err)
  71. }
  72. return mdstat, nil
  73. }
  74. // parseMDStat parses data from mdstat file (/proc/mdstat) and returns a slice of
  75. // structs containing the relevant info.
  76. func parseMDStat(mdStatData []byte) ([]MDStat, error) {
  77. mdStats := []MDStat{}
  78. lines := strings.Split(string(mdStatData), "\n")
  79. for i, line := range lines {
  80. if strings.TrimSpace(line) == "" || line[0] == ' ' ||
  81. strings.HasPrefix(line, "Personalities") ||
  82. strings.HasPrefix(line, "unused") {
  83. continue
  84. }
  85. deviceFields := strings.Fields(line)
  86. if len(deviceFields) < 3 {
  87. return nil, fmt.Errorf("%w: Expected 3+ lines, got %q", ErrFileParse, line)
  88. }
  89. mdName := deviceFields[0] // mdx
  90. state := deviceFields[2] // active or inactive
  91. if len(lines) <= i+3 {
  92. return nil, fmt.Errorf("%w: Too few lines for md device: %q", ErrFileParse, mdName)
  93. }
  94. // Failed disks have the suffix (F) & Spare disks have the suffix (S).
  95. fail := int64(strings.Count(line, "(F)"))
  96. spare := int64(strings.Count(line, "(S)"))
  97. active, total, down, size, err := evalStatusLine(lines[i], lines[i+1])
  98. if err != nil {
  99. return nil, fmt.Errorf("%w: Cannot parse md device lines: %v: %w", ErrFileParse, active, err)
  100. }
  101. syncLineIdx := i + 2
  102. if strings.Contains(lines[i+2], "bitmap") { // skip bitmap line
  103. syncLineIdx++
  104. }
  105. // If device is syncing at the moment, get the number of currently
  106. // synced bytes, otherwise that number equals the size of the device.
  107. blocksSynced := size
  108. blocksToBeSynced := size
  109. speed := float64(0)
  110. finish := float64(0)
  111. pct := float64(0)
  112. recovering := strings.Contains(lines[syncLineIdx], "recovery")
  113. resyncing := strings.Contains(lines[syncLineIdx], "resync")
  114. checking := strings.Contains(lines[syncLineIdx], "check")
  115. // Append recovery and resyncing state info.
  116. if recovering || resyncing || checking {
  117. if recovering {
  118. state = "recovering"
  119. } else if checking {
  120. state = "checking"
  121. } else {
  122. state = "resyncing"
  123. }
  124. // Handle case when resync=PENDING or resync=DELAYED.
  125. if strings.Contains(lines[syncLineIdx], "PENDING") ||
  126. strings.Contains(lines[syncLineIdx], "DELAYED") {
  127. blocksSynced = 0
  128. } else {
  129. blocksSynced, blocksToBeSynced, pct, finish, speed, err = evalRecoveryLine(lines[syncLineIdx])
  130. if err != nil {
  131. return nil, fmt.Errorf("%w: Cannot parse sync line in md device: %q: %w", ErrFileParse, mdName, err)
  132. }
  133. }
  134. }
  135. mdStats = append(mdStats, MDStat{
  136. Name: mdName,
  137. ActivityState: state,
  138. DisksActive: active,
  139. DisksFailed: fail,
  140. DisksDown: down,
  141. DisksSpare: spare,
  142. DisksTotal: total,
  143. BlocksTotal: size,
  144. BlocksSynced: blocksSynced,
  145. BlocksToBeSynced: blocksToBeSynced,
  146. BlocksSyncedPct: pct,
  147. BlocksSyncedFinishTime: finish,
  148. BlocksSyncedSpeed: speed,
  149. Devices: evalComponentDevices(deviceFields),
  150. })
  151. }
  152. return mdStats, nil
  153. }
  154. func evalStatusLine(deviceLine, statusLine string) (active, total, down, size int64, err error) {
  155. statusFields := strings.Fields(statusLine)
  156. if len(statusFields) < 1 {
  157. return 0, 0, 0, 0, fmt.Errorf("%w: Unexpected statusline %q: %w", ErrFileParse, statusLine, err)
  158. }
  159. sizeStr := statusFields[0]
  160. size, err = strconv.ParseInt(sizeStr, 10, 64)
  161. if err != nil {
  162. return 0, 0, 0, 0, fmt.Errorf("%w: Unexpected statusline %q: %w", ErrFileParse, statusLine, err)
  163. }
  164. if strings.Contains(deviceLine, "raid0") || strings.Contains(deviceLine, "linear") {
  165. // In the device deviceLine, only disks have a number associated with them in [].
  166. total = int64(strings.Count(deviceLine, "["))
  167. return total, total, 0, size, nil
  168. }
  169. if strings.Contains(deviceLine, "inactive") {
  170. return 0, 0, 0, size, nil
  171. }
  172. matches := statusLineRE.FindStringSubmatch(statusLine)
  173. if len(matches) != 5 {
  174. return 0, 0, 0, 0, fmt.Errorf("%w: Could not fild all substring matches %s: %w", ErrFileParse, statusLine, err)
  175. }
  176. total, err = strconv.ParseInt(matches[2], 10, 64)
  177. if err != nil {
  178. return 0, 0, 0, 0, fmt.Errorf("%w: Unexpected statusline %q: %w", ErrFileParse, statusLine, err)
  179. }
  180. active, err = strconv.ParseInt(matches[3], 10, 64)
  181. if err != nil {
  182. return 0, 0, 0, 0, fmt.Errorf("%w: Unexpected active %d: %w", ErrFileParse, active, err)
  183. }
  184. down = int64(strings.Count(matches[4], "_"))
  185. return active, total, down, size, nil
  186. }
  187. func evalRecoveryLine(recoveryLine string) (blocksSynced int64, blocksToBeSynced int64, pct float64, finish float64, speed float64, err error) {
  188. matches := recoveryLineBlocksRE.FindStringSubmatch(recoveryLine)
  189. if len(matches) != 2 {
  190. return 0, 0, 0, 0, 0, fmt.Errorf("%w: Unexpected recoveryLine blocks %s: %w", ErrFileParse, recoveryLine, err)
  191. }
  192. blocks := strings.Split(matches[1], "/")
  193. blocksSynced, err = strconv.ParseInt(blocks[0], 10, 64)
  194. if err != nil {
  195. return 0, 0, 0, 0, 0, fmt.Errorf("%w: Unable to parse recovery blocks synced %q: %w", ErrFileParse, matches[1], err)
  196. }
  197. blocksToBeSynced, err = strconv.ParseInt(blocks[1], 10, 64)
  198. if err != nil {
  199. return blocksSynced, 0, 0, 0, 0, fmt.Errorf("%w: Unable to parse recovery to be synced blocks %q: %w", ErrFileParse, matches[2], err)
  200. }
  201. // Get percentage complete
  202. matches = recoveryLinePctRE.FindStringSubmatch(recoveryLine)
  203. if len(matches) != 2 {
  204. return blocksSynced, blocksToBeSynced, 0, 0, 0, fmt.Errorf("%w: Unexpected recoveryLine matching percentage %s", ErrFileParse, recoveryLine)
  205. }
  206. pct, err = strconv.ParseFloat(strings.TrimSpace(matches[1]), 64)
  207. if err != nil {
  208. return blocksSynced, blocksToBeSynced, 0, 0, 0, fmt.Errorf("%w: Error parsing float from recoveryLine %q", ErrFileParse, recoveryLine)
  209. }
  210. // Get time expected left to complete
  211. matches = recoveryLineFinishRE.FindStringSubmatch(recoveryLine)
  212. if len(matches) != 2 {
  213. return blocksSynced, blocksToBeSynced, pct, 0, 0, fmt.Errorf("%w: Unexpected recoveryLine matching est. finish time: %s", ErrFileParse, recoveryLine)
  214. }
  215. finish, err = strconv.ParseFloat(matches[1], 64)
  216. if err != nil {
  217. return blocksSynced, blocksToBeSynced, pct, 0, 0, fmt.Errorf("%w: Unable to parse float from recoveryLine: %q", ErrFileParse, recoveryLine)
  218. }
  219. // Get recovery speed
  220. matches = recoveryLineSpeedRE.FindStringSubmatch(recoveryLine)
  221. if len(matches) != 2 {
  222. return blocksSynced, blocksToBeSynced, pct, finish, 0, fmt.Errorf("%w: Unexpected recoveryLine value: %s", ErrFileParse, recoveryLine)
  223. }
  224. speed, err = strconv.ParseFloat(matches[1], 64)
  225. if err != nil {
  226. return blocksSynced, blocksToBeSynced, pct, finish, 0, fmt.Errorf("%w: Error parsing float from recoveryLine: %q: %w", ErrFileParse, recoveryLine, err)
  227. }
  228. return blocksSynced, blocksToBeSynced, pct, finish, speed, nil
  229. }
  230. func evalComponentDevices(deviceFields []string) []string {
  231. mdComponentDevices := make([]string, 0)
  232. if len(deviceFields) > 3 {
  233. for _, field := range deviceFields[4:] {
  234. match := componentDeviceRE.FindStringSubmatch(field)
  235. if match == nil {
  236. continue
  237. }
  238. mdComponentDevices = append(mdComponentDevices, match[1])
  239. }
  240. }
  241. return mdComponentDevices
  242. }