You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

267 line
8.5 KiB

  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. // progress percentage of current sync
  50. BlocksSyncedPct float64
  51. // estimated finishing time for current sync (in minutes)
  52. BlocksSyncedFinishTime float64
  53. // current sync speed (in Kilobytes/sec)
  54. BlocksSyncedSpeed float64
  55. // Name of md component devices
  56. Devices []string
  57. }
  58. // MDStat parses an mdstat-file (/proc/mdstat) and returns a slice of
  59. // structs containing the relevant info. More information available here:
  60. // https://raid.wiki.kernel.org/index.php/Mdstat
  61. func (fs FS) MDStat() ([]MDStat, error) {
  62. data, err := os.ReadFile(fs.proc.Path("mdstat"))
  63. if err != nil {
  64. return nil, err
  65. }
  66. mdstat, err := parseMDStat(data)
  67. if err != nil {
  68. return nil, fmt.Errorf("error parsing mdstat %q: %w", fs.proc.Path("mdstat"), err)
  69. }
  70. return mdstat, nil
  71. }
  72. // parseMDStat parses data from mdstat file (/proc/mdstat) and returns a slice of
  73. // structs containing the relevant info.
  74. func parseMDStat(mdStatData []byte) ([]MDStat, error) {
  75. mdStats := []MDStat{}
  76. lines := strings.Split(string(mdStatData), "\n")
  77. for i, line := range lines {
  78. if strings.TrimSpace(line) == "" || line[0] == ' ' ||
  79. strings.HasPrefix(line, "Personalities") ||
  80. strings.HasPrefix(line, "unused") {
  81. continue
  82. }
  83. deviceFields := strings.Fields(line)
  84. if len(deviceFields) < 3 {
  85. return nil, fmt.Errorf("not enough fields in mdline (expected at least 3): %s", line)
  86. }
  87. mdName := deviceFields[0] // mdx
  88. state := deviceFields[2] // active or inactive
  89. if len(lines) <= i+3 {
  90. return nil, fmt.Errorf("error parsing %q: too few lines for md device", mdName)
  91. }
  92. // Failed disks have the suffix (F) & Spare disks have the suffix (S).
  93. fail := int64(strings.Count(line, "(F)"))
  94. spare := int64(strings.Count(line, "(S)"))
  95. active, total, down, size, err := evalStatusLine(lines[i], lines[i+1])
  96. if err != nil {
  97. return nil, fmt.Errorf("error parsing md device lines: %w", err)
  98. }
  99. syncLineIdx := i + 2
  100. if strings.Contains(lines[i+2], "bitmap") { // skip bitmap line
  101. syncLineIdx++
  102. }
  103. // If device is syncing at the moment, get the number of currently
  104. // synced bytes, otherwise that number equals the size of the device.
  105. syncedBlocks := size
  106. speed := float64(0)
  107. finish := float64(0)
  108. pct := float64(0)
  109. recovering := strings.Contains(lines[syncLineIdx], "recovery")
  110. resyncing := strings.Contains(lines[syncLineIdx], "resync")
  111. checking := strings.Contains(lines[syncLineIdx], "check")
  112. // Append recovery and resyncing state info.
  113. if recovering || resyncing || checking {
  114. if recovering {
  115. state = "recovering"
  116. } else if checking {
  117. state = "checking"
  118. } else {
  119. state = "resyncing"
  120. }
  121. // Handle case when resync=PENDING or resync=DELAYED.
  122. if strings.Contains(lines[syncLineIdx], "PENDING") ||
  123. strings.Contains(lines[syncLineIdx], "DELAYED") {
  124. syncedBlocks = 0
  125. } else {
  126. syncedBlocks, pct, finish, speed, err = evalRecoveryLine(lines[syncLineIdx])
  127. if err != nil {
  128. return nil, fmt.Errorf("error parsing sync line in md device %q: %w", mdName, err)
  129. }
  130. }
  131. }
  132. mdStats = append(mdStats, MDStat{
  133. Name: mdName,
  134. ActivityState: state,
  135. DisksActive: active,
  136. DisksFailed: fail,
  137. DisksDown: down,
  138. DisksSpare: spare,
  139. DisksTotal: total,
  140. BlocksTotal: size,
  141. BlocksSynced: syncedBlocks,
  142. BlocksSyncedPct: pct,
  143. BlocksSyncedFinishTime: finish,
  144. BlocksSyncedSpeed: speed,
  145. Devices: evalComponentDevices(deviceFields),
  146. })
  147. }
  148. return mdStats, nil
  149. }
  150. func evalStatusLine(deviceLine, statusLine string) (active, total, down, size int64, err error) {
  151. statusFields := strings.Fields(statusLine)
  152. if len(statusFields) < 1 {
  153. return 0, 0, 0, 0, fmt.Errorf("unexpected statusLine %q", statusLine)
  154. }
  155. sizeStr := statusFields[0]
  156. size, err = strconv.ParseInt(sizeStr, 10, 64)
  157. if err != nil {
  158. return 0, 0, 0, 0, fmt.Errorf("unexpected statusLine %q: %w", statusLine, err)
  159. }
  160. if strings.Contains(deviceLine, "raid0") || strings.Contains(deviceLine, "linear") {
  161. // In the device deviceLine, only disks have a number associated with them in [].
  162. total = int64(strings.Count(deviceLine, "["))
  163. return total, total, 0, size, nil
  164. }
  165. if strings.Contains(deviceLine, "inactive") {
  166. return 0, 0, 0, size, nil
  167. }
  168. matches := statusLineRE.FindStringSubmatch(statusLine)
  169. if len(matches) != 5 {
  170. return 0, 0, 0, 0, fmt.Errorf("couldn't find all the substring matches: %s", statusLine)
  171. }
  172. total, err = strconv.ParseInt(matches[2], 10, 64)
  173. if err != nil {
  174. return 0, 0, 0, 0, fmt.Errorf("unexpected statusLine %q: %w", statusLine, err)
  175. }
  176. active, err = strconv.ParseInt(matches[3], 10, 64)
  177. if err != nil {
  178. return 0, 0, 0, 0, fmt.Errorf("unexpected statusLine %q: %w", statusLine, err)
  179. }
  180. down = int64(strings.Count(matches[4], "_"))
  181. return active, total, down, size, nil
  182. }
  183. func evalRecoveryLine(recoveryLine string) (syncedBlocks int64, pct float64, finish float64, speed float64, err error) {
  184. matches := recoveryLineBlocksRE.FindStringSubmatch(recoveryLine)
  185. if len(matches) != 2 {
  186. return 0, 0, 0, 0, fmt.Errorf("unexpected recoveryLine: %s", recoveryLine)
  187. }
  188. syncedBlocks, err = strconv.ParseInt(matches[1], 10, 64)
  189. if err != nil {
  190. return 0, 0, 0, 0, fmt.Errorf("error parsing int from recoveryLine %q: %w", recoveryLine, err)
  191. }
  192. // Get percentage complete
  193. matches = recoveryLinePctRE.FindStringSubmatch(recoveryLine)
  194. if len(matches) != 2 {
  195. return syncedBlocks, 0, 0, 0, fmt.Errorf("unexpected recoveryLine matching percentage: %s", recoveryLine)
  196. }
  197. pct, err = strconv.ParseFloat(strings.TrimSpace(matches[1]), 64)
  198. if err != nil {
  199. return syncedBlocks, 0, 0, 0, fmt.Errorf("error parsing float from recoveryLine %q: %w", recoveryLine, err)
  200. }
  201. // Get time expected left to complete
  202. matches = recoveryLineFinishRE.FindStringSubmatch(recoveryLine)
  203. if len(matches) != 2 {
  204. return syncedBlocks, pct, 0, 0, fmt.Errorf("unexpected recoveryLine matching est. finish time: %s", recoveryLine)
  205. }
  206. finish, err = strconv.ParseFloat(matches[1], 64)
  207. if err != nil {
  208. return syncedBlocks, pct, 0, 0, fmt.Errorf("error parsing float from recoveryLine %q: %w", recoveryLine, err)
  209. }
  210. // Get recovery speed
  211. matches = recoveryLineSpeedRE.FindStringSubmatch(recoveryLine)
  212. if len(matches) != 2 {
  213. return syncedBlocks, pct, finish, 0, fmt.Errorf("unexpected recoveryLine matching speed: %s", recoveryLine)
  214. }
  215. speed, err = strconv.ParseFloat(matches[1], 64)
  216. if err != nil {
  217. return syncedBlocks, pct, finish, 0, fmt.Errorf("error parsing float from recoveryLine %q: %w", recoveryLine, err)
  218. }
  219. return syncedBlocks, pct, finish, speed, nil
  220. }
  221. func evalComponentDevices(deviceFields []string) []string {
  222. mdComponentDevices := make([]string, 0)
  223. if len(deviceFields) > 3 {
  224. for _, field := range deviceFields[4:] {
  225. match := componentDeviceRE.FindStringSubmatch(field)
  226. if match == nil {
  227. continue
  228. }
  229. mdComponentDevices = append(mdComponentDevices, match[1])
  230. }
  231. }
  232. return mdComponentDevices
  233. }