mirror of
				https://github.com/prometheus/node_exporter.git
				synced 2025-08-20 18:33:52 -07:00 
			
		
		
		
	making sure the tests pass
Signed-off-by: Shashwat Hiregoudar <shashwat.h@flipkart.com>
This commit is contained in:
		
							parent
							
								
									74533358f9
								
							
						
					
					
						commit
						6cc12c3a03
					
				| 
						 | 
				
			
			@ -2847,7 +2847,7 @@ node_nfsd_server_rpcs_total 18628
 | 
			
		|||
node_nfsd_server_threads 8
 | 
			
		||||
# HELP node_nvme_info Non-numeric data from /sys/class/nvme/<device>, value is always 1.
 | 
			
		||||
# 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.
 | 
			
		||||
# 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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2179,6 +2179,11 @@ Lines: 1
 | 
			
		|||
1B2QEXP7
 | 
			
		||||
Mode: 444
 | 
			
		||||
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
Path: sys/class/nvme/nvme0/cntlid
 | 
			
		||||
Lines: 1
 | 
			
		||||
1997
 | 
			
		||||
Mode: 666
 | 
			
		||||
# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 | 
			
		||||
Path: sys/class/nvme/nvme0/model
 | 
			
		||||
Lines: 1
 | 
			
		||||
Samsung SSD 970 PRO 512GB
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -25,15 +25,14 @@ import (
 | 
			
		|||
	"strconv"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/go-kit/log"
 | 
			
		||||
	"github.com/go-kit/log/level"
 | 
			
		||||
	"github.com/prometheus/client_golang/prometheus"
 | 
			
		||||
	"github.com/prometheus/procfs/sysfs"
 | 
			
		||||
	"log/slog"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type nvmeCollector struct {
 | 
			
		||||
	fs                             sysfs.FS
 | 
			
		||||
	logger                         log.Logger
 | 
			
		||||
	logger                         *slog.Logger
 | 
			
		||||
	namespaceInfo                  *prometheus.Desc
 | 
			
		||||
	namespaceCapacityBytes         *prometheus.Desc
 | 
			
		||||
	namespaceSizeBytes             *prometheus.Desc
 | 
			
		||||
| 
						 | 
				
			
			@ -47,7 +46,7 @@ func init() {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
// NewNVMeCollector returns a new Collector exposing NVMe stats.
 | 
			
		||||
func NewNVMeCollector(logger log.Logger) (Collector, error) {
 | 
			
		||||
func NewNVMeCollector(logger *slog.Logger) (Collector, error) {
 | 
			
		||||
	fs, err := sysfs.NewFS(*sysPath)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return nil, fmt.Errorf("failed to open sysfs: %w", err)
 | 
			
		||||
| 
						 | 
				
			
			@ -105,7 +104,7 @@ func (c *nvmeCollector) Update(ch chan<- prometheus.Metric) error {
 | 
			
		|||
	devices, err := c.fs.NVMeClass()
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		if errors.Is(err, os.ErrNotExist) {
 | 
			
		||||
			level.Debug(c.logger).Log("msg", "nvme statistics not found, skipping")
 | 
			
		||||
			c.logger.Debug("nvme statistics not found, skipping")
 | 
			
		||||
			return ErrNoData
 | 
			
		||||
		}
 | 
			
		||||
		return fmt.Errorf("error obtaining NVMe class info: %w", err)
 | 
			
		||||
| 
						 | 
				
			
			@ -117,13 +116,13 @@ func (c *nvmeCollector) Update(ch chan<- prometheus.Metric) error {
 | 
			
		|||
		devicePath := filepath.Join(*sysPath, "class/nvme", device.Name)
 | 
			
		||||
		cntlid, err := readUintFromFile(filepath.Join(devicePath, "cntlid"))
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			level.Debug(c.logger).Log("msg", "failed to read cntlid", "device", device.Name, "err", err)
 | 
			
		||||
			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 {
 | 
			
		||||
			level.Error(c.logger).Log("msg", "failed to list NVMe namespaces", "device", device.Name, "err", err)
 | 
			
		||||
			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]+)`)
 | 
			
		||||
| 
						 | 
				
			
			@ -138,15 +137,15 @@ func (c *nvmeCollector) Update(ch chan<- prometheus.Metric) error {
 | 
			
		|||
			nsid := match[1]
 | 
			
		||||
			nuse, err := readUintFromFile(filepath.Join(namespacePath, "nuse"))
 | 
			
		||||
			if err != nil {
 | 
			
		||||
				level.Debug(c.logger).Log("msg", "failed to read nuse", "device", device.Name, "namespace", match[0], "err", err)
 | 
			
		||||
				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 {
 | 
			
		||||
				level.Debug(c.logger).Log("msg", "failed to read size", "device", device.Name, "namespace", match[0], "err", err)
 | 
			
		||||
				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 {
 | 
			
		||||
				level.Debug(c.logger).Log("msg", "failed to read queue/logical_block_size", "device", device.Name, "namespace", match[0], "err", err)
 | 
			
		||||
				c.logger.Debug("failed to read queue/logical_block_size", "device", device.Name, "namespace", match[0], "err", err)
 | 
			
		||||
			}
 | 
			
		||||
			ncap := nsze * lbaSize
 | 
			
		||||
			anaState := "unknown"
 | 
			
		||||
| 
						 | 
				
			
			@ -154,7 +153,7 @@ func (c *nvmeCollector) Update(ch chan<- prometheus.Metric) error {
 | 
			
		|||
			if err == nil {
 | 
			
		||||
				anaState = strings.TrimSpace(string(anaStateSysfs))
 | 
			
		||||
			} else {
 | 
			
		||||
				level.Debug(c.logger).Log("msg", "failed to read ana_state", "device", device.Name, "namespace", match[0], "err", err)
 | 
			
		||||
				c.logger.Debug("failed to read ana_state", "device", device.Name, "namespace", match[0], "err", err)
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
			ch <- prometheus.MustNewConstMetric(
 | 
			
		||||
| 
						 | 
				
			
			@ -201,4 +200,4 @@ func (c *nvmeCollector) Update(ch chan<- prometheus.Metric) error {
 | 
			
		|||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue