Skip to content
This repository has been archived by the owner on Aug 22, 2024. It is now read-only.

Commit

Permalink
Add node_system_storage_size_bytes metric as replacement for node_sys…
Browse files Browse the repository at this point in the history
…tem_storage_info.
  • Loading branch information
lesovsky committed Aug 5, 2021
1 parent 33e46f0 commit 9bb0c8d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
42 changes: 38 additions & 4 deletions internal/collector/linux_diskstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
)

const (
// Linux always considers sectors to be 512 bytes long independently of the devices real block size.
// https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/include/linux/types.h#n117
diskSectorSize = 512
)

Expand All @@ -32,7 +34,8 @@ type diskstatsCollector struct {
ionow typedDesc
iotime typedDesc
iotimeweighted typedDesc
storages typedDesc
storageInfo typedDesc
storageSize typedDesc
}

// NewDiskstatsCollector returns a new Collector exposing disk device stats.
Expand Down Expand Up @@ -121,8 +124,15 @@ func NewDiskstatsCollector(constLabels labels, settings model.CollectorSettings)
[]string{"device"}, constLabels,
settings.Filters,
),
storages: newBuiltinTypedDesc(
descOpts{"node", "system", "storage_info", "Labeled information about storage devices present in the system.", 0},
// DEPRECATED.
storageInfo: newBuiltinTypedDesc(
descOpts{"node", "system", "storage_info", "Labeled information about storage devices present in the system. DEPRECATED: consider using node_system_storage_size_bytes.", 0},
prometheus.GaugeValue,
[]string{"device", "rotational", "scheduler"}, constLabels,
settings.Filters,
),
storageSize: newBuiltinTypedDesc(
descOpts{"node", "system", "storage_size_bytes", "Total size of storage device in bytes.", diskSectorSize},
prometheus.GaugeValue,
[]string{"device", "rotational", "scheduler"}, constLabels,
settings.Filters,
Expand Down Expand Up @@ -191,7 +201,8 @@ func (c *diskstatsCollector) Update(config Config, ch chan<- prometheus.Metric)
log.Warnf("get storage devices properties failed: %s; skip", err)
} else {
for _, s := range storages {
ch <- c.storages.newConstMetric(1, s.device, s.rotational, s.scheduler)
ch <- c.storageInfo.newConstMetric(1, s.device, s.rotational, s.scheduler)
ch <- c.storageSize.newConstMetric(float64(s.size), s.device, s.rotational, s.scheduler)
}
}

Expand Down Expand Up @@ -249,6 +260,7 @@ type storageDeviceProperties struct {
device string
rotational string
scheduler string
size int64
}

// getStorageProperties reads storages properties.
Expand Down Expand Up @@ -280,10 +292,17 @@ func getStorageProperties(path string) ([]storageDeviceProperties, error) {
continue
}

size, err := getDeviceSize(devpath)
if err != nil {
log.Warnf("get size for %s failed: %s; skip", device, err)
continue
}

storages = append(storages, storageDeviceProperties{
device: device,
scheduler: scheduler,
rotational: rotational,
size: size,
})
}
return storages, nil
Expand Down Expand Up @@ -336,3 +355,18 @@ func getDeviceScheduler(devpath string) (string, error) {

return "", fmt.Errorf("unknown scheduler: %s", line)
}

// getDeviceSize returns size of the device in sectors.
func getDeviceSize(devpath string) (int64, error) {
sizeStr, err := os.ReadFile(devpath + "/size")
if err != nil {
return 0, err
}

size, err := strconv.ParseInt(strings.TrimSpace(string(sizeStr)), 10, 64)
if err != nil {
return 0, err
}

return size, nil
}
21 changes: 19 additions & 2 deletions internal/collector/linux_diskstats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestDiskstatsCollector_Update(t *testing.T) {
"node_disk_io_time_seconds_total",
"node_disk_io_time_weighted_seconds_total",
"node_system_storage_info",
"node_system_storage_size_bytes",
},
collector: NewDiskstatsCollector,
collectorSettings: model.CollectorSettings{Filters: filter.New()},
Expand All @@ -50,8 +51,8 @@ func Test_parseDiskstats(t *testing.T) {

func Test_getStorageProperties(t *testing.T) {
want := []storageDeviceProperties{
{device: "sda", rotational: "0", scheduler: "mq-deadline"},
{device: "sdb", rotational: "1", scheduler: "deadline"},
{device: "sda", rotational: "0", scheduler: "mq-deadline", size: 234441648},
{device: "sdb", rotational: "1", scheduler: "deadline", size: 3907029168},
}

storages, err := getStorageProperties("testdata/sys/block/*")
Expand Down Expand Up @@ -90,3 +91,19 @@ func Test_getDeviceScheduler(t *testing.T) {
assert.Error(t, err)
assert.Equal(t, "", r)
}

func Test_getDeviceSize(t *testing.T) {
sz, err := getDeviceSize("testdata/sys/block/sda")
assert.NoError(t, err)
assert.Equal(t, int64(234441648), sz)

// Read file with bad content
sz, err = getDeviceSize("testdata/sys/block/sdz")
assert.Error(t, err)
assert.Equal(t, int64(0), sz)

// Read unknown file
sz, err = getDeviceSize("testdata/proc/meminfo.golden")
assert.Error(t, err)
assert.Equal(t, int64(0), sz)
}
1 change: 1 addition & 0 deletions internal/collector/testdata/sys/block/sda/size
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
234441648
1 change: 1 addition & 0 deletions internal/collector/testdata/sys/block/sdb/size
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3907029168

0 comments on commit 9bb0c8d

Please sign in to comment.