...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30 type NVMeDevice struct {
31 Name string
32 Serial string
33 Model string
34 State string
35 FirmwareRevision string
36 }
37
38
39
40
41 type NVMeClass map[string]NVMeDevice
42
43
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
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