2015-09-26 17:36:40 +02:00
|
|
|
// Copyright 2015 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2021-10-03 13:35:24 +02:00
|
|
|
//go:build !nodiskstats
|
2015-05-12 13:06:41 +02:00
|
|
|
// +build !nodiskstats
|
2014-06-04 13:12:34 +02:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-11-14 11:53:51 +01:00
|
|
|
"github.com/go-kit/log"
|
2014-06-04 13:12:34 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-09-28 10:14:12 +02:00
|
|
|
"github.com/prometheus/procfs/blockdevice"
|
2014-06-04 13:12:34 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-09-28 10:14:12 +02:00
|
|
|
secondsPerTick = 1.0 / 1000.0
|
2022-03-07 23:24:08 +01:00
|
|
|
|
2022-03-11 01:43:07 +01:00
|
|
|
// Read sectors and write sectors are the "standard UNIX 512-byte sectors, not any device- or filesystem-specific block size."
|
|
|
|
// See also https://www.kernel.org/doc/Documentation/block/stat.txt
|
2022-03-07 23:24:08 +01:00
|
|
|
unixSectorSize = 512.0
|
2014-06-04 13:12:34 +02:00
|
|
|
|
2022-06-28 08:10:40 +02:00
|
|
|
diskstatsDefaultIgnoredDevices = "^(ram|loop|fd|(h|s|v|xv)d[a-z]|nvme\\d+n\\d+p)\\d+$"
|
2014-06-04 13:12:34 +02:00
|
|
|
)
|
|
|
|
|
2018-01-17 17:55:55 +01:00
|
|
|
type typedFactorDesc struct {
|
|
|
|
desc *prometheus.Desc
|
|
|
|
valueType prometheus.ValueType
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *typedFactorDesc) mustNewConstMetric(value float64, labels ...string) prometheus.Metric {
|
|
|
|
return prometheus.MustNewConstMetric(d.desc, d.valueType, value, labels...)
|
|
|
|
}
|
|
|
|
|
2014-06-04 13:12:34 +02:00
|
|
|
type diskstatsCollector struct {
|
2022-06-07 08:17:31 +02:00
|
|
|
deviceFilter deviceFilter
|
|
|
|
fs blockdevice.FS
|
|
|
|
infoDesc typedFactorDesc
|
|
|
|
descs []typedFactorDesc
|
|
|
|
logger log.Logger
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-09-28 15:06:26 +02:00
|
|
|
registerCollector("diskstats", defaultEnabled, NewDiskstatsCollector)
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
|
|
|
|
2017-02-28 17:44:53 +01:00
|
|
|
// NewDiskstatsCollector returns a new Collector exposing disk device stats.
|
2018-10-15 17:24:28 +02:00
|
|
|
// Docs from https://www.kernel.org/doc/Documentation/iostats.txt
|
2019-12-31 17:19:37 +01:00
|
|
|
func NewDiskstatsCollector(logger log.Logger) (Collector, error) {
|
2014-11-25 03:00:17 +01:00
|
|
|
var diskLabelNames = []string{"device"}
|
2021-09-28 10:14:12 +02:00
|
|
|
fs, err := blockdevice.NewFS(*procPath, *sysPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to open sysfs: %w", err)
|
|
|
|
}
|
2014-11-25 03:00:17 +01:00
|
|
|
|
2022-06-28 08:10:40 +02:00
|
|
|
deviceFilter, err := newDiskstatsDeviceFilter(logger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to parse device filter flags: %w", err)
|
2022-06-07 08:17:31 +02:00
|
|
|
}
|
|
|
|
|
2014-11-25 03:00:17 +01:00
|
|
|
return &diskstatsCollector{
|
2022-06-28 08:10:40 +02:00
|
|
|
deviceFilter: deviceFilter,
|
2022-06-07 08:17:31 +02:00
|
|
|
fs: fs,
|
2021-09-28 10:14:12 +02:00
|
|
|
infoDesc: typedFactorDesc{
|
|
|
|
desc: prometheus.NewDesc(prometheus.BuildFQName(namespace, diskSubsystem, "info"),
|
|
|
|
"Info of /sys/block/<block_device>.",
|
|
|
|
[]string{"device", "major", "minor"},
|
|
|
|
nil,
|
|
|
|
), valueType: prometheus.GaugeValue,
|
|
|
|
},
|
2018-01-17 17:55:55 +01:00
|
|
|
descs: []typedFactorDesc{
|
2016-12-28 15:21:31 +01:00
|
|
|
{
|
2019-02-06 11:36:22 +01:00
|
|
|
desc: readsCompletedDesc, valueType: prometheus.CounterValue,
|
2016-12-28 15:21:31 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: prometheus.NewDesc(
|
2018-01-17 17:55:55 +01:00
|
|
|
prometheus.BuildFQName(namespace, diskSubsystem, "reads_merged_total"),
|
2018-10-15 17:24:28 +02:00
|
|
|
"The total number of reads merged.",
|
2016-12-28 15:21:31 +01:00
|
|
|
diskLabelNames,
|
|
|
|
nil,
|
|
|
|
), valueType: prometheus.CounterValue,
|
|
|
|
},
|
|
|
|
{
|
2019-02-06 11:36:22 +01:00
|
|
|
desc: readBytesDesc, valueType: prometheus.CounterValue,
|
2016-12-28 15:21:31 +01:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 11:36:22 +01:00
|
|
|
desc: readTimeSecondsDesc, valueType: prometheus.CounterValue,
|
2016-12-28 15:21:31 +01:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 11:36:22 +01:00
|
|
|
desc: writesCompletedDesc, valueType: prometheus.CounterValue,
|
2016-12-28 15:21:31 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: prometheus.NewDesc(
|
2018-01-17 17:55:55 +01:00
|
|
|
prometheus.BuildFQName(namespace, diskSubsystem, "writes_merged_total"),
|
2018-10-15 17:24:28 +02:00
|
|
|
"The number of writes merged.",
|
2016-12-28 15:21:31 +01:00
|
|
|
diskLabelNames,
|
|
|
|
nil,
|
|
|
|
), valueType: prometheus.CounterValue,
|
|
|
|
},
|
|
|
|
{
|
2019-02-06 11:36:22 +01:00
|
|
|
desc: writtenBytesDesc, valueType: prometheus.CounterValue,
|
2016-12-28 15:21:31 +01:00
|
|
|
},
|
|
|
|
{
|
2019-02-06 11:36:22 +01:00
|
|
|
desc: writeTimeSecondsDesc, valueType: prometheus.CounterValue,
|
2016-12-28 15:21:31 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, diskSubsystem, "io_now"),
|
2016-12-28 15:21:31 +01:00
|
|
|
"The number of I/Os currently in progress.",
|
|
|
|
diskLabelNames,
|
|
|
|
nil,
|
|
|
|
), valueType: prometheus.GaugeValue,
|
|
|
|
},
|
|
|
|
{
|
2019-02-06 11:36:22 +01:00
|
|
|
desc: ioTimeSecondsDesc, valueType: prometheus.CounterValue,
|
2016-12-28 15:21:31 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: prometheus.NewDesc(
|
2018-01-17 17:55:55 +01:00
|
|
|
prometheus.BuildFQName(namespace, diskSubsystem, "io_time_weighted_seconds_total"),
|
2018-10-15 17:24:28 +02:00
|
|
|
"The weighted # of seconds spent doing I/Os.",
|
|
|
|
diskLabelNames,
|
|
|
|
nil,
|
|
|
|
), valueType: prometheus.CounterValue,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, diskSubsystem, "discards_completed_total"),
|
|
|
|
"The total number of discards completed successfully.",
|
|
|
|
diskLabelNames,
|
|
|
|
nil,
|
|
|
|
), valueType: prometheus.CounterValue,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, diskSubsystem, "discards_merged_total"),
|
|
|
|
"The total number of discards merged.",
|
|
|
|
diskLabelNames,
|
|
|
|
nil,
|
|
|
|
), valueType: prometheus.CounterValue,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, diskSubsystem, "discarded_sectors_total"),
|
2018-10-30 18:45:00 +01:00
|
|
|
"The total number of sectors discarded successfully.",
|
2018-10-15 17:24:28 +02:00
|
|
|
diskLabelNames,
|
|
|
|
nil,
|
|
|
|
), valueType: prometheus.CounterValue,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, diskSubsystem, "discard_time_seconds_total"),
|
|
|
|
"This is the total number of seconds spent by all discards.",
|
2016-12-28 15:21:31 +01:00
|
|
|
diskLabelNames,
|
|
|
|
nil,
|
|
|
|
), valueType: prometheus.CounterValue,
|
|
|
|
},
|
2019-11-25 20:16:15 +01:00
|
|
|
{
|
|
|
|
desc: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, diskSubsystem, "flush_requests_total"),
|
|
|
|
"The total number of flush requests completed successfully",
|
|
|
|
diskLabelNames,
|
|
|
|
nil,
|
|
|
|
), valueType: prometheus.CounterValue,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
desc: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, diskSubsystem, "flush_requests_time_seconds_total"),
|
|
|
|
"This is the total number of seconds spent by all flush requests.",
|
|
|
|
diskLabelNames,
|
|
|
|
nil,
|
|
|
|
), valueType: prometheus.CounterValue,
|
|
|
|
},
|
2014-11-25 03:00:17 +01:00
|
|
|
},
|
2019-12-31 17:19:37 +01:00
|
|
|
logger: logger,
|
2014-11-25 03:00:17 +01:00
|
|
|
}, nil
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
|
|
|
|
2016-12-28 15:21:31 +01:00
|
|
|
func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error {
|
2021-09-28 10:14:12 +02:00
|
|
|
diskStats, err := c.fs.ProcDiskstats()
|
2014-06-04 13:12:34 +02:00
|
|
|
if err != nil {
|
2020-06-15 22:27:14 +02:00
|
|
|
return fmt.Errorf("couldn't get diskstats: %w", err)
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
2014-11-25 03:00:17 +01:00
|
|
|
|
2021-09-28 10:14:12 +02:00
|
|
|
for _, stats := range diskStats {
|
|
|
|
dev := stats.DeviceName
|
2022-06-07 08:17:31 +02:00
|
|
|
if c.deviceFilter.ignored(dev) {
|
2014-06-04 13:12:34 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-09-28 10:14:12 +02:00
|
|
|
ch <- c.infoDesc.mustNewConstMetric(1.0, dev, fmt.Sprint(stats.MajorNumber), fmt.Sprint(stats.MinorNumber))
|
|
|
|
|
|
|
|
statCount := stats.IoStatsCount - 3 // Total diskstats record count, less MajorNumber, MinorNumber and DeviceName
|
|
|
|
|
|
|
|
for i, val := range []float64{
|
|
|
|
float64(stats.ReadIOs),
|
|
|
|
float64(stats.ReadMerges),
|
2022-03-07 23:24:08 +01:00
|
|
|
float64(stats.ReadSectors) * unixSectorSize,
|
2021-09-28 10:14:12 +02:00
|
|
|
float64(stats.ReadTicks) * secondsPerTick,
|
|
|
|
float64(stats.WriteIOs),
|
|
|
|
float64(stats.WriteMerges),
|
2022-03-07 23:24:08 +01:00
|
|
|
float64(stats.WriteSectors) * unixSectorSize,
|
2021-09-28 10:14:12 +02:00
|
|
|
float64(stats.WriteTicks) * secondsPerTick,
|
|
|
|
float64(stats.IOsInProgress),
|
|
|
|
float64(stats.IOsTotalTicks) * secondsPerTick,
|
|
|
|
float64(stats.WeightedIOTicks) * secondsPerTick,
|
|
|
|
float64(stats.DiscardIOs),
|
|
|
|
float64(stats.DiscardMerges),
|
|
|
|
float64(stats.DiscardSectors),
|
|
|
|
float64(stats.DiscardTicks) * secondsPerTick,
|
|
|
|
float64(stats.FlushRequestsCompleted),
|
|
|
|
float64(stats.TimeSpentFlushing) * secondsPerTick,
|
|
|
|
} {
|
|
|
|
if i >= statCount {
|
2018-10-30 18:45:00 +01:00
|
|
|
break
|
|
|
|
}
|
2021-09-28 10:14:12 +02:00
|
|
|
ch <- c.descs[i].mustNewConstMetric(val, dev)
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-28 15:21:31 +01:00
|
|
|
return nil
|
2014-06-04 13:12:34 +02:00
|
|
|
}
|