...

Source file src/github.com/prometheus/procfs/sysfs/mdraid.go

Documentation: github.com/prometheus/procfs/sysfs

     1  // Copyright 2023 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  
    14  //go:build linux
    15  // +build linux
    16  
    17  package sysfs
    18  
    19  import (
    20  	"fmt"
    21  	"path/filepath"
    22  	"strings"
    23  
    24  	"github.com/prometheus/procfs/internal/util"
    25  )
    26  
    27  // Mdraid holds info parsed from relevant files in the /sys/block/md*/md directory.
    28  type Mdraid struct {
    29  	Device          string            // Kernel device name of array.
    30  	Level           string            // mdraid level.
    31  	ArrayState      string            // State of the array.
    32  	MetadataVersion string            // mdraid metadata version.
    33  	Disks           uint64            // Number of devices in a fully functional array.
    34  	Components      []MdraidComponent // mdraid component devices.
    35  	UUID            string            // UUID of the array.
    36  
    37  	// The following item is only valid for raid0, 4, 5, 6 and 10.
    38  	ChunkSize uint64 // Chunk size
    39  
    40  	// The following items are only valid for raid1, 4, 5, 6 and 10.
    41  	DegradedDisks uint64  // Number of degraded disks in the array.
    42  	SyncAction    string  // Current sync action.
    43  	SyncCompleted float64 // Fraction (0-1) representing the completion status of current sync operation.
    44  }
    45  
    46  type MdraidComponent struct {
    47  	Device string // Kernel device name.
    48  	State  string // Current state of device.
    49  }
    50  
    51  // Mdraids gathers information and statistics about mdraid devices present. Based on upstream
    52  // kernel documentation https://docs.kernel.org/admin-guide/md.html.
    53  func (fs FS) Mdraids() ([]Mdraid, error) {
    54  	matches, err := filepath.Glob(fs.sys.Path("block/md*/md"))
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	mdraids := make([]Mdraid, 0)
    60  
    61  	for _, m := range matches {
    62  		md := Mdraid{Device: filepath.Base(filepath.Dir(m))}
    63  		path := fs.sys.Path("block", md.Device, "md")
    64  
    65  		if val, err := util.SysReadFile(filepath.Join(path, "level")); err == nil {
    66  			md.Level = val
    67  		} else {
    68  			return mdraids, err
    69  		}
    70  
    71  		// Array state can be one of: clear, inactive, readonly, read-auto, clean, active,
    72  		// write-pending, active-idle.
    73  		if val, err := util.SysReadFile(filepath.Join(path, "array_state")); err == nil {
    74  			md.ArrayState = val
    75  		} else {
    76  			return mdraids, err
    77  		}
    78  
    79  		if val, err := util.SysReadFile(filepath.Join(path, "metadata_version")); err == nil {
    80  			md.MetadataVersion = val
    81  		} else {
    82  			return mdraids, err
    83  		}
    84  
    85  		if val, err := util.ReadUintFromFile(filepath.Join(path, "raid_disks")); err == nil {
    86  			md.Disks = val
    87  		} else {
    88  			return mdraids, err
    89  		}
    90  
    91  		if val, err := util.SysReadFile(filepath.Join(path, "uuid")); err == nil {
    92  			md.UUID = val
    93  		} else {
    94  			return mdraids, err
    95  		}
    96  
    97  		if devs, err := filepath.Glob(filepath.Join(path, "dev-*")); err == nil {
    98  			for _, dev := range devs {
    99  				comp := MdraidComponent{Device: strings.TrimPrefix(filepath.Base(dev), "dev-")}
   100  
   101  				// Component state can be a comma-separated list of: faulty, in_sync, writemostly,
   102  				// blocked, spare, write_error, want_replacement, replacement.
   103  				if val, err := util.SysReadFile(filepath.Join(dev, "state")); err == nil {
   104  					comp.State = val
   105  				} else {
   106  					return mdraids, err
   107  				}
   108  
   109  				md.Components = append(md.Components, comp)
   110  			}
   111  		} else {
   112  			return mdraids, err
   113  		}
   114  
   115  		switch md.Level {
   116  		case "raid0", "raid4", "raid5", "raid6", "raid10":
   117  			if val, err := util.ReadUintFromFile(filepath.Join(path, "chunk_size")); err == nil {
   118  				md.ChunkSize = val
   119  			} else {
   120  				return mdraids, err
   121  			}
   122  		}
   123  
   124  		switch md.Level {
   125  		case "raid1", "raid4", "raid5", "raid6", "raid10":
   126  			if val, err := util.ReadUintFromFile(filepath.Join(path, "degraded")); err == nil {
   127  				md.DegradedDisks = val
   128  			} else {
   129  				return mdraids, err
   130  			}
   131  
   132  			// Array sync action can be one of: resync, recover, idle, check, repair.
   133  			if val, err := util.SysReadFile(filepath.Join(path, "sync_action")); err == nil {
   134  				md.SyncAction = val
   135  			} else {
   136  				return mdraids, err
   137  			}
   138  
   139  			if val, err := util.SysReadFile(filepath.Join(path, "sync_completed")); err == nil {
   140  				if val != "none" {
   141  					var a, b uint64
   142  
   143  					// File contains two values representing the fraction of number of completed
   144  					// sectors divided by number of total sectors to process.
   145  					if _, err := fmt.Sscanf(val, "%d / %d", &a, &b); err == nil {
   146  						md.SyncCompleted = float64(a) / float64(b)
   147  					} else {
   148  						return mdraids, err
   149  					}
   150  				}
   151  			} else {
   152  				return mdraids, err
   153  			}
   154  		}
   155  
   156  		mdraids = append(mdraids, md)
   157  	}
   158  
   159  	return mdraids, nil
   160  }
   161  

View as plain text