blob: 67a9d2b448673ad283698f044b7df50f906489c5 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// 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"
Abhay Kumara2ae5992025-11-10 14:02:24 +000018 "os"
khenaidooab1f7bd2019-11-14 14:00:27 -050019 "regexp"
20 "strconv"
21 "strings"
22)
23
24var (
Abhay Kumara2ae5992025-11-10 14:02:24 +000025 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+\]`)
khenaidooab1f7bd2019-11-14 14:00:27 -050031)
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
khenaidood948f772021-08-11 17:49:24 -040041 // Total number of disks the device requires.
khenaidooab1f7bd2019-11-14 14:00:27 -050042 DisksTotal int64
khenaidood948f772021-08-11 17:49:24 -040043 // Number of failed disks.
44 DisksFailed int64
Abhay Kumara2ae5992025-11-10 14:02:24 +000045 // Number of "down" disks. (the _ indicator in the status line)
46 DisksDown int64
khenaidood948f772021-08-11 17:49:24 -040047 // Spare disks in the device.
48 DisksSpare int64
khenaidooab1f7bd2019-11-14 14:00:27 -050049 // Number of blocks the device holds.
50 BlocksTotal int64
51 // Number of blocks on the device that are in sync.
52 BlocksSynced int64
Abhay Kumara2ae5992025-11-10 14:02:24 +000053 // 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
khenaidood948f772021-08-11 17:49:24 -040061 // Name of md component devices
62 Devices []string
khenaidooab1f7bd2019-11-14 14:00:27 -050063}
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) {
Abhay Kumara2ae5992025-11-10 14:02:24 +000069 data, err := os.ReadFile(fs.proc.Path("mdstat"))
khenaidooab1f7bd2019-11-14 14:00:27 -050070 if err != nil {
khenaidood948f772021-08-11 17:49:24 -040071 return nil, err
khenaidooab1f7bd2019-11-14 14:00:27 -050072 }
73 mdstat, err := parseMDStat(data)
74 if err != nil {
Abhay Kumara2ae5992025-11-10 14:02:24 +000075 return nil, fmt.Errorf("%w: Cannot parse %v: %w", ErrFileParse, fs.proc.Path("mdstat"), err)
khenaidooab1f7bd2019-11-14 14:00:27 -050076 }
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.
khenaidood948f772021-08-11 17:49:24 -040082func parseMDStat(mdStatData []byte) ([]MDStat, error) {
khenaidooab1f7bd2019-11-14 14:00:27 -050083 mdStats := []MDStat{}
khenaidood948f772021-08-11 17:49:24 -040084 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") {
khenaidooab1f7bd2019-11-14 14:00:27 -050090 continue
91 }
92
khenaidood948f772021-08-11 17:49:24 -040093 deviceFields := strings.Fields(line)
khenaidooab1f7bd2019-11-14 14:00:27 -050094 if len(deviceFields) < 3 {
Abhay Kumara2ae5992025-11-10 14:02:24 +000095 return nil, fmt.Errorf("%w: Expected 3+ lines, got %q", ErrFileParse, line)
khenaidooab1f7bd2019-11-14 14:00:27 -050096 }
khenaidood948f772021-08-11 17:49:24 -040097 mdName := deviceFields[0] // mdx
98 state := deviceFields[2] // active or inactive
khenaidooab1f7bd2019-11-14 14:00:27 -050099
100 if len(lines) <= i+3 {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000101 return nil, fmt.Errorf("%w: Too few lines for md device: %q", ErrFileParse, mdName)
khenaidooab1f7bd2019-11-14 14:00:27 -0500102 }
103
khenaidood948f772021-08-11 17:49:24 -0400104 // 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)"))
Abhay Kumara2ae5992025-11-10 14:02:24 +0000107 active, total, down, size, err := evalStatusLine(lines[i], lines[i+1])
khenaidood948f772021-08-11 17:49:24 -0400108
khenaidooab1f7bd2019-11-14 14:00:27 -0500109 if err != nil {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000110 return nil, fmt.Errorf("%w: Cannot parse md device lines: %v: %w", ErrFileParse, active, err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500111 }
112
113 syncLineIdx := i + 2
114 if strings.Contains(lines[i+2], "bitmap") { // skip bitmap line
115 syncLineIdx++
116 }
117
khenaidood948f772021-08-11 17:49:24 -0400118 // If device is syncing at the moment, get the number of currently
khenaidooab1f7bd2019-11-14 14:00:27 -0500119 // synced bytes, otherwise that number equals the size of the device.
Abhay Kumara2ae5992025-11-10 14:02:24 +0000120 blocksSynced := size
121 blocksToBeSynced := size
122 speed := float64(0)
123 finish := float64(0)
124 pct := float64(0)
khenaidood948f772021-08-11 17:49:24 -0400125 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") {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000142 blocksSynced = 0
khenaidood948f772021-08-11 17:49:24 -0400143 } else {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000144 blocksSynced, blocksToBeSynced, pct, finish, speed, err = evalRecoveryLine(lines[syncLineIdx])
khenaidood948f772021-08-11 17:49:24 -0400145 if err != nil {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000146 return nil, fmt.Errorf("%w: Cannot parse sync line in md device: %q: %w", ErrFileParse, mdName, err)
khenaidood948f772021-08-11 17:49:24 -0400147 }
khenaidooab1f7bd2019-11-14 14:00:27 -0500148 }
149 }
150
151 mdStats = append(mdStats, MDStat{
Abhay Kumara2ae5992025-11-10 14:02:24 +0000152 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),
khenaidooab1f7bd2019-11-14 14:00:27 -0500166 })
167 }
168
169 return mdStats, nil
170}
171
Abhay Kumara2ae5992025-11-10 14:02:24 +0000172func 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 }
khenaidood948f772021-08-11 17:49:24 -0400177
Abhay Kumara2ae5992025-11-10 14:02:24 +0000178 sizeStr := statusFields[0]
khenaidood948f772021-08-11 17:49:24 -0400179 size, err = strconv.ParseInt(sizeStr, 10, 64)
180 if err != nil {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000181 return 0, 0, 0, 0, fmt.Errorf("%w: Unexpected statusline %q: %w", ErrFileParse, statusLine, err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500182 }
183
khenaidood948f772021-08-11 17:49:24 -0400184 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, "["))
Abhay Kumara2ae5992025-11-10 14:02:24 +0000187 return total, total, 0, size, nil
khenaidood948f772021-08-11 17:49:24 -0400188 }
189
190 if strings.Contains(deviceLine, "inactive") {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000191 return 0, 0, 0, size, nil
khenaidood948f772021-08-11 17:49:24 -0400192 }
193
194 matches := statusLineRE.FindStringSubmatch(statusLine)
Abhay Kumara2ae5992025-11-10 14:02:24 +0000195 if len(matches) != 5 {
196 return 0, 0, 0, 0, fmt.Errorf("%w: Could not fild all substring matches %s: %w", ErrFileParse, statusLine, err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500197 }
198
199 total, err = strconv.ParseInt(matches[2], 10, 64)
200 if err != nil {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000201 return 0, 0, 0, 0, fmt.Errorf("%w: Unexpected statusline %q: %w", ErrFileParse, statusLine, err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500202 }
203
204 active, err = strconv.ParseInt(matches[3], 10, 64)
205 if err != nil {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000206 return 0, 0, 0, 0, fmt.Errorf("%w: Unexpected active %d: %w", ErrFileParse, active, err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500207 }
Abhay Kumara2ae5992025-11-10 14:02:24 +0000208 down = int64(strings.Count(matches[4], "_"))
khenaidooab1f7bd2019-11-14 14:00:27 -0500209
Abhay Kumara2ae5992025-11-10 14:02:24 +0000210 return active, total, down, size, nil
khenaidooab1f7bd2019-11-14 14:00:27 -0500211}
212
Abhay Kumara2ae5992025-11-10 14:02:24 +0000213func evalRecoveryLine(recoveryLine string) (blocksSynced int64, blocksToBeSynced int64, pct float64, finish float64, speed float64, err error) {
214 matches := recoveryLineBlocksRE.FindStringSubmatch(recoveryLine)
khenaidooab1f7bd2019-11-14 14:00:27 -0500215 if len(matches) != 2 {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000216 return 0, 0, 0, 0, 0, fmt.Errorf("%w: Unexpected recoveryLine blocks %s: %w", ErrFileParse, recoveryLine, err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500217 }
218
Abhay Kumara2ae5992025-11-10 14:02:24 +0000219 blocks := strings.Split(matches[1], "/")
220 blocksSynced, err = strconv.ParseInt(blocks[0], 10, 64)
khenaidooab1f7bd2019-11-14 14:00:27 -0500221 if err != nil {
Abhay Kumara2ae5992025-11-10 14:02:24 +0000222 return 0, 0, 0, 0, 0, fmt.Errorf("%w: Unable to parse recovery blocks synced %q: %w", ErrFileParse, matches[1], err)
khenaidooab1f7bd2019-11-14 14:00:27 -0500223 }
224
Abhay Kumara2ae5992025-11-10 14:02:24 +0000225 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
khenaidooab1f7bd2019-11-14 14:00:27 -0500261}
khenaidood948f772021-08-11 17:49:24 -0400262
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}