diff --git a/collector/diskstats_linux.go b/collector/diskstats_linux.go index 45a3b179..c9a31238 100644 --- a/collector/diskstats_linux.go +++ b/collector/diskstats_linux.go @@ -385,12 +385,13 @@ func (c *diskstatsCollector) Update(ch chan<- prometheus.Metric) error { } } - ioDeviceStats, err := c.fs.SysBlockDeviceIOStat(dev) - if err != nil && !os.IsNotExist(err) { - c.logger.Debug("Failed to get block device io stats", "device", dev, "err", err) + ioDeviceStats, ioErr := c.fs.SysBlockDeviceIOStat(dev) + if ioErr == nil { + ch <- c.ioErrDesc.mustNewConstMetric(float64(ioDeviceStats.IOErrCount), dev) + ch <- c.ioDoneDesc.mustNewConstMetric(float64(ioDeviceStats.IODoneCount), dev) + } else if !os.IsNotExist(ioErr) { + c.logger.Info("Failed to get block device io stats", "device", dev, "err", ioErr) } - ch <- c.ioErrDesc.mustNewConstMetric(float64(ioDeviceStats.IOErrCount), dev) - ch <- c.ioDoneDesc.mustNewConstMetric(float64(ioDeviceStats.IODoneCount), dev) } return nil diff --git a/collector/ext4_linux.go b/collector/ext4_linux.go index 6ae591b2..a7b15630 100644 --- a/collector/ext4_linux.go +++ b/collector/ext4_linux.go @@ -61,37 +61,42 @@ func (c *ext4Collector) Update(ch chan<- prometheus.Metric) error { return nil } -// updateExt4Stats collects statistics for a single ext4 filesystem. -func (c *ext4Collector) updateExt4Stats(ch chan<- prometheus.Metric, s *ext4.Stats) { - const ( - subsystem = "ext4" - ) - var ( - labels = []string{"device"} - ) +type ext4Metric struct { + name string + desc string + value float64 +} - metrics := []struct { - name string - desc string - value float64 - }{ +func (c *ext4Collector) getMetrics(s *ext4.Stats) []ext4Metric { + return []ext4Metric{ { - name: "errors", + name: "errors_total", desc: "Number of ext4 filesystem errors.", value: float64(s.Errors), }, { - name: "warnings", + name: "warnings_total", desc: "Number of ext4 filesystem warnings.", value: float64(s.Warnings), }, { - name: "messages", + name: "messages_total", desc: "Number of ext4 filesystem log messages.", value: float64(s.Messages), }, } +} + +// updateExt4Stats collects statistics for a single ext4 filesystem. +func (c *ext4Collector) updateExt4Stats(ch chan<- prometheus.Metric, s *ext4.Stats) { + const ( + subsystem = "ext4" + ) + var ( + labels = []string{"device"} + ) + metrics := c.getMetrics(s) for _, m := range metrics { desc := prometheus.NewDesc( prometheus.BuildFQName(namespace, subsystem, m.name), diff --git a/collector/ext4_linux_test.go b/collector/ext4_linux_test.go new file mode 100644 index 00000000..8ec32232 --- /dev/null +++ b/collector/ext4_linux_test.go @@ -0,0 +1,69 @@ +// Copyright 2019 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. + +//go:build !noext4 + +package collector + +import ( + "testing" + + "github.com/prometheus/procfs" + "github.com/prometheus/procfs/ext4" +) + +var expectedExt4Metrics = [][]ext4Metric{ + { + {name: "errors_total", value: 12}, + {name: "warnings_total", value: 34}, + {name: "messages_total", value: 567}, + }, +} + +func checkExt4Metric(exp, got *ext4Metric) bool { + if exp.name != got.name || + exp.value != got.value { + return false + } + return true +} + +func TestExt4(t *testing.T) { + fs, err := ext4.NewFS(procfs.DefaultMountPoint, "fixtures/sys") + if err != nil { + t.Fatal(err) + } + collector := &ext4Collector{fs: fs} + + stats, err := collector.fs.ProcStat() + if err != nil { + t.Fatalf("Failed to retrieve ext4 stats: %v", err) + } + if len(stats) != len(expectedExt4Metrics) { + t.Fatalf("Unexpected number of ext4 stats: expected %v, got %v", len(expectedExt4Metrics), len(stats)) + } + + for i, s := range stats { + metrics := collector.getMetrics(s) + if len(metrics) != len(expectedExt4Metrics[i]) { + t.Fatalf("Unexpected number of ext4 metrics: expected %v, got %v", len(expectedExt4Metrics[i]), len(metrics)) + } + + for j, m := range metrics { + exp := expectedExt4Metrics[i][j] + if !checkExt4Metric(&exp, &m) { + t.Errorf("Incorrect ext4 metric: expected %#v, got: %#v", exp, m) + } + } + } +} diff --git a/collector/fixtures/sys.ttar b/collector/fixtures/sys.ttar index bc8744cb..624bb67a 100644 --- a/collector/fixtures/sys.ttar +++ b/collector/fixtures/sys.ttar @@ -595,6 +595,19 @@ Mode: 644 Directory: sys/block/sda Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Directory: sys/block/sda/device +Mode: 775 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/block/sda/device/iodone_cnt +Lines: 1 +307 +Mode: 664 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/block/sda/device/ioerr_cnt +Lines: 1 +3 +Mode: 664 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: sys/block/sda/queue Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -826,6 +839,22 @@ Lines: 1 none Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Directory: sys/block/sr0 +Mode: 775 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Directory: sys/block/sr0/device +Mode: 775 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/block/sr0/device/iodone_cnt +Lines: 1 +2767 +Mode: 664 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/block/sr0/device/ioerr_cnt +Lines: 1 +29 +Mode: 664 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: sys/bus Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -2219,16 +2248,6 @@ Lines: 1 Samsung SSD 970 PRO 512GB Mode: 444 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Path: sys/class/nvme/nvme0/serial -Lines: 1 -S680HF8N190894I -Mode: 444 -# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Path: sys/class/nvme/nvme0/state -Lines: 1 -live -Mode: 444 -# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: sys/class/nvme/nvme0/nvme0c0n0 Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -2237,11 +2256,6 @@ Lines: 1 optimized Mode: 444 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -Path: sys/class/nvme/nvme0/nvme0c0n0/size -Lines: 1 -3906250000 -Mode: 444 -# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Path: sys/class/nvme/nvme0/nvme0c0n0/nuse Lines: 1 488281250 @@ -2255,6 +2269,21 @@ Lines: 1 4096 Mode: 644 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/class/nvme/nvme0/nvme0c0n0/size +Lines: 1 +3906250000 +Mode: 444 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/class/nvme/nvme0/serial +Lines: 1 +S680HF8N190894I +Mode: 444 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/class/nvme/nvme0/state +Lines: 1 +live +Mode: 444 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: sys/class/power_supply Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -11413,6 +11442,27 @@ Lines: 1 4096 Mode: 444 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Directory: sys/fs/ext4 +Mode: 775 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Directory: sys/fs/ext4/sdb1 +Mode: 775 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/fs/ext4/sdb1/errors_count +Lines: 1 +12 +Mode: 664 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/fs/ext4/sdb1/msg_count +Lines: 1 +567 +Mode: 664 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +Path: sys/fs/ext4/sdb1/warning_count +Lines: 1 +34 +Mode: 664 +# ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Directory: sys/fs/xfs Mode: 755 # ttar - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -