...

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

Documentation: github.com/prometheus/procfs/sysfs

     1  // Copyright 2021 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  	"os"
    22  	"path/filepath"
    23  
    24  	"github.com/prometheus/procfs/internal/util"
    25  )
    26  
    27  const nvmeClassPath = "class/nvme"
    28  
    29  // NVMeDevice contains info from files in /sys/class/nvme for a single NVMe device.
    30  type NVMeDevice struct {
    31  	Name             string
    32  	Serial           string // /sys/class/nvme/<Name>/serial
    33  	Model            string // /sys/class/nvme/<Name>/model
    34  	State            string // /sys/class/nvme/<Name>/state
    35  	FirmwareRevision string // /sys/class/nvme/<Name>/firmware_rev
    36  }
    37  
    38  // NVMeClass is a collection of every NVMe device in /sys/class/nvme.
    39  //
    40  // The map keys are the names of the NVMe devices.
    41  type NVMeClass map[string]NVMeDevice
    42  
    43  // NVMeClass returns info for all NVMe devices read from /sys/class/nvme.
    44  func (fs FS) NVMeClass() (NVMeClass, error) {
    45  	path := fs.sys.Path(nvmeClassPath)
    46  
    47  	dirs, err := os.ReadDir(path)
    48  	if err != nil {
    49  		return nil, fmt.Errorf("failed to list NVMe devices at %q: %w", path, err)
    50  	}
    51  
    52  	nc := make(NVMeClass, len(dirs))
    53  	for _, d := range dirs {
    54  		device, err := fs.parseNVMeDevice(d.Name())
    55  		if err != nil {
    56  			return nil, err
    57  		}
    58  
    59  		nc[device.Name] = *device
    60  	}
    61  
    62  	return nc, nil
    63  }
    64  
    65  // Parse one NVMe device.
    66  func (fs FS) parseNVMeDevice(name string) (*NVMeDevice, error) {
    67  	path := fs.sys.Path(nvmeClassPath, name)
    68  	device := NVMeDevice{Name: name}
    69  
    70  	for _, f := range [...]string{"firmware_rev", "model", "serial", "state"} {
    71  		name := filepath.Join(path, f)
    72  		value, err := util.SysReadFile(name)
    73  		if err != nil {
    74  			return nil, fmt.Errorf("failed to read file %q: %w", name, err)
    75  		}
    76  
    77  		switch f {
    78  		case "firmware_rev":
    79  			device.FirmwareRevision = value
    80  		case "model":
    81  			device.Model = value
    82  		case "serial":
    83  			device.Serial = value
    84  		case "state":
    85  			device.State = value
    86  		}
    87  	}
    88  
    89  	return &device, nil
    90  }
    91  

View as plain text