Copyright 2018 The Prometheus Authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

package procfs

import (
	
	
	
	
	
)

var (
	statusLineRE   = regexp.MustCompile(`(\d+) blocks .*\[(\d+)/(\d+)\] \[[U_]+\]`)
	recoveryLineRE = regexp.MustCompile(`\((\d+)/\d+\)`)
)
MDStat holds info parsed from /proc/mdstat.
Name of the device.
activity-state of the device.
Number of active disks.
Total number of disks the device requires.
Number of failed disks.
Spare disks in the device.
Number of blocks the device holds.
Number of blocks on the device that are in sync.
MDStat parses an mdstat-file (/proc/mdstat) and returns a slice of structs containing the relevant info. More information available here: https://raid.wiki.kernel.org/index.php/Mdstat
func ( FS) () ([]MDStat, error) {
	,  := ioutil.ReadFile(.proc.Path("mdstat"))
	if  != nil {
		return nil, fmt.Errorf("error parsing mdstat %s: %s", .proc.Path("mdstat"), )
	}
	,  := parseMDStat()
	if  != nil {
		return nil, fmt.Errorf("error parsing mdstat %s: %s", .proc.Path("mdstat"), )
	}
	return , nil
}
parseMDStat parses data from mdstat file (/proc/mdstat) and returns a slice of structs containing the relevant info.
func ( []byte) ([]MDStat, error) {
	 := []MDStat{}
	 := strings.Split(string(), "\n")

	for ,  := range  {
		if strings.TrimSpace() == "" || [0] == ' ' ||
			strings.HasPrefix(, "Personalities") ||
			strings.HasPrefix(, "unused") {
			continue
		}

		 := strings.Fields()
		if len() < 3 {
			return nil, fmt.Errorf("not enough fields in mdline (expected at least 3): %s", )
		}
		 := [0] // mdx
		 := [2]  // active or inactive

		if len() <= +3 {
			return nil, fmt.Errorf(
				"error parsing %s: too few lines for md device",
				,
			)
		}
Failed disks have the suffix (F) & Spare disks have the suffix (S).
		 := int64(strings.Count(, "(F)"))
		 := int64(strings.Count(, "(S)"))
		, , ,  := evalStatusLine([], [+1])

		if  != nil {
			return nil, fmt.Errorf("error parsing md device lines: %s", )
		}

		 :=  + 2
		if strings.Contains([+2], "bitmap") { // skip bitmap line
			++
		}
If device is syncing at the moment, get the number of currently synced bytes, otherwise that number equals the size of the device.
		 := 
		 := strings.Contains([], "recovery")
		 := strings.Contains([], "resync")
Append recovery and resyncing state info.
		if  ||  {
			if  {
				 = "recovering"
			} else {
				 = "resyncing"
			}
Handle case when resync=PENDING or resync=DELAYED.
			if strings.Contains([], "PENDING") ||
				strings.Contains([], "DELAYED") {
				 = 0
			} else {
				,  = evalRecoveryLine([])
				if  != nil {
					return nil, fmt.Errorf("error parsing sync line in md device %s: %s", , )
				}
			}
		}

		 = append(, MDStat{
			Name:          ,
			ActivityState: ,
			DisksActive:   ,
			DisksFailed:   ,
			DisksSpare:    ,
			DisksTotal:    ,
			BlocksTotal:   ,
			BlocksSynced:  ,
		})
	}

	return , nil
}

func (,  string) (, ,  int64,  error) {

	 := strings.Fields()[0]
	,  = strconv.ParseInt(, 10, 64)
	if  != nil {
		return 0, 0, 0, fmt.Errorf("unexpected statusLine %s: %s", , )
	}

In the device deviceLine, only disks have a number associated with them in [].
		 = int64(strings.Count(, "["))
		return , , , nil
	}

	if strings.Contains(, "inactive") {
		return 0, 0, , nil
	}

	 := statusLineRE.FindStringSubmatch()
	if len() != 4 {
		return 0, 0, 0, fmt.Errorf("couldn't find all the substring matches: %s", )
	}

	,  = strconv.ParseInt([2], 10, 64)
	if  != nil {
		return 0, 0, 0, fmt.Errorf("unexpected statusLine %s: %s", , )
	}

	,  = strconv.ParseInt([3], 10, 64)
	if  != nil {
		return 0, 0, 0, fmt.Errorf("unexpected statusLine %s: %s", , )
	}

	return , , , nil
}

func ( string) ( int64,  error) {
	 := recoveryLineRE.FindStringSubmatch()
	if len() != 2 {
		return 0, fmt.Errorf("unexpected recoveryLine: %s", )
	}

	,  = strconv.ParseInt([1], 10, 64)
	if  != nil {
		return 0, fmt.Errorf("%s in recoveryLine: %s", , )
	}

	return , nil