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
6 // http://www.apache.org/licenses/LICENSE-2.0
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.
25 statuslineRE = regexp.MustCompile(`(\d+) blocks .*\[(\d+)/(\d+)\] \[[U_]+\]`)
26 buildlineRE = regexp.MustCompile(`\((\d+)/\d+\)`)
29 // MDStat holds info parsed from /proc/mdstat.
31 // Name of the device.
33 // activity-state of the device.
35 // Number of active disks.
37 // Total number of disks the device consists of.
39 // Number of blocks the device holds.
41 // Number of blocks on the device that are in sync.
45 // ParseMDStat parses an mdstat-file and returns a struct with the relevant infos.
46 func (fs FS) ParseMDStat() (mdstates []MDStat, err error) {
47 mdStatusFilePath := fs.Path("mdstat")
48 content, err := ioutil.ReadFile(mdStatusFilePath)
50 return []MDStat{}, fmt.Errorf("error parsing %s: %s", mdStatusFilePath, err)
53 mdStates := []MDStat{}
54 lines := strings.Split(string(content), "\n")
55 for i, l := range lines {
62 if strings.HasPrefix(l, "Personalities") || strings.HasPrefix(l, "unused") {
66 mainLine := strings.Split(l, " ")
67 if len(mainLine) < 3 {
68 return mdStates, fmt.Errorf("error parsing mdline: %s", l)
71 activityState := mainLine[2]
73 if len(lines) <= i+3 {
74 return mdStates, fmt.Errorf(
75 "error parsing %s: too few lines for md device %s",
81 active, total, size, err := evalStatusline(lines[i+1])
83 return mdStates, fmt.Errorf("error parsing %s: %s", mdStatusFilePath, err)
86 // j is the line number of the syncing-line.
88 if strings.Contains(lines[i+2], "bitmap") { // skip bitmap line
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.
95 if strings.Contains(lines[j], "recovery") || strings.Contains(lines[j], "resync") {
96 syncedBlocks, err = evalBuildline(lines[j])
98 return mdStates, fmt.Errorf("error parsing %s: %s", mdStatusFilePath, err)
102 mdStates = append(mdStates, MDStat{
104 ActivityState: activityState,
108 BlocksSynced: syncedBlocks,
115 func evalStatusline(statusline string) (active, total, size int64, err error) {
116 matches := statuslineRE.FindStringSubmatch(statusline)
117 if len(matches) != 4 {
118 return 0, 0, 0, fmt.Errorf("unexpected statusline: %s", statusline)
121 size, err = strconv.ParseInt(matches[1], 10, 64)
123 return 0, 0, 0, fmt.Errorf("unexpected statusline %s: %s", statusline, err)
126 total, err = strconv.ParseInt(matches[2], 10, 64)
128 return 0, 0, 0, fmt.Errorf("unexpected statusline %s: %s", statusline, err)
131 active, err = strconv.ParseInt(matches[3], 10, 64)
133 return 0, 0, 0, fmt.Errorf("unexpected statusline %s: %s", statusline, err)
136 return active, total, size, nil
139 func evalBuildline(buildline string) (syncedBlocks int64, err error) {
140 matches := buildlineRE.FindStringSubmatch(buildline)
141 if len(matches) != 2 {
142 return 0, fmt.Errorf("unexpected buildline: %s", buildline)
145 syncedBlocks, err = strconv.ParseInt(matches[1], 10, 64)
147 return 0, fmt.Errorf("%s in buildline: %s", err, buildline)
150 return syncedBlocks, nil