mdstat.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. "io/ioutil"
  17. "regexp"
  18. "strconv"
  19. "strings"
  20. )
  21. var (
  22. statusLineRE = regexp.MustCompile(`(\d+) blocks .*\[(\d+)/(\d+)\] \[[U_]+\]`)
  23. recoveryLineRE = regexp.MustCompile(`\((\d+)/\d+\)`)
  24. )
  25. // MDStat holds info parsed from /proc/mdstat.
  26. type MDStat struct {
  27. // Name of the device.
  28. Name string
  29. // activity-state of the device.
  30. ActivityState string
  31. // Number of active disks.
  32. DisksActive int64
  33. // Total number of disks the device requires.
  34. DisksTotal int64
  35. // Number of failed disks.
  36. DisksFailed int64
  37. // Spare disks in the device.
  38. DisksSpare int64
  39. // Number of blocks the device holds.
  40. BlocksTotal int64
  41. // Number of blocks on the device that are in sync.
  42. BlocksSynced int64
  43. }
  44. // MDStat parses an mdstat-file (/proc/mdstat) and returns a slice of
  45. // structs containing the relevant info. More information available here:
  46. // https://raid.wiki.kernel.org/index.php/Mdstat
  47. func (fs FS) MDStat() ([]MDStat, error) {
  48. data, err := ioutil.ReadFile(fs.proc.Path("mdstat"))
  49. if err != nil {
  50. return nil, err
  51. }
  52. mdstat, err := parseMDStat(data)
  53. if err != nil {
  54. return nil, fmt.Errorf("error parsing mdstat %s: %s", fs.proc.Path("mdstat"), err)
  55. }
  56. return mdstat, nil
  57. }
  58. // parseMDStat parses data from mdstat file (/proc/mdstat) and returns a slice of
  59. // structs containing the relevant info.
  60. func parseMDStat(mdStatData []byte) ([]MDStat, error) {
  61. mdStats := []MDStat{}
  62. lines := strings.Split(string(mdStatData), "\n")
  63. for i, line := range lines {
  64. if strings.TrimSpace(line) == "" || line[0] == ' ' ||
  65. strings.HasPrefix(line, "Personalities") ||
  66. strings.HasPrefix(line, "unused") {
  67. continue
  68. }
  69. deviceFields := strings.Fields(line)
  70. if len(deviceFields) < 3 {
  71. return nil, fmt.Errorf("not enough fields in mdline (expected at least 3): %s", line)
  72. }
  73. mdName := deviceFields[0] // mdx
  74. state := deviceFields[2] // active or inactive
  75. if len(lines) <= i+3 {
  76. return nil, fmt.Errorf(
  77. "error parsing %s: too few lines for md device",
  78. mdName,
  79. )
  80. }
  81. // Failed disks have the suffix (F) & Spare disks have the suffix (S).
  82. fail := int64(strings.Count(line, "(F)"))
  83. spare := int64(strings.Count(line, "(S)"))
  84. active, total, size, err := evalStatusLine(lines[i], lines[i+1])
  85. if err != nil {
  86. return nil, fmt.Errorf("error parsing md device lines: %s", err)
  87. }
  88. syncLineIdx := i + 2
  89. if strings.Contains(lines[i+2], "bitmap") { // skip bitmap line
  90. syncLineIdx++
  91. }
  92. // If device is syncing at the moment, get the number of currently
  93. // synced bytes, otherwise that number equals the size of the device.
  94. syncedBlocks := size
  95. recovering := strings.Contains(lines[syncLineIdx], "recovery")
  96. resyncing := strings.Contains(lines[syncLineIdx], "resync")
  97. checking := strings.Contains(lines[syncLineIdx], "check")
  98. // Append recovery and resyncing state info.
  99. if recovering || resyncing || checking {
  100. if recovering {
  101. state = "recovering"
  102. } else if checking {
  103. state = "checking"
  104. } else {
  105. state = "resyncing"
  106. }
  107. // Handle case when resync=PENDING or resync=DELAYED.
  108. if strings.Contains(lines[syncLineIdx], "PENDING") ||
  109. strings.Contains(lines[syncLineIdx], "DELAYED") {
  110. syncedBlocks = 0
  111. } else {
  112. syncedBlocks, err = evalRecoveryLine(lines[syncLineIdx])
  113. if err != nil {
  114. return nil, fmt.Errorf("error parsing sync line in md device %s: %s", mdName, err)
  115. }
  116. }
  117. }
  118. mdStats = append(mdStats, MDStat{
  119. Name: mdName,
  120. ActivityState: state,
  121. DisksActive: active,
  122. DisksFailed: fail,
  123. DisksSpare: spare,
  124. DisksTotal: total,
  125. BlocksTotal: size,
  126. BlocksSynced: syncedBlocks,
  127. })
  128. }
  129. return mdStats, nil
  130. }
  131. func evalStatusLine(deviceLine, statusLine string) (active, total, size int64, err error) {
  132. sizeStr := strings.Fields(statusLine)[0]
  133. size, err = strconv.ParseInt(sizeStr, 10, 64)
  134. if err != nil {
  135. return 0, 0, 0, fmt.Errorf("unexpected statusLine %s: %s", statusLine, err)
  136. }
  137. if strings.Contains(deviceLine, "raid0") || strings.Contains(deviceLine, "linear") {
  138. // In the device deviceLine, only disks have a number associated with them in [].
  139. total = int64(strings.Count(deviceLine, "["))
  140. return total, total, size, nil
  141. }
  142. if strings.Contains(deviceLine, "inactive") {
  143. return 0, 0, size, nil
  144. }
  145. matches := statusLineRE.FindStringSubmatch(statusLine)
  146. if len(matches) != 4 {
  147. return 0, 0, 0, fmt.Errorf("couldn't find all the substring matches: %s", statusLine)
  148. }
  149. total, err = strconv.ParseInt(matches[2], 10, 64)
  150. if err != nil {
  151. return 0, 0, 0, fmt.Errorf("unexpected statusLine %s: %s", statusLine, err)
  152. }
  153. active, err = strconv.ParseInt(matches[3], 10, 64)
  154. if err != nil {
  155. return 0, 0, 0, fmt.Errorf("unexpected statusLine %s: %s", statusLine, err)
  156. }
  157. return active, total, size, nil
  158. }
  159. func evalRecoveryLine(recoveryLine string) (syncedBlocks int64, err error) {
  160. matches := recoveryLineRE.FindStringSubmatch(recoveryLine)
  161. if len(matches) != 2 {
  162. return 0, fmt.Errorf("unexpected recoveryLine: %s", recoveryLine)
  163. }
  164. syncedBlocks, err = strconv.ParseInt(matches[1], 10, 64)
  165. if err != nil {
  166. return 0, fmt.Errorf("%s in recoveryLine: %s", err, recoveryLine)
  167. }
  168. return syncedBlocks, nil
  169. }