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.
|
|
|
|
|
2016-12-08 16:06:53 +01:00
|
|
|
// +build linux
|
2016-02-25 13:55:02 +01:00
|
|
|
// +build !nozfs
|
|
|
|
|
2016-12-08 16:06:53 +01:00
|
|
|
package collector
|
|
|
|
|
2016-02-25 13:55:02 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/common/log"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
type zfsMetric struct {
|
2017-01-26 04:47:48 +01:00
|
|
|
subsystem string // The Prometheus subsystem name.
|
|
|
|
name string // The Prometheus name of the metric.
|
|
|
|
sysctl zfsSysctl // The sysctl of the ZFS metric.
|
2016-02-25 13:55:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["zfs"] = NewZFSCollector
|
|
|
|
}
|
|
|
|
|
|
|
|
type zfsCollector struct {
|
2017-01-26 04:47:48 +01:00
|
|
|
zfsMetrics []zfsMetric
|
|
|
|
linuxProcpathBase string
|
2017-02-02 00:32:26 +01:00
|
|
|
linuxZpoolIoPath string
|
2017-01-26 04:47:48 +01:00
|
|
|
linuxPathMap map[string]string
|
2016-02-25 13:55:02 +01:00
|
|
|
}
|
|
|
|
|
2017-02-28 17:44:53 +01:00
|
|
|
// NewZFSCollector returns a new Collector exposing ZFS statistics.
|
2016-02-25 13:55:02 +01:00
|
|
|
func NewZFSCollector() (Collector, error) {
|
2017-01-26 04:47:48 +01:00
|
|
|
var z zfsCollector
|
|
|
|
z.linuxProcpathBase = "spl/kstat/zfs"
|
2017-02-02 00:32:26 +01:00
|
|
|
z.linuxZpoolIoPath = "/*/io"
|
2017-01-26 04:47:48 +01:00
|
|
|
z.linuxPathMap = map[string]string{
|
2017-01-29 22:59:01 +01:00
|
|
|
"zfs_arc": "arcstats",
|
|
|
|
"zfs_dmu_tx": "dmu_tx",
|
|
|
|
"zfs_fm": "fm",
|
|
|
|
"zfs_zfetch": "zfetchstats",
|
|
|
|
"zfs_vdev_cache": "vdev_cache_stats",
|
|
|
|
"zfs_xuio": "xuio_stats",
|
|
|
|
"zfs_zil": "zil",
|
2017-01-26 04:47:48 +01:00
|
|
|
}
|
|
|
|
return &z, 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 {
|
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 {
|
|
|
|
log.Debug(err)
|
|
|
|
return nil
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-01-26 04:47:48 +01:00
|
|
|
func (c *zfsCollector) constSysctlMetric(subsystem string, sysctl zfsSysctl, value int) prometheus.Metric {
|
2016-02-25 13:55:02 +01:00
|
|
|
metricName := sysctl.metricName()
|
|
|
|
|
|
|
|
return prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
2017-02-28 18:42:43 +01:00
|
|
|
prometheus.BuildFQName(Namespace, subsystem, metricName),
|
2016-02-25 13:55:02 +01:00
|
|
|
string(sysctl),
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
prometheus.UntypedValue,
|
|
|
|
float64(value),
|
|
|
|
)
|
|
|
|
}
|
2017-02-02 00:32:26 +01:00
|
|
|
|
|
|
|
func (c *zfsCollector) constPoolMetric(poolName string, sysctl zfsSysctl, value int) prometheus.Metric {
|
|
|
|
metricName := sysctl.metricName()
|
|
|
|
|
|
|
|
return prometheus.MustNewConstMetric(
|
|
|
|
prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, "zfs_zpool", metricName),
|
|
|
|
string(sysctl),
|
|
|
|
[]string{"zpool"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
prometheus.UntypedValue,
|
|
|
|
float64(value),
|
|
|
|
poolName,
|
|
|
|
)
|
|
|
|
}
|