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