2016-12-06 20:22:26 +01:00
|
|
|
// Copyright 2016 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 linux && !nozfs
|
|
|
|
// +build linux,!nozfs
|
2016-02-25 13:55:02 +01:00
|
|
|
|
2016-12-08 16:06:53 +01:00
|
|
|
package collector
|
|
|
|
|
2016-02-25 13:55:02 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
2024-09-11 10:51:28 +02:00
|
|
|
"log/slog"
|
2016-02-25 13:55:02 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
2017-02-28 17:44:53 +01:00
|
|
|
var errZFSNotAvailable = errors.New("ZFS / ZFS statistics are not available")
|
2016-02-25 13:55:02 +01:00
|
|
|
|
|
|
|
type zfsSysctl string
|
|
|
|
|
|
|
|
func init() {
|
2017-09-28 15:06:26 +02:00
|
|
|
registerCollector("zfs", defaultEnabled, NewZFSCollector)
|
2016-02-25 13:55:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type zfsCollector struct {
|
2020-05-13 21:06:00 +02:00
|
|
|
linuxProcpathBase string
|
|
|
|
linuxZpoolIoPath string
|
|
|
|
linuxZpoolObjsetPath string
|
2020-10-26 14:06:22 +01:00
|
|
|
linuxZpoolStatePath string
|
2020-05-13 21:06:00 +02:00
|
|
|
linuxPathMap map[string]string
|
2024-09-11 10:51:28 +02:00
|
|
|
logger *slog.Logger
|
2016-02-25 13:55:02 +01:00
|
|
|
}
|
|
|
|
|
2017-02-28 17:44:53 +01:00
|
|
|
// NewZFSCollector returns a new Collector exposing ZFS statistics.
|
2024-09-11 10:51:28 +02:00
|
|
|
func NewZFSCollector(logger *slog.Logger) (Collector, error) {
|
2017-02-28 21:36:35 +01:00
|
|
|
return &zfsCollector{
|
2020-05-13 21:06:00 +02:00
|
|
|
linuxProcpathBase: "spl/kstat/zfs",
|
|
|
|
linuxZpoolIoPath: "/*/io",
|
|
|
|
linuxZpoolObjsetPath: "/*/objset-*",
|
2020-10-26 14:06:22 +01:00
|
|
|
linuxZpoolStatePath: "/*/state",
|
2017-02-28 21:36:35 +01:00
|
|
|
linuxPathMap: map[string]string{
|
2018-02-16 15:46:31 +01:00
|
|
|
"zfs_abd": "abdstats",
|
|
|
|
"zfs_arc": "arcstats",
|
2023-05-23 11:13:48 +02:00
|
|
|
"zfs_dbuf": "dbufstats",
|
2018-02-16 15:46:31 +01:00
|
|
|
"zfs_dmu_tx": "dmu_tx",
|
|
|
|
"zfs_dnode": "dnodestats",
|
|
|
|
"zfs_fm": "fm",
|
|
|
|
"zfs_vdev_cache": "vdev_cache_stats", // vdev_cache is deprecated
|
|
|
|
"zfs_vdev_mirror": "vdev_mirror_stats",
|
|
|
|
"zfs_xuio": "xuio_stats", // no known consumers of the XUIO interface on Linux exist
|
|
|
|
"zfs_zfetch": "zfetchstats",
|
|
|
|
"zfs_zil": "zil",
|
2017-02-28 21:36:35 +01:00
|
|
|
},
|
2019-12-31 17:19:37 +01:00
|
|
|
logger: logger,
|
2017-02-28 21:36:35 +01:00
|
|
|
}, nil
|
2016-02-25 13:55:02 +01:00
|
|
|
}
|
|
|
|
|
2017-02-28 17:44:53 +01:00
|
|
|
func (c *zfsCollector) Update(ch chan<- prometheus.Metric) error {
|
2020-08-20 14:09:55 +02:00
|
|
|
|
|
|
|
if _, err := c.openProcFile(c.linuxProcpathBase); err != nil {
|
|
|
|
if err == errZFSNotAvailable {
|
2024-09-11 10:51:28 +02:00
|
|
|
c.logger.Debug(err.Error())
|
2020-08-20 14:09:55 +02:00
|
|
|
return ErrNoData
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 04:47:48 +01:00
|
|
|
for subsystem := range c.linuxPathMap {
|
2017-02-28 17:44:53 +01:00
|
|
|
if err := c.updateZfsStats(subsystem, ch); err != nil {
|
|
|
|
if err == errZFSNotAvailable {
|
2024-09-11 10:51:28 +02:00
|
|
|
c.logger.Debug(err.Error())
|
2018-02-16 15:46:31 +01:00
|
|
|
// ZFS /proc files are added as new features to ZFS arrive, it is ok to continue
|
|
|
|
continue
|
2017-02-28 17:44:53 +01:00
|
|
|
}
|
2017-01-26 04:47:48 +01:00
|
|
|
return err
|
|
|
|
}
|
2017-01-23 19:56:43 +01:00
|
|
|
}
|
2017-01-23 19:45:36 +01:00
|
|
|
|
2016-02-25 13:55:02 +01:00
|
|
|
// Pool stats
|
|
|
|
return c.updatePoolStats(ch)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s zfsSysctl) metricName() string {
|
|
|
|
parts := strings.Split(string(s), ".")
|
2017-01-31 21:11:56 +01:00
|
|
|
return strings.Replace(parts[len(parts)-1], "-", "_", -1)
|
2016-02-25 13:55:02 +01:00
|
|
|
}
|
|
|
|
|
2024-07-14 14:36:30 +02:00
|
|
|
func (c *zfsCollector) constSysctlMetric(subsystem string, sysctl zfsSysctl, value float64) prometheus.Metric {
|
2016-02-25 13:55:02 +01:00
|
|
|
metricName := sysctl.metricName()
|
|
|
|
|
|
|
|
return prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, metricName),
|
2016-02-25 13:55:02 +01:00
|
|
|
string(sysctl),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
prometheus.UntypedValue,
|
2024-07-14 14:36:30 +02:00
|
|
|
value,
|
2016-02-25 13:55:02 +01:00
|
|
|
)
|
|
|
|
}
|
2017-02-02 00:32:26 +01:00
|
|
|
|
2018-01-06 12:36:55 +01:00
|
|
|
func (c *zfsCollector) constPoolMetric(poolName string, sysctl zfsSysctl, value uint64) prometheus.Metric {
|
2017-02-02 00:32:26 +01:00
|
|
|
metricName := sysctl.metricName()
|
|
|
|
|
|
|
|
return prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2017-09-28 15:06:26 +02:00
|
|
|
prometheus.BuildFQName(namespace, "zfs_zpool", metricName),
|
2017-02-02 00:32:26 +01:00
|
|
|
string(sysctl),
|
|
|
|
[]string{"zpool"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
prometheus.UntypedValue,
|
|
|
|
float64(value),
|
|
|
|
poolName,
|
|
|
|
)
|
|
|
|
}
|
2020-05-13 21:06:00 +02:00
|
|
|
|
|
|
|
func (c *zfsCollector) constPoolObjsetMetric(poolName string, datasetName string, sysctl zfsSysctl, value uint64) prometheus.Metric {
|
|
|
|
metricName := sysctl.metricName()
|
|
|
|
|
|
|
|
return prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "zfs_zpool_dataset", metricName),
|
|
|
|
string(sysctl),
|
|
|
|
[]string{"zpool", "dataset"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
prometheus.UntypedValue,
|
|
|
|
float64(value),
|
|
|
|
poolName,
|
|
|
|
datasetName,
|
|
|
|
)
|
|
|
|
}
|
2020-10-26 14:06:22 +01:00
|
|
|
|
|
|
|
func (c *zfsCollector) constPoolStateMetric(poolName string, stateName string, isActive uint64) prometheus.Metric {
|
|
|
|
return prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(namespace, "zfs_zpool", "state"),
|
|
|
|
"kstat.zfs.misc.state",
|
|
|
|
[]string{"zpool", "state"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
float64(isActive),
|
|
|
|
poolName,
|
|
|
|
stateName,
|
|
|
|
)
|
|
|
|
}
|