mirror of
				https://github.com/prometheus/node_exporter.git
				synced 2025-08-20 18:33:52 -07:00 
			
		
		
		
	check_master
This commit is contained in:
		
							parent
							
								
									38d32a3977
								
							
						
					
					
						commit
						5fafe8f01f
					
				| 
						 | 
					@ -2847,7 +2847,7 @@ node_nfsd_server_rpcs_total 18628
 | 
				
			||||||
node_nfsd_server_threads 8
 | 
					node_nfsd_server_threads 8
 | 
				
			||||||
# HELP node_nvme_info Non-numeric data from /sys/class/nvme/<device>, value is always 1.
 | 
					# HELP node_nvme_info Non-numeric data from /sys/class/nvme/<device>, value is always 1.
 | 
				
			||||||
# TYPE node_nvme_info gauge
 | 
					# TYPE node_nvme_info gauge
 | 
				
			||||||
node_nvme_info{device="nvme0",firmware_revision="1B2QEXP7",model="Samsung SSD 970 PRO 512GB",serial="S680HF8N190894I",state="live"} 1
 | 
					node_nvme_info{cntlid="1997",device="nvme0",firmware_revision="1B2QEXP7",model="Samsung SSD 970 PRO 512GB",serial="S680HF8N190894I",state="live"} 1
 | 
				
			||||||
# HELP node_os_info A metric with a constant '1' value labeled by build_id, id, id_like, image_id, image_version, name, pretty_name, variant, variant_id, version, version_codename, version_id.
 | 
					# HELP node_os_info A metric with a constant '1' value labeled by build_id, id, id_like, image_id, image_version, name, pretty_name, variant, variant_id, version, version_codename, version_id.
 | 
				
			||||||
# TYPE node_os_info gauge
 | 
					# TYPE node_os_info gauge
 | 
				
			||||||
node_os_info{build_id="",id="ubuntu",id_like="debian",image_id="",image_version="",name="Ubuntu",pretty_name="Ubuntu 20.04.2 LTS",variant="",variant_id="",version="20.04.2 LTS (Focal Fossa)",version_codename="focal",version_id="20.04"} 1
 | 
					node_os_info{build_id="",id="ubuntu",id_like="debian",image_id="",image_version="",name="Ubuntu",pretty_name="Ubuntu 20.04.2 LTS",variant="",variant_id="",version="20.04.2 LTS (Focal Fossa)",version_codename="focal",version_id="20.04"} 1
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -2179,6 +2179,11 @@ Lines: 1
 | 
				
			||||||
1B2QEXP7
 | 
					1B2QEXP7
 | 
				
			||||||
Mode: 444
 | 
					Mode: 444
 | 
				
			||||||
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
					# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
				
			||||||
 | 
					Path: sys/class/nvme/nvme0/cntlid
 | 
				
			||||||
 | 
					Lines: 1
 | 
				
			||||||
 | 
					1997
 | 
				
			||||||
 | 
					Mode: 666
 | 
				
			||||||
 | 
					# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
				
			||||||
Path: sys/class/nvme/nvme0/model
 | 
					Path: sys/class/nvme/nvme0/model
 | 
				
			||||||
Lines: 1
 | 
					Lines: 1
 | 
				
			||||||
Samsung SSD 970 PRO 512GB
 | 
					Samsung SSD 970 PRO 512GB
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -19,16 +19,26 @@ package collector
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"log/slog"
 | 
					 | 
				
			||||||
	"os"
 | 
						"os"
 | 
				
			||||||
 | 
						"path/filepath"
 | 
				
			||||||
 | 
						"regexp"
 | 
				
			||||||
 | 
						"strconv"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/prometheus/client_golang/prometheus"
 | 
						"github.com/prometheus/client_golang/prometheus"
 | 
				
			||||||
	"github.com/prometheus/procfs/sysfs"
 | 
						"github.com/prometheus/procfs/sysfs"
 | 
				
			||||||
 | 
						"log/slog"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type nvmeCollector struct {
 | 
					type nvmeCollector struct {
 | 
				
			||||||
	fs     sysfs.FS
 | 
						fs                             sysfs.FS
 | 
				
			||||||
	logger *slog.Logger
 | 
						logger                         *slog.Logger
 | 
				
			||||||
 | 
						namespaceInfo                  *prometheus.Desc
 | 
				
			||||||
 | 
						namespaceCapacityBytes         *prometheus.Desc
 | 
				
			||||||
 | 
						namespaceSizeBytes             *prometheus.Desc
 | 
				
			||||||
 | 
						namespaceUsedBytes             *prometheus.Desc
 | 
				
			||||||
 | 
						namespaceLogicalBlockSizeBytes *prometheus.Desc
 | 
				
			||||||
 | 
						info                           *prometheus.Desc
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
| 
						 | 
					@ -42,9 +52,51 @@ func NewNVMeCollector(logger *slog.Logger) (Collector, error) {
 | 
				
			||||||
		return nil, fmt.Errorf("failed to open sysfs: %w", err)
 | 
							return nil, fmt.Errorf("failed to open sysfs: %w", err)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						info := prometheus.NewDesc(
 | 
				
			||||||
 | 
							prometheus.BuildFQName(namespace, "nvme", "info"),
 | 
				
			||||||
 | 
							"Non-numeric data from /sys/class/nvme/<device>, value is always 1.",
 | 
				
			||||||
 | 
							[]string{"device", "firmware_revision", "model", "serial", "state", "cntlid"},
 | 
				
			||||||
 | 
							nil,
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
						namespaceInfo := prometheus.NewDesc(
 | 
				
			||||||
 | 
							prometheus.BuildFQName(namespace, "nvme", "namespace_info"),
 | 
				
			||||||
 | 
							"Information about NVMe namespaces. Value is always 1",
 | 
				
			||||||
 | 
							[]string{"device", "nsid", "ana_state"}, nil,
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						namespaceCapacityBytes := prometheus.NewDesc(
 | 
				
			||||||
 | 
							prometheus.BuildFQName(namespace, "nvme", "namespace_capacity_bytes"),
 | 
				
			||||||
 | 
							"Capacity of the NVMe namespace in bytes. Computed as namespace_size * namespace_logical_block_size",
 | 
				
			||||||
 | 
							[]string{"device", "nsid"}, nil,
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						namespaceSizeBytes := prometheus.NewDesc(
 | 
				
			||||||
 | 
							prometheus.BuildFQName(namespace, "nvme", "namespace_size_bytes"),
 | 
				
			||||||
 | 
							"Size of the NVMe namespace in bytes. Available in /sys/class/nvme/<device>/<namespace>/size",
 | 
				
			||||||
 | 
							[]string{"device", "nsid"}, nil,
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						namespaceUsedBytes := prometheus.NewDesc(
 | 
				
			||||||
 | 
							prometheus.BuildFQName(namespace, "nvme", "namespace_used_bytes"),
 | 
				
			||||||
 | 
							"Used space of the NVMe namespace in bytes. Available in /sys/class/nvme/<device>/<namespace>/nuse",
 | 
				
			||||||
 | 
							[]string{"device", "nsid"}, nil,
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						namespaceLogicalBlockSizeBytes := prometheus.NewDesc(
 | 
				
			||||||
 | 
							prometheus.BuildFQName(namespace, "nvme", "namespace_logical_block_size_bytes"),
 | 
				
			||||||
 | 
							"Logical block size of the NVMe namespace in bytes. Usually 4Kb. Available in /sys/class/nvme/<device>/<namespace>/queue/logical_block_size",
 | 
				
			||||||
 | 
							[]string{"device", "nsid"}, nil,
 | 
				
			||||||
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return &nvmeCollector{
 | 
						return &nvmeCollector{
 | 
				
			||||||
		fs:     fs,
 | 
							fs:                             fs,
 | 
				
			||||||
		logger: logger,
 | 
							logger:                         logger,
 | 
				
			||||||
 | 
							namespaceInfo:                  namespaceInfo,
 | 
				
			||||||
 | 
							namespaceCapacityBytes:         namespaceCapacityBytes,
 | 
				
			||||||
 | 
							namespaceSizeBytes:             namespaceSizeBytes,
 | 
				
			||||||
 | 
							namespaceUsedBytes:             namespaceUsedBytes,
 | 
				
			||||||
 | 
							namespaceLogicalBlockSizeBytes: namespaceLogicalBlockSizeBytes,
 | 
				
			||||||
 | 
							info:                           info,
 | 
				
			||||||
	}, nil
 | 
						}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -59,14 +111,92 @@ func (c *nvmeCollector) Update(ch chan<- prometheus.Metric) error {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, device := range devices {
 | 
						for _, device := range devices {
 | 
				
			||||||
		infoDesc := prometheus.NewDesc(
 | 
					 | 
				
			||||||
			prometheus.BuildFQName(namespace, "nvme", "info"),
 | 
					 | 
				
			||||||
			"Non-numeric data from /sys/class/nvme/<device>, value is always 1.",
 | 
					 | 
				
			||||||
			[]string{"device", "firmware_revision", "model", "serial", "state"},
 | 
					 | 
				
			||||||
			nil,
 | 
					 | 
				
			||||||
		)
 | 
					 | 
				
			||||||
		infoValue := 1.0
 | 
							infoValue := 1.0
 | 
				
			||||||
		ch <- prometheus.MustNewConstMetric(infoDesc, prometheus.GaugeValue, infoValue, device.Name, device.FirmwareRevision, device.Model, device.Serial, device.State)
 | 
					
 | 
				
			||||||
 | 
							devicePath := filepath.Join(*sysPath, "class/nvme", device.Name)
 | 
				
			||||||
 | 
							cntlid, err := readUintFromFile(filepath.Join(devicePath, "cntlid"))
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								c.logger.Debug("failed to read cntlid", "device", device.Name, "err", err)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							ch <- prometheus.MustNewConstMetric(c.info, prometheus.GaugeValue, infoValue, device.Name, device.FirmwareRevision, device.Model, device.Serial, device.State, strconv.FormatUint(cntlid, 10))
 | 
				
			||||||
 | 
							// Find namespace directories.
 | 
				
			||||||
 | 
							namespacePaths, err := filepath.Glob(filepath.Join(devicePath, "nvme[0-9]*c[0-9]*n[0-9]*"))
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								c.logger.Error("failed to list NVMe namespaces", "device", device.Name, "err", err)
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							re := regexp.MustCompile(`nvme[0-9]+c[0-9]+n([0-9]+)`)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							for _, namespacePath := range namespacePaths {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								// Read namespace data.
 | 
				
			||||||
 | 
								match := re.FindStringSubmatch(filepath.Base(namespacePath))
 | 
				
			||||||
 | 
								if len(match) == 0 {
 | 
				
			||||||
 | 
									continue
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								nsid := match[1]
 | 
				
			||||||
 | 
								nuse, err := readUintFromFile(filepath.Join(namespacePath, "nuse"))
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									c.logger.Debug("failed to read nuse", "device", device.Name, "namespace", match[0], "err", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								nsze, err := readUintFromFile(filepath.Join(namespacePath, "size"))
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									c.logger.Debug("failed to read size", "device", device.Name, "namespace", match[0], "err", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								lbaSize, err := readUintFromFile(filepath.Join(namespacePath, "queue", "logical_block_size"))
 | 
				
			||||||
 | 
								if err != nil {
 | 
				
			||||||
 | 
									c.logger.Debug("failed to read queue/logical_block_size", "device", device.Name, "namespace", match[0], "err", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								ncap := nsze * lbaSize
 | 
				
			||||||
 | 
								anaState := "unknown"
 | 
				
			||||||
 | 
								anaStateSysfs, err := os.ReadFile(filepath.Join(namespacePath, "ana_state"))
 | 
				
			||||||
 | 
								if err == nil {
 | 
				
			||||||
 | 
									anaState = strings.TrimSpace(string(anaStateSysfs))
 | 
				
			||||||
 | 
								} else {
 | 
				
			||||||
 | 
									c.logger.Debug("failed to read ana_state", "device", device.Name, "namespace", match[0], "err", err)
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								ch <- prometheus.MustNewConstMetric(
 | 
				
			||||||
 | 
									c.namespaceInfo,
 | 
				
			||||||
 | 
									prometheus.GaugeValue,
 | 
				
			||||||
 | 
									1.0,
 | 
				
			||||||
 | 
									device.Name,
 | 
				
			||||||
 | 
									nsid,
 | 
				
			||||||
 | 
									anaState,
 | 
				
			||||||
 | 
								)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								ch <- prometheus.MustNewConstMetric(
 | 
				
			||||||
 | 
									c.namespaceCapacityBytes,
 | 
				
			||||||
 | 
									prometheus.GaugeValue,
 | 
				
			||||||
 | 
									float64(ncap),
 | 
				
			||||||
 | 
									device.Name,
 | 
				
			||||||
 | 
									nsid,
 | 
				
			||||||
 | 
								)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								ch <- prometheus.MustNewConstMetric(
 | 
				
			||||||
 | 
									c.namespaceSizeBytes,
 | 
				
			||||||
 | 
									prometheus.GaugeValue,
 | 
				
			||||||
 | 
									float64(nsze),
 | 
				
			||||||
 | 
									device.Name,
 | 
				
			||||||
 | 
									nsid,
 | 
				
			||||||
 | 
								)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								ch <- prometheus.MustNewConstMetric(
 | 
				
			||||||
 | 
									c.namespaceUsedBytes,
 | 
				
			||||||
 | 
									prometheus.GaugeValue,
 | 
				
			||||||
 | 
									float64(nuse*lbaSize),
 | 
				
			||||||
 | 
									device.Name,
 | 
				
			||||||
 | 
									nsid,
 | 
				
			||||||
 | 
								)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								ch <- prometheus.MustNewConstMetric(
 | 
				
			||||||
 | 
									c.namespaceLogicalBlockSizeBytes,
 | 
				
			||||||
 | 
									prometheus.GaugeValue,
 | 
				
			||||||
 | 
									float64(lbaSize),
 | 
				
			||||||
 | 
									device.Name,
 | 
				
			||||||
 | 
									nsid,
 | 
				
			||||||
 | 
								)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return nil
 | 
						return nil
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue