|
| 1 | +// Copyright 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 !nonvmesubsystem |
| 15 | + |
| 16 | +package collector |
| 17 | + |
| 18 | +import ( |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "log/slog" |
| 22 | + "os" |
| 23 | + "path/filepath" |
| 24 | + "regexp" |
| 25 | + "strings" |
| 26 | + |
| 27 | + "github.com/prometheus/client_golang/prometheus" |
| 28 | +) |
| 29 | + |
| 30 | +type nvmeSubsystemCollector struct { |
| 31 | + logger *slog.Logger |
| 32 | + scanSubsystems func() ([]nvmeSubsystem, error) |
| 33 | + |
| 34 | + subsystemInfo *prometheus.Desc |
| 35 | + subsystemPaths *prometheus.Desc |
| 36 | + subsystemPathsLive *prometheus.Desc |
| 37 | + pathState *prometheus.Desc |
| 38 | +} |
| 39 | + |
| 40 | +type nvmeSubsystem struct { |
| 41 | + Name string |
| 42 | + NQN string |
| 43 | + Model string |
| 44 | + Serial string |
| 45 | + IOPolicy string |
| 46 | + Controllers []nvmeController |
| 47 | +} |
| 48 | + |
| 49 | +type nvmeController struct { |
| 50 | + Name string |
| 51 | + State string |
| 52 | + Transport string |
| 53 | + Address string |
| 54 | +} |
| 55 | + |
| 56 | +var ( |
| 57 | + nvmeControllerRE = regexp.MustCompile(`^nvme\d+$`) |
| 58 | + |
| 59 | + nvmeControllerStates = []string{ |
| 60 | + "live", "connecting", "resetting", "dead", "unknown", |
| 61 | + } |
| 62 | +) |
| 63 | + |
| 64 | +func normalizeControllerState(raw string) string { |
| 65 | + switch raw { |
| 66 | + case "live", "connecting", "resetting", "dead": |
| 67 | + return raw |
| 68 | + case "deleting", "deleting (no IO)", "new": |
| 69 | + return raw |
| 70 | + default: |
| 71 | + return "unknown" |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func init() { |
| 76 | + registerCollector("nvmesubsystem", defaultDisabled, NewNVMeSubsystemCollector) |
| 77 | +} |
| 78 | + |
| 79 | +// NewNVMeSubsystemCollector returns a new Collector exposing NVMe-oF subsystem |
| 80 | +// path health from /sys/class/nvme-subsystem/. |
| 81 | +func NewNVMeSubsystemCollector(logger *slog.Logger) (Collector, error) { |
| 82 | + const subsystem = "nvmesubsystem" |
| 83 | + |
| 84 | + c := &nvmeSubsystemCollector{ |
| 85 | + logger: logger, |
| 86 | + subsystemInfo: prometheus.NewDesc( |
| 87 | + prometheus.BuildFQName(namespace, subsystem, "info"), |
| 88 | + "Non-numeric information about an NVMe subsystem.", |
| 89 | + []string{"subsystem", "nqn", "model", "serial", "iopolicy"}, nil, |
| 90 | + ), |
| 91 | + subsystemPaths: prometheus.NewDesc( |
| 92 | + prometheus.BuildFQName(namespace, subsystem, "paths"), |
| 93 | + "Number of controller paths for an NVMe subsystem.", |
| 94 | + []string{"subsystem"}, nil, |
| 95 | + ), |
| 96 | + subsystemPathsLive: prometheus.NewDesc( |
| 97 | + prometheus.BuildFQName(namespace, subsystem, "paths_live"), |
| 98 | + "Number of controller paths in live state for an NVMe subsystem.", |
| 99 | + []string{"subsystem"}, nil, |
| 100 | + ), |
| 101 | + pathState: prometheus.NewDesc( |
| 102 | + prometheus.BuildFQName(namespace, subsystem, "path_state"), |
| 103 | + "Current NVMe controller path state (1 for the current state, 0 for all others).", |
| 104 | + []string{"subsystem", "controller", "transport", "state"}, nil, |
| 105 | + ), |
| 106 | + } |
| 107 | + |
| 108 | + c.scanSubsystems = func() ([]nvmeSubsystem, error) { |
| 109 | + return scanNVMeSubsystems(*sysPath) |
| 110 | + } |
| 111 | + |
| 112 | + return c, nil |
| 113 | +} |
| 114 | + |
| 115 | +func (c *nvmeSubsystemCollector) Update(ch chan<- prometheus.Metric) error { |
| 116 | + subsystems, err := c.scanSubsystems() |
| 117 | + if err != nil { |
| 118 | + if errors.Is(err, os.ErrNotExist) || errors.Is(err, os.ErrPermission) { |
| 119 | + c.logger.Debug("Could not read NVMe subsystem info", "err", err) |
| 120 | + return ErrNoData |
| 121 | + } |
| 122 | + return fmt.Errorf("failed to scan NVMe subsystems: %w", err) |
| 123 | + } |
| 124 | + |
| 125 | + for _, subsys := range subsystems { |
| 126 | + ch <- prometheus.MustNewConstMetric(c.subsystemInfo, prometheus.GaugeValue, 1, |
| 127 | + subsys.Name, subsys.NQN, subsys.Model, subsys.Serial, subsys.IOPolicy) |
| 128 | + |
| 129 | + total := float64(len(subsys.Controllers)) |
| 130 | + var live float64 |
| 131 | + for _, ctrl := range subsys.Controllers { |
| 132 | + state := normalizeControllerState(ctrl.State) |
| 133 | + if state == "live" { |
| 134 | + live++ |
| 135 | + } |
| 136 | + |
| 137 | + for _, s := range nvmeControllerStates { |
| 138 | + val := 0.0 |
| 139 | + if s == state { |
| 140 | + val = 1.0 |
| 141 | + } |
| 142 | + ch <- prometheus.MustNewConstMetric(c.pathState, prometheus.GaugeValue, val, |
| 143 | + subsys.Name, ctrl.Name, ctrl.Transport, s) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + ch <- prometheus.MustNewConstMetric(c.subsystemPaths, prometheus.GaugeValue, total, subsys.Name) |
| 148 | + ch <- prometheus.MustNewConstMetric(c.subsystemPathsLive, prometheus.GaugeValue, live, subsys.Name) |
| 149 | + } |
| 150 | + |
| 151 | + return nil |
| 152 | +} |
| 153 | + |
| 154 | +func scanNVMeSubsystems(sysfsBase string) ([]nvmeSubsystem, error) { |
| 155 | + subsysBase := filepath.Join(sysfsBase, "class", "nvme-subsystem") |
| 156 | + |
| 157 | + entries, err := os.ReadDir(subsysBase) |
| 158 | + if err != nil { |
| 159 | + return nil, err |
| 160 | + } |
| 161 | + |
| 162 | + var subsystems []nvmeSubsystem |
| 163 | + for _, entry := range entries { |
| 164 | + if !strings.HasPrefix(entry.Name(), "nvme-subsys") { |
| 165 | + continue |
| 166 | + } |
| 167 | + subsysPath := filepath.Join(subsysBase, entry.Name()) |
| 168 | + subsys, err := parseNVMeSubsystem(entry.Name(), subsysPath) |
| 169 | + if err != nil { |
| 170 | + continue |
| 171 | + } |
| 172 | + subsystems = append(subsystems, *subsys) |
| 173 | + } |
| 174 | + |
| 175 | + return subsystems, nil |
| 176 | +} |
| 177 | + |
| 178 | +func parseNVMeSubsystem(name, path string) (*nvmeSubsystem, error) { |
| 179 | + subsys := &nvmeSubsystem{Name: name} |
| 180 | + |
| 181 | + subsys.NQN = readSysfsString(filepath.Join(path, "subsysnqn")) |
| 182 | + subsys.Model = readSysfsString(filepath.Join(path, "model")) |
| 183 | + subsys.Serial = readSysfsString(filepath.Join(path, "serial")) |
| 184 | + subsys.IOPolicy = readSysfsString(filepath.Join(path, "iopolicy")) |
| 185 | + |
| 186 | + entries, err := os.ReadDir(path) |
| 187 | + if err != nil { |
| 188 | + return subsys, nil |
| 189 | + } |
| 190 | + |
| 191 | + for _, entry := range entries { |
| 192 | + if !nvmeControllerRE.MatchString(entry.Name()) { |
| 193 | + continue |
| 194 | + } |
| 195 | + ctrlPath := filepath.Join(path, entry.Name()) |
| 196 | + ctrl := nvmeController{ |
| 197 | + Name: entry.Name(), |
| 198 | + State: readSysfsString(filepath.Join(ctrlPath, "state")), |
| 199 | + Transport: readSysfsString(filepath.Join(ctrlPath, "transport")), |
| 200 | + Address: readSysfsString(filepath.Join(ctrlPath, "address")), |
| 201 | + } |
| 202 | + subsys.Controllers = append(subsys.Controllers, ctrl) |
| 203 | + } |
| 204 | + |
| 205 | + return subsys, nil |
| 206 | +} |
| 207 | + |
| 208 | +func readSysfsString(path string) string { |
| 209 | + data, err := os.ReadFile(path) |
| 210 | + if err != nil { |
| 211 | + return "" |
| 212 | + } |
| 213 | + return strings.TrimSpace(string(data)) |
| 214 | +} |
0 commit comments