diff --git a/pkg/systemstatsmonitor/disk_collector.go b/pkg/systemstatsmonitor/disk_collector.go index 66cc30a9e..7e30eed1d 100644 --- a/pkg/systemstatsmonitor/disk_collector.go +++ b/pkg/systemstatsmonitor/disk_collector.go @@ -244,8 +244,15 @@ func (dc *diskCollector) collect() { if dc.config.IncludeRootBlk { devices = append(devices, listRootBlockDevices(dc.config.LsblkTimeout)...) } + + partitions, err := disk.Partitions(false) + if err != nil { + glog.Errorf("Failed to list disk partitions: %v", err) + return + } + if dc.config.IncludeAllAttachedBlk { - devices = append(devices, listAttachedBlockDevices()...) + devices = append(devices, listAttachedBlockDevices(partitions)...) } // Fetch metrics from /proc, /sys. @@ -254,11 +261,6 @@ func (dc *diskCollector) collect() { glog.Errorf("Failed to retrieve disk IO counters: %v", err) return } - partitions, err := disk.Partitions(false) - if err != nil { - glog.Errorf("Failed to list disk partitions: %v", err) - return - } sampleTime := time.Now() defer func() { dc.lastSampleTime = sampleTime }() @@ -310,15 +312,9 @@ func listRootBlockDevices(timeout time.Duration) []string { } // listAttachedBlockDevices lists all currently attached block devices. -func listAttachedBlockDevices() []string { +func listAttachedBlockDevices(partitions []disk.PartitionStat) []string { blks := []string{} - partitions, err := disk.Partitions(false) - if err != nil { - glog.Errorf("Failed to retrieve the list of disk partitions: %v", err) - return blks - } - for _, partition := range partitions { blks = append(blks, partition.Device) } diff --git a/pkg/systemstatsmonitor/system_stats_monitor_test.go b/pkg/systemstatsmonitor/system_stats_monitor_test.go index 2f74b81c8..5beb550ce 100644 --- a/pkg/systemstatsmonitor/system_stats_monitor_test.go +++ b/pkg/systemstatsmonitor/system_stats_monitor_test.go @@ -28,4 +28,4 @@ func TestRegistration(t *testing.T) { assert.NotPanics(t, func() { problemdaemon.GetProblemDaemonHandlerOrDie(SystemStatsMonitorName) }, "System stats monitor failed to register itself as a problem daemon.") -} \ No newline at end of file +}