本站源代码
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.

195 lines
5.6KB

  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, fmt.Errorf("error parsing mdstat %s: %s", fs.proc.Path("mdstat"), 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. // Append recovery and resyncing state info.
  98. if recovering || resyncing {
  99. if recovering {
  100. state = "recovering"
  101. } else {
  102. state = "resyncing"
  103. }
  104. // Handle case when resync=PENDING or resync=DELAYED.
  105. if strings.Contains(lines[syncLineIdx], "PENDING") ||
  106. strings.Contains(lines[syncLineIdx], "DELAYED") {
  107. syncedBlocks = 0
  108. } else {
  109. syncedBlocks, err = evalRecoveryLine(lines[syncLineIdx])
  110. if err != nil {
  111. return nil, fmt.Errorf("error parsing sync line in md device %s: %s", mdName, err)
  112. }
  113. }
  114. }
  115. mdStats = append(mdStats, MDStat{
  116. Name: mdName,
  117. ActivityState: state,
  118. DisksActive: active,
  119. DisksFailed: fail,
  120. DisksSpare: spare,
  121. DisksTotal: total,
  122. BlocksTotal: size,
  123. BlocksSynced: syncedBlocks,
  124. })
  125. }
  126. return mdStats, nil
  127. }
  128. func evalStatusLine(deviceLine, statusLine string) (active, total, size int64, err error) {
  129. sizeStr := strings.Fields(statusLine)[0]
  130. size, err = strconv.ParseInt(sizeStr, 10, 64)
  131. if err != nil {
  132. return 0, 0, 0, fmt.Errorf("unexpected statusLine %s: %s", statusLine, err)
  133. }
  134. if strings.Contains(deviceLine, "raid0") || strings.Contains(deviceLine, "linear") {
  135. // In the device deviceLine, only disks have a number associated with them in [].
  136. total = int64(strings.Count(deviceLine, "["))
  137. return total, total, size, nil
  138. }
  139. if strings.Contains(deviceLine, "inactive") {
  140. return 0, 0, size, nil
  141. }
  142. matches := statusLineRE.FindStringSubmatch(statusLine)
  143. if len(matches) != 4 {
  144. return 0, 0, 0, fmt.Errorf("couldn't find all the substring matches: %s", statusLine)
  145. }
  146. total, err = strconv.ParseInt(matches[2], 10, 64)
  147. if err != nil {
  148. return 0, 0, 0, fmt.Errorf("unexpected statusLine %s: %s", statusLine, err)
  149. }
  150. active, err = strconv.ParseInt(matches[3], 10, 64)
  151. if err != nil {
  152. return 0, 0, 0, fmt.Errorf("unexpected statusLine %s: %s", statusLine, err)
  153. }
  154. return active, total, size, nil
  155. }
  156. func evalRecoveryLine(recoveryLine string) (syncedBlocks int64, err error) {
  157. matches := recoveryLineRE.FindStringSubmatch(recoveryLine)
  158. if len(matches) != 2 {
  159. return 0, fmt.Errorf("unexpected recoveryLine: %s", recoveryLine)
  160. }
  161. syncedBlocks, err = strconv.ParseInt(matches[1], 10, 64)
  162. if err != nil {
  163. return 0, fmt.Errorf("%s in recoveryLine: %s", err, recoveryLine)
  164. }
  165. return syncedBlocks, nil
  166. }
上海开阖软件有限公司 沪ICP备12045867号-1