func()

in pkg/systemstatsmonitor/disk_collector.go [237:296]


func (dc *diskCollector) collect() {
	if dc == nil {
		return
	}

	// List available devices.
	devices := []string{}
	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(partitions)...)
	}

	// Fetch metrics from /proc, /sys.
	ioCountersStats, err := disk.IOCounters(devices...)
	if err != nil {
		glog.Errorf("Failed to retrieve disk IO counters: %v", err)
		return
	}
	sampleTime := time.Now()
	defer func() { dc.lastSampleTime = sampleTime }()

	// Record metrics regarding disk IO.
	dc.recordIOCounters(ioCountersStats, sampleTime)

	// Record metrics regarding disk space usage.
	if dc.mBytesUsed == nil {
		return
	}

	// to make sure that the rows are not duplicated
	// we display only the only one row even if there are
	// mutiple rows for the same disk.
	seen := make(map[string]bool)
	for _, partition := range partitions {
		if seen[partition.Device] {
			continue
		}
		seen[partition.Device] = true
		usageStat, err := disk.Usage(partition.Mountpoint)
		if err != nil {
			glog.Errorf("Failed to retrieve disk usage for %q: %v", partition.Mountpoint, err)
			continue
		}
		deviceName := strings.TrimPrefix(partition.Device, "/dev/")
		fstype := partition.Fstype
		opttypes := partition.Opts
		dc.mBytesUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "free"}, int64(usageStat.Free))
		dc.mBytesUsed.Record(map[string]string{deviceNameLabel: deviceName, fsTypeLabel: fstype, mountOptionLabel: opttypes, stateLabel: "used"}, int64(usageStat.Used))
	}

}