From a939e513d77158ef3bb595d2cc83a0e678780a64 Mon Sep 17 00:00:00 2001 From: fs185143 Date: Tue, 2 Jul 2024 10:49:08 +0000 Subject: [PATCH 01/12] foundational logic for linux disk capacity metric --- collector/diskstats_common.go | 6 ++++++ collector/diskstats_linux.go | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/collector/diskstats_common.go b/collector/diskstats_common.go index 2ab84438e7..a18c09cfd0 100644 --- a/collector/diskstats_common.go +++ b/collector/diskstats_common.go @@ -91,6 +91,12 @@ var ( diskLabelNames, nil, ) + + diskCapacityDesc = prometheus.NewDesc( + prometheus.BuildFQName(namespace, diskSubsystem, "capacity_bytes"), + "Capacity of the disk in bytes.", + diskLabelNames, nil, + ) ) func newDiskstatsDeviceFilter(logger log.Logger) (deviceFilter, error) { diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index ed5d044a61..686d488c5d 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -219,6 +219,9 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) { nil, ), valueType: prometheus.CounterValue, }, + { + desc: diskCapacityDesc, valueType: prometheus.GaugeValue, + }, }, filesystemInfoDesc: typedFactorDesc{ desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "filesystem_info"), @@ -366,6 +369,28 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { } } } + + sizePath := fmt.Sprintf("/sys/block/%s/size", dev) + sizeBytes, err := os.ReadFile(sizePath) + if err != nil { + level.Error(c.logger).Log("msg", "Failed to read disk size", "path", sizePath, "err", err) + continue + } + + size, err := strconv.ParseUint(strings.TrimSpace(string(sizeBytes)), 10, 64) + if err != nil { + level.Error(c.logger).Log("msg", "Failed to parse disk size", "err", err) + continue + } + + sizeInBytes := size * 512 // convert size to bytes (size is in 512-byte sectors) + + ch <- prometheus.MustNewConstMetric( + diskCapacityDesc, + prometheus.GaugeValue, + float64(sizeInBytes), + dev, + ) } return nil } From 672a9cf2a0ae199b42c7a9b66f93923cd95081ef Mon Sep 17 00:00:00 2001 From: fs185143 Date: Tue, 2 Jul 2024 13:02:18 +0000 Subject: [PATCH 02/12] using ghw for disk size --- collector/diskstats_linux.go | 44 +++++++++++++++++++----------------- go.mod | 10 ++++++++ go.sum | 16 +++++++++++++ 3 files changed, 49 insertions(+), 21 deletions(-) diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index 686d488c5d..cd893c2d28 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -23,6 +23,8 @@ import ( "strconv" "strings" + "github.com/jaypipes/ghw" + "github.com/go-kit/log" "github.com/go-kit/log/level" "github.com/prometheus/client_golang/prometheus" @@ -85,6 +87,7 @@ type diskstatsCollector struct { filesystemInfoDesc typedFactorDesc deviceMapperInfoDesc typedFactorDesc ataDescs map[string]typedFactorDesc + diskSizeDesc typedFactorDesc logger log.Logger getUdevDeviceProperties func(uint32, uint32) (udevInfo, error) } @@ -219,9 +222,6 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) { nil, ), valueType: prometheus.CounterValue, }, - { - desc: diskCapacityDesc, valueType: prometheus.GaugeValue, - }, }, filesystemInfoDesc: typedFactorDesc{ desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "filesystem_info"), @@ -260,6 +260,12 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) { ), valueType: prometheus.GaugeValue, }, }, + diskSizeDesc: typedFactorDesc{ + desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "size_bytes"), + "Size of the disk in bytes.", + diskLabelNames, nil, + ), valueType: prometheus.GaugeValue, + }, logger: logger, } @@ -284,7 +290,6 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { if c.deviceFilter.ignored(dev) { continue } - info, err := getUdevDeviceProperties(stats.MajorNumber, stats.MinorNumber) if err != nil { level.Debug(c.logger).Log("msg", "Failed to parse udev info", "err", err) @@ -370,27 +375,24 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { } } - sizePath := fmt.Sprintf("/sys/block/%s/size", dev) - sizeBytes, err := os.ReadFile(sizePath) - if err != nil { - level.Error(c.logger).Log("msg", "Failed to read disk size", "path", sizePath, "err", err) - continue + if err := c.updateDiskSize(ch); err != nil { + level.Error(c.logger).Log("msg", "Failed to update disk size metric", "err", err) } + } - size, err := strconv.ParseUint(strings.TrimSpace(string(sizeBytes)), 10, 64) - if err != nil { - level.Error(c.logger).Log("msg", "Failed to parse disk size", "err", err) + return nil +} + +func (c *diskstatsCollector) updateDiskSize(ch chan<- prometheus.Metric) error { + block, err := ghw.Block() + if err != nil { + return err + } + for _, disk := range block.Disks { + if c.deviceFilter.ignored(disk.Name) { continue } - - sizeInBytes := size * 512 // convert size to bytes (size is in 512-byte sectors) - - ch <- prometheus.MustNewConstMetric( - diskCapacityDesc, - prometheus.GaugeValue, - float64(sizeInBytes), - dev, - ) + ch <- c.diskSizeDesc.mustNewConstMetric(float64(disk.SizeBytes), disk.Name) } return nil } diff --git a/go.mod b/go.mod index ee979cd611..4f4d04d5ea 100644 --- a/go.mod +++ b/go.mod @@ -33,6 +33,15 @@ require ( howett.net/plist v1.0.1 ) +require ( + github.com/StackExchange/wmi v1.2.1 // indirect + github.com/ghodss/yaml v1.0.0 // indirect + github.com/go-ole/go-ole v1.2.6 // indirect + github.com/jaypipes/pcidb v1.0.0 // indirect + github.com/mitchellh/go-homedir v1.1.0 // indirect + github.com/pkg/errors v0.9.1 // indirect +) + require ( github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect github.com/beorn7/perks v1.0.1 // indirect @@ -42,6 +51,7 @@ require ( github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/go-cmp v0.6.0 // indirect + github.com/jaypipes/ghw v0.12.0 github.com/jpillora/backoff v1.0.0 // indirect github.com/mdlayher/genetlink v1.3.2 // indirect github.com/mdlayher/socket v0.4.1 // indirect diff --git a/go.sum b/go.sum index 5f9265e264..a407cbde8d 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA= +github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8= github.com/alecthomas/kingpin/v2 v2.4.0 h1:f48lwail6p8zpO1bC4TxtqACaGqHYA22qkHjHpqDjYY= github.com/alecthomas/kingpin/v2 v2.4.0/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE= github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc= @@ -21,10 +23,15 @@ github.com/dennwc/ioctl v1.0.0 h1:DsWAAjIxRqNcLn9x6mwfuf2pet3iB7aK90K4tF16rLg= github.com/dennwc/ioctl v1.0.0/go.mod h1:ellh2YB5ldny99SBU/VX7Nq0xiZbHphf1DrtHxxjMk0= github.com/ema/qdisc v1.0.0 h1:EHLG08FVRbWLg8uRICa3xzC9Zm0m7HyMHfXobWFnXYg= github.com/ema/qdisc v1.0.0/go.mod h1:FhIc0fLYi7f+lK5maMsesDqwYojIOh3VfRs8EVd5YJQ= +github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= +github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU= github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0= github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA= github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= @@ -41,6 +48,10 @@ github.com/hodgesds/perf-utils v0.7.0 h1:7KlHGMuig4FRH5fNw68PV6xLmgTe7jKs9hgAcEA github.com/hodgesds/perf-utils v0.7.0/go.mod h1:LAklqfDadNKpkxoAJNHpD5tkY0rkZEVdnCEWN5k4QJY= github.com/illumos/go-kstat v0.0.0-20210513183136-173c9b0a9973 h1:hk4LPqXIY/c9XzRbe7dA6qQxaT6Axcbny0L/G5a4owQ= github.com/illumos/go-kstat v0.0.0-20210513183136-173c9b0a9973/go.mod h1:PoK3ejP3LJkGTzKqRlpvCIFas3ncU02v8zzWDW+g0FY= +github.com/jaypipes/ghw v0.12.0 h1:xU2/MDJfWmBhJnujHY9qwXQLs3DBsf0/Xa9vECY0Tho= +github.com/jaypipes/ghw v0.12.0/go.mod h1:jeJGbkRB2lL3/gxYzNYzEDETV1ZJ56OKr+CSeSEym+g= +github.com/jaypipes/pcidb v1.0.0 h1:vtZIfkiCUE42oYbJS0TAq9XSfSmcsgo9IdxSm9qzYU8= +github.com/jaypipes/pcidb v1.0.0/go.mod h1:TnYUvqhPBzCKnH34KrIX22kAeEbDCSRJ9cqLRCuNDfk= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA= github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w= @@ -66,10 +77,14 @@ github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA= github.com/mdlayher/wifi v0.2.0 h1:vwbVyu5MWTiFNvOmWdvIx9veBlMVnEasZ90PhUi1DYU= github.com/mdlayher/wifi v0.2.0/go.mod h1:yOfWhVZ4FFJxeHzAxDzt87Om9EkqqcCiY9Gi5gfSXwI= +github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= +github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaLpt7tQ7oU= github.com/opencontainers/selinux v1.11.0/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus-community/go-runit v0.1.0 h1:uTWEj/Fn2RoLdfg/etSqwzgYNOYPrARx1BHUN052tGA= @@ -114,6 +129,7 @@ golang.org/x/oauth2 v0.18.0/go.mod h1:Wf7knwG0MPoWIMMBgFlEaSUDaKskp0dCfrlJRJXbBi golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20211031064116-611d5d643895/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= From 6006706e120c4474b79cebb1608e6d607b43b3aa Mon Sep 17 00:00:00 2001 From: fs185143 Date: Tue, 2 Jul 2024 13:06:25 +0000 Subject: [PATCH 03/12] moved disk size update out of loop --- collector/diskstats_linux.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index cd893c2d28..c6058ec38d 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -374,12 +374,11 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { } } } - - if err := c.updateDiskSize(ch); err != nil { - level.Error(c.logger).Log("msg", "Failed to update disk size metric", "err", err) - } } + if err := c.updateDiskSize(ch); err != nil { + level.Error(c.logger).Log("msg", "Failed to update disk size metric", "err", err) + } return nil } From 2f203b4bc8db536e19109bc177c0c8e14a3b9d2c Mon Sep 17 00:00:00 2001 From: fs185143 Date: Tue, 2 Jul 2024 13:07:50 +0000 Subject: [PATCH 04/12] updated diskstats test --- collector/diskstats_linux_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/collector/diskstats_linux_test.go b/collector/diskstats_linux_test.go index 88d8c8265e..4c1f1765ed 100644 --- a/collector/diskstats_linux_test.go +++ b/collector/diskstats_linux_test.go @@ -247,6 +247,9 @@ node_disk_reads_merged_total{device="sdb"} 841 node_disk_reads_merged_total{device="sdc"} 141 node_disk_reads_merged_total{device="sr0"} 0 node_disk_reads_merged_total{device="vda"} 15386 +# HELP node_disk_size_bytes Size of the disk in bytes. +# TYPE node_disk_size_bytes gauge +node_disk_size_bytes{device="nvme0n1"} 1.024209543168e+12 # HELP node_disk_write_time_seconds_total This is the total number of seconds spent by all writes. # TYPE node_disk_write_time_seconds_total counter node_disk_write_time_seconds_total{device="dm-0"} 1.1585578e+06 From fcbe324466b9eca663921aaa04e68b9cede0b0fa Mon Sep 17 00:00:00 2001 From: fs185143 Date: Tue, 2 Jul 2024 13:11:26 +0000 Subject: [PATCH 05/12] removed unused variable --- collector/diskstats_common.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/collector/diskstats_common.go b/collector/diskstats_common.go index a18c09cfd0..2ab84438e7 100644 --- a/collector/diskstats_common.go +++ b/collector/diskstats_common.go @@ -91,12 +91,6 @@ var ( diskLabelNames, nil, ) - - diskCapacityDesc = prometheus.NewDesc( - prometheus.BuildFQName(namespace, diskSubsystem, "capacity_bytes"), - "Capacity of the disk in bytes.", - diskLabelNames, nil, - ) ) func newDiskstatsDeviceFilter(logger log.Logger) (deviceFilter, error) { From 3f5296da6d206615da5fed744df92e258977713e Mon Sep 17 00:00:00 2001 From: fs185143 Date: Tue, 2 Jul 2024 13:26:46 +0000 Subject: [PATCH 06/12] updated deps --- collector/diskstats_linux.go | 5 ++--- go.mod | 2 -- go.sum | 4 ---- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index c6058ec38d..1ea9093032 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -23,10 +23,9 @@ import ( "strconv" "strings" - "github.com/jaypipes/ghw" - "github.com/go-kit/log" "github.com/go-kit/log/level" + "github.com/jaypipes/ghw/pkg/block" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/procfs/blockdevice" ) @@ -383,7 +382,7 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { } func (c *diskstatsCollector) updateDiskSize(ch chan<- prometheus.Metric) error { - block, err := ghw.Block() + block, err := block.New() if err != nil { return err } diff --git a/go.mod b/go.mod index 4f4d04d5ea..b0da4456f2 100644 --- a/go.mod +++ b/go.mod @@ -37,8 +37,6 @@ require ( github.com/StackExchange/wmi v1.2.1 // indirect github.com/ghodss/yaml v1.0.0 // indirect github.com/go-ole/go-ole v1.2.6 // indirect - github.com/jaypipes/pcidb v1.0.0 // indirect - github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/pkg/errors v0.9.1 // indirect ) diff --git a/go.sum b/go.sum index a407cbde8d..d70eb76dbe 100644 --- a/go.sum +++ b/go.sum @@ -50,8 +50,6 @@ github.com/illumos/go-kstat v0.0.0-20210513183136-173c9b0a9973 h1:hk4LPqXIY/c9Xz github.com/illumos/go-kstat v0.0.0-20210513183136-173c9b0a9973/go.mod h1:PoK3ejP3LJkGTzKqRlpvCIFas3ncU02v8zzWDW+g0FY= github.com/jaypipes/ghw v0.12.0 h1:xU2/MDJfWmBhJnujHY9qwXQLs3DBsf0/Xa9vECY0Tho= github.com/jaypipes/ghw v0.12.0/go.mod h1:jeJGbkRB2lL3/gxYzNYzEDETV1ZJ56OKr+CSeSEym+g= -github.com/jaypipes/pcidb v1.0.0 h1:vtZIfkiCUE42oYbJS0TAq9XSfSmcsgo9IdxSm9qzYU8= -github.com/jaypipes/pcidb v1.0.0/go.mod h1:TnYUvqhPBzCKnH34KrIX22kAeEbDCSRJ9cqLRCuNDfk= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA= github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w= @@ -77,8 +75,6 @@ github.com/mdlayher/socket v0.4.1 h1:eM9y2/jlbs1M615oshPQOHZzj6R6wMT7bX5NPiQvn2U github.com/mdlayher/socket v0.4.1/go.mod h1:cAqeGjoufqdxWkD7DkpyS+wcefOtmu5OQ8KuoJGIReA= github.com/mdlayher/wifi v0.2.0 h1:vwbVyu5MWTiFNvOmWdvIx9veBlMVnEasZ90PhUi1DYU= github.com/mdlayher/wifi v0.2.0/go.mod h1:yOfWhVZ4FFJxeHzAxDzt87Om9EkqqcCiY9Gi5gfSXwI= -github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y= -github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU= github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaLpt7tQ7oU= From 58b2f9c5bf33d982cc48d461324838069037b345 Mon Sep 17 00:00:00 2001 From: fs185143 Date: Wed, 3 Jul 2024 09:12:04 +0000 Subject: [PATCH 07/12] updated to use new incoming procfs function --- collector/diskstats_linux.go | 29 +++++++++-------------------- collector/diskstats_linux_test.go | 3 ++- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index 1ea9093032..90c5b78f5e 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -25,7 +25,6 @@ import ( "github.com/go-kit/log" "github.com/go-kit/log/level" - "github.com/jaypipes/ghw/pkg/block" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/procfs/blockdevice" ) @@ -267,7 +266,6 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) { }, logger: logger, } - // Only enable getting device properties from udev if the directory is readable. if stat, err := os.Stat(*udevDataPath); err != nil || !stat.IsDir() { level.Error(logger).Log("msg", "Failed to open directory, disabling udev device properties", "path", *udevDataPath) @@ -283,7 +281,6 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { if err != nil { return fmt.Errorf("couldn't get diskstats: %w", err) } - for _, stats := range diskStats { dev := stats.DeviceName if c.deviceFilter.ignored(dev) { @@ -373,24 +370,16 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { } } } - } - - if err := c.updateDiskSize(ch); err != nil { - level.Error(c.logger).Log("msg", "Failed to update disk size metric", "err", err) - } - return nil -} - -func (c *diskstatsCollector) updateDiskSize(ch chan<- prometheus.Metric) error { - block, err := block.New() - if err != nil { - return err - } - for _, disk := range block.Disks { - if c.deviceFilter.ignored(disk.Name) { - continue + queueStats, err := c.fs.SysBlockDeviceQueueStats(dev) + if err != nil { + level.Error(c.logger).Log("msg", "Failed to get disk queue stats", "err", err) + } + size, err := c.fs.SysBlockDeviceSize(dev) + if err != nil { + level.Error(c.logger).Log("msg", "Failed to get disk size", "err", err) } - ch <- c.diskSizeDesc.mustNewConstMetric(float64(disk.SizeBytes), disk.Name) + sizeBytes := size * queueStats.LogicalBlockSize + ch <- c.diskSizeDesc.mustNewConstMetric(float64(sizeBytes), dev) } return nil } diff --git a/collector/diskstats_linux_test.go b/collector/diskstats_linux_test.go index 4c1f1765ed..19ee5e3b75 100644 --- a/collector/diskstats_linux_test.go +++ b/collector/diskstats_linux_test.go @@ -249,7 +249,8 @@ node_disk_reads_merged_total{device="sr0"} 0 node_disk_reads_merged_total{device="vda"} 15386 # HELP node_disk_size_bytes Size of the disk in bytes. # TYPE node_disk_size_bytes gauge -node_disk_size_bytes{device="nvme0n1"} 1.024209543168e+12 +node_disk_size_bytes{device="nvme0n1"} 1.073741824e+10 +node_disk_size_bytes{device="nvme1n1"} 9e+11 # HELP node_disk_write_time_seconds_total This is the total number of seconds spent by all writes. # TYPE node_disk_write_time_seconds_total counter node_disk_write_time_seconds_total{device="dm-0"} 1.1585578e+06 From 7788877f302e46e2bd6b6587b740dd99602c0478 Mon Sep 17 00:00:00 2001 From: fs185143 Date: Wed, 3 Jul 2024 09:13:54 +0000 Subject: [PATCH 08/12] updated deps --- go.mod | 8 -------- go.sum | 12 ------------ 2 files changed, 20 deletions(-) diff --git a/go.mod b/go.mod index b0da4456f2..ee979cd611 100644 --- a/go.mod +++ b/go.mod @@ -33,13 +33,6 @@ require ( howett.net/plist v1.0.1 ) -require ( - github.com/StackExchange/wmi v1.2.1 // indirect - github.com/ghodss/yaml v1.0.0 // indirect - github.com/go-ole/go-ole v1.2.6 // indirect - github.com/pkg/errors v0.9.1 // indirect -) - require ( github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect github.com/beorn7/perks v1.0.1 // indirect @@ -49,7 +42,6 @@ require ( github.com/go-logfmt/logfmt v0.5.1 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/go-cmp v0.6.0 // indirect - github.com/jaypipes/ghw v0.12.0 github.com/jpillora/backoff v1.0.0 // indirect github.com/mdlayher/genetlink v1.3.2 // indirect github.com/mdlayher/socket v0.4.1 // indirect diff --git a/go.sum b/go.sum index d70eb76dbe..5f9265e264 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/StackExchange/wmi v1.2.1 h1:VIkavFPXSjcnS+O8yTq7NI32k0R5Aj+v39y29VYDOSA= -github.com/StackExchange/wmi v1.2.1/go.mod h1:rcmrprowKIVzvc+NUiLncP2uuArMWLCbu9SBzvHz7e8= github.com/alecthomas/kingpin/v2 v2.4.0 h1:f48lwail6p8zpO1bC4TxtqACaGqHYA22qkHjHpqDjYY= github.com/alecthomas/kingpin/v2 v2.4.0/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE= github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc= @@ -23,15 +21,10 @@ github.com/dennwc/ioctl v1.0.0 h1:DsWAAjIxRqNcLn9x6mwfuf2pet3iB7aK90K4tF16rLg= github.com/dennwc/ioctl v1.0.0/go.mod h1:ellh2YB5ldny99SBU/VX7Nq0xiZbHphf1DrtHxxjMk0= github.com/ema/qdisc v1.0.0 h1:EHLG08FVRbWLg8uRICa3xzC9Zm0m7HyMHfXobWFnXYg= github.com/ema/qdisc v1.0.0/go.mod h1:FhIc0fLYi7f+lK5maMsesDqwYojIOh3VfRs8EVd5YJQ= -github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= -github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU= github.com/go-kit/log v0.2.1/go.mod h1:NwTd00d/i8cPZ3xOwwiv2PO5MOcx78fFErGNcVmBjv0= github.com/go-logfmt/logfmt v0.5.1 h1:otpy5pqBCBZ1ng9RQ0dPu4PN7ba75Y/aA+UpowDyNVA= github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= -github.com/go-ole/go-ole v1.2.5/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= -github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY= -github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk= github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= @@ -48,8 +41,6 @@ github.com/hodgesds/perf-utils v0.7.0 h1:7KlHGMuig4FRH5fNw68PV6xLmgTe7jKs9hgAcEA github.com/hodgesds/perf-utils v0.7.0/go.mod h1:LAklqfDadNKpkxoAJNHpD5tkY0rkZEVdnCEWN5k4QJY= github.com/illumos/go-kstat v0.0.0-20210513183136-173c9b0a9973 h1:hk4LPqXIY/c9XzRbe7dA6qQxaT6Axcbny0L/G5a4owQ= github.com/illumos/go-kstat v0.0.0-20210513183136-173c9b0a9973/go.mod h1:PoK3ejP3LJkGTzKqRlpvCIFas3ncU02v8zzWDW+g0FY= -github.com/jaypipes/ghw v0.12.0 h1:xU2/MDJfWmBhJnujHY9qwXQLs3DBsf0/Xa9vECY0Tho= -github.com/jaypipes/ghw v0.12.0/go.mod h1:jeJGbkRB2lL3/gxYzNYzEDETV1ZJ56OKr+CSeSEym+g= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA= github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w= @@ -79,8 +70,6 @@ github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+ github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/opencontainers/selinux v1.11.0 h1:+5Zbo97w3Lbmb3PeqQtpmTkMwsW5nRI3YaLpt7tQ7oU= github.com/opencontainers/selinux v1.11.0/go.mod h1:E5dMC3VPuVvVHDYmi78qvhJp8+M586T4DlDRYpFkyec= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus-community/go-runit v0.1.0 h1:uTWEj/Fn2RoLdfg/etSqwzgYNOYPrARx1BHUN052tGA= @@ -125,7 +114,6 @@ golang.org/x/oauth2 v0.18.0/go.mod h1:Wf7knwG0MPoWIMMBgFlEaSUDaKskp0dCfrlJRJXbBi golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20211031064116-611d5d643895/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= From fa915d704d6ad19face7640ad020906029c19800 Mon Sep 17 00:00:00 2001 From: fs185143 Date: Wed, 3 Jul 2024 09:23:28 +0000 Subject: [PATCH 09/12] continue if err --- collector/diskstats_linux.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index 90c5b78f5e..eb2e3f126e 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -281,11 +281,13 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { if err != nil { return fmt.Errorf("couldn't get diskstats: %w", err) } + for _, stats := range diskStats { dev := stats.DeviceName if c.deviceFilter.ignored(dev) { continue } + info, err := getUdevDeviceProperties(stats.MajorNumber, stats.MinorNumber) if err != nil { level.Debug(c.logger).Log("msg", "Failed to parse udev info", "err", err) @@ -370,13 +372,16 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { } } } + queueStats, err := c.fs.SysBlockDeviceQueueStats(dev) if err != nil { level.Error(c.logger).Log("msg", "Failed to get disk queue stats", "err", err) + continue } size, err := c.fs.SysBlockDeviceSize(dev) if err != nil { level.Error(c.logger).Log("msg", "Failed to get disk size", "err", err) + continue } sizeBytes := size * queueStats.LogicalBlockSize ch <- c.diskSizeDesc.mustNewConstMetric(float64(sizeBytes), dev) From d2ecfa7a1dbea7158e50a6d5f46ff72be79771fd Mon Sep 17 00:00:00 2001 From: fs185143 Date: Wed, 3 Jul 2024 09:24:21 +0000 Subject: [PATCH 10/12] returned whitespace --- collector/diskstats_linux.go | 1 + 1 file changed, 1 insertion(+) diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index eb2e3f126e..3e00d1ce64 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -266,6 +266,7 @@ func NewDiskstatsCollector(logger log.Logger) (Collector, error) { }, logger: logger, } + // Only enable getting device properties from udev if the directory is readable. if stat, err := os.Stat(*udevDataPath); err != nil || !stat.IsDir() { level.Error(logger).Log("msg", "Failed to open directory, disabling udev device properties", "path", *udevDataPath) From 6a188ae6e501f88b4f9618391aeb1adb4d78fa78 Mon Sep 17 00:00:00 2001 From: fs185143 Date: Wed, 3 Jul 2024 09:29:31 +0000 Subject: [PATCH 11/12] disk -> device --- collector/diskstats_linux.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index 3e00d1ce64..ca0f8deeb1 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -376,12 +376,12 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { queueStats, err := c.fs.SysBlockDeviceQueueStats(dev) if err != nil { - level.Error(c.logger).Log("msg", "Failed to get disk queue stats", "err", err) + level.Error(c.logger).Log("msg", "Failed to get device queue stats", "err", err) continue } size, err := c.fs.SysBlockDeviceSize(dev) if err != nil { - level.Error(c.logger).Log("msg", "Failed to get disk size", "err", err) + level.Error(c.logger).Log("msg", "Failed to get device size", "err", err) continue } sizeBytes := size * queueStats.LogicalBlockSize From d8e74d65fdada6219fab08383abfb10244cc6858 Mon Sep 17 00:00:00 2001 From: fs185143 Date: Tue, 9 Jul 2024 12:29:38 +0000 Subject: [PATCH 12/12] updated with new incoming SysBlockDeviceSizeBytes --- collector/diskstats_linux.go | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index ca0f8deeb1..d90b6862a8 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -374,17 +374,11 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { } } - queueStats, err := c.fs.SysBlockDeviceQueueStats(dev) - if err != nil { - level.Error(c.logger).Log("msg", "Failed to get device queue stats", "err", err) - continue - } - size, err := c.fs.SysBlockDeviceSize(dev) + sizeBytes, err := c.fs.SysBlockDeviceSizeBytes(dev) if err != nil { level.Error(c.logger).Log("msg", "Failed to get device size", "err", err) continue } - sizeBytes := size * queueStats.LogicalBlockSize ch <- c.diskSizeDesc.mustNewConstMetric(float64(sizeBytes), dev) } return nil