|
| 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 | + |
| 24 | + "github.com/prometheus/client_golang/prometheus" |
| 25 | + "github.com/prometheus/procfs/sysfs" |
| 26 | +) |
| 27 | + |
| 28 | +var nvmeControllerStates = []string{ |
| 29 | + "live", "connecting", "resetting", "dead", "unknown", |
| 30 | +} |
| 31 | + |
| 32 | +func normalizeControllerState(raw string) string { |
| 33 | + switch raw { |
| 34 | + case "live", "connecting", "resetting", "dead": |
| 35 | + return raw |
| 36 | + case "deleting", "deleting (no IO)", "new": |
| 37 | + return raw |
| 38 | + default: |
| 39 | + return "unknown" |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +type nvmeSubsystemCollector struct { |
| 44 | + fs sysfs.FS |
| 45 | + logger *slog.Logger |
| 46 | + |
| 47 | + subsystemInfo *prometheus.Desc |
| 48 | + namespaceInfo *prometheus.Desc |
| 49 | + subsystemPaths *prometheus.Desc |
| 50 | + subsystemPathsLive *prometheus.Desc |
| 51 | + pathState *prometheus.Desc |
| 52 | +} |
| 53 | + |
| 54 | +func init() { |
| 55 | + registerCollector("nvmesubsystem", defaultDisabled, NewNVMeSubsystemCollector) |
| 56 | +} |
| 57 | + |
| 58 | +// NewNVMeSubsystemCollector returns a new Collector exposing NVMe-oF subsystem |
| 59 | +// path health from /sys/class/nvme-subsystem/. |
| 60 | +func NewNVMeSubsystemCollector(logger *slog.Logger) (Collector, error) { |
| 61 | + const subsystem = "nvmesubsystem" |
| 62 | + |
| 63 | + fs, err := sysfs.NewFS(*sysPath) |
| 64 | + if err != nil { |
| 65 | + return nil, fmt.Errorf("failed to open sysfs: %w", err) |
| 66 | + } |
| 67 | + |
| 68 | + return &nvmeSubsystemCollector{ |
| 69 | + fs: fs, |
| 70 | + logger: logger, |
| 71 | + subsystemInfo: prometheus.NewDesc( |
| 72 | + prometheus.BuildFQName(namespace, subsystem, "info"), |
| 73 | + "Non-numeric information about an NVMe subsystem.", |
| 74 | + []string{"subsystem", "nqn", "model", "serial", "iopolicy"}, nil, |
| 75 | + ), |
| 76 | + namespaceInfo: prometheus.NewDesc( |
| 77 | + prometheus.BuildFQName(namespace, subsystem, "namespace_info"), |
| 78 | + "Maps an NVMe namespace block device to its subsystem.", |
| 79 | + []string{"subsystem", "device"}, nil, |
| 80 | + ), |
| 81 | + subsystemPaths: prometheus.NewDesc( |
| 82 | + prometheus.BuildFQName(namespace, subsystem, "paths"), |
| 83 | + "Number of controller paths for an NVMe subsystem.", |
| 84 | + []string{"subsystem"}, nil, |
| 85 | + ), |
| 86 | + subsystemPathsLive: prometheus.NewDesc( |
| 87 | + prometheus.BuildFQName(namespace, subsystem, "paths_live"), |
| 88 | + "Number of controller paths in live state for an NVMe subsystem.", |
| 89 | + []string{"subsystem"}, nil, |
| 90 | + ), |
| 91 | + pathState: prometheus.NewDesc( |
| 92 | + prometheus.BuildFQName(namespace, subsystem, "path_state"), |
| 93 | + "Current NVMe controller path state (1 for the current state, 0 for all others).", |
| 94 | + []string{"subsystem", "controller", "transport", "state"}, nil, |
| 95 | + ), |
| 96 | + }, nil |
| 97 | +} |
| 98 | + |
| 99 | +func (c *nvmeSubsystemCollector) Update(ch chan<- prometheus.Metric) error { |
| 100 | + subsystems, err := c.fs.NVMeSubsystemClass() |
| 101 | + if err != nil { |
| 102 | + if errors.Is(err, os.ErrNotExist) || errors.Is(err, os.ErrPermission) { |
| 103 | + c.logger.Debug("Could not read NVMe subsystem info", "err", err) |
| 104 | + return ErrNoData |
| 105 | + } |
| 106 | + return fmt.Errorf("failed to scan NVMe subsystems: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + for _, subsys := range subsystems { |
| 110 | + ch <- prometheus.MustNewConstMetric(c.subsystemInfo, prometheus.GaugeValue, 1, |
| 111 | + subsys.Name, subsys.NQN, subsys.Model, subsys.Serial, subsys.IOPolicy) |
| 112 | + |
| 113 | + for _, ns := range subsys.Namespaces { |
| 114 | + ch <- prometheus.MustNewConstMetric(c.namespaceInfo, prometheus.GaugeValue, 1, |
| 115 | + subsys.Name, ns) |
| 116 | + } |
| 117 | + |
| 118 | + total := float64(len(subsys.Controllers)) |
| 119 | + var live float64 |
| 120 | + for _, ctrl := range subsys.Controllers { |
| 121 | + state := normalizeControllerState(ctrl.State) |
| 122 | + if state == "live" { |
| 123 | + live++ |
| 124 | + } |
| 125 | + |
| 126 | + for _, s := range nvmeControllerStates { |
| 127 | + val := 0.0 |
| 128 | + if s == state { |
| 129 | + val = 1.0 |
| 130 | + } |
| 131 | + ch <- prometheus.MustNewConstMetric(c.pathState, prometheus.GaugeValue, val, |
| 132 | + subsys.Name, ctrl.Name, ctrl.Transport, s) |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + ch <- prometheus.MustNewConstMetric(c.subsystemPaths, prometheus.GaugeValue, total, subsys.Name) |
| 137 | + ch <- prometheus.MustNewConstMetric(c.subsystemPathsLive, prometheus.GaugeValue, live, subsys.Name) |
| 138 | + } |
| 139 | + |
| 140 | + return nil |
| 141 | +} |
0 commit comments