2015-09-26 15:36:40 +00: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.
|
|
|
|
|
2015-09-16 13:34:34 +00:00
|
|
|
// +build !nofilesystem
|
2016-09-11 16:08:00 +00:00
|
|
|
// +build linux freebsd openbsd darwin,amd64 dragonfly
|
2015-09-16 13:34:34 +00:00
|
|
|
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
|
2019-12-31 16:19:37 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
2015-09-16 13:34:34 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2017-08-12 13:07:24 +00:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
2015-09-16 13:34:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Arch-dependent implementation must define:
|
|
|
|
// * defIgnoredMountPoints
|
2016-05-13 20:38:14 +00:00
|
|
|
// * defIgnoredFSTypes
|
2015-09-16 13:34:34 +00:00
|
|
|
// * filesystemLabelNames
|
|
|
|
// * filesystemCollector.GetStats
|
|
|
|
|
|
|
|
var (
|
2017-08-12 13:07:24 +00:00
|
|
|
ignoredMountPoints = kingpin.Flag(
|
2015-09-16 13:34:34 +00:00
|
|
|
"collector.filesystem.ignored-mount-points",
|
2017-08-12 13:07:24 +00:00
|
|
|
"Regexp of mount points to ignore for filesystem collector.",
|
|
|
|
).Default(defIgnoredMountPoints).String()
|
|
|
|
ignoredFSTypes = kingpin.Flag(
|
2016-03-15 13:01:08 +00:00
|
|
|
"collector.filesystem.ignored-fs-types",
|
2017-08-12 13:07:24 +00:00
|
|
|
"Regexp of filesystem types to ignore for filesystem collector.",
|
|
|
|
).Default(defIgnoredFSTypes).String()
|
2016-03-15 13:01:08 +00:00
|
|
|
|
2015-11-02 03:25:21 +00:00
|
|
|
filesystemLabelNames = []string{"device", "mountpoint", "fstype"}
|
2015-09-16 13:34:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type filesystemCollector struct {
|
2017-03-23 00:48:18 +00:00
|
|
|
ignoredMountPointsPattern *regexp.Regexp
|
|
|
|
ignoredFSTypesPattern *regexp.Regexp
|
|
|
|
sizeDesc, freeDesc, availDesc *prometheus.Desc
|
|
|
|
filesDesc, filesFreeDesc *prometheus.Desc
|
|
|
|
roDesc, deviceErrorDesc *prometheus.Desc
|
2019-12-31 16:19:37 +00:00
|
|
|
logger log.Logger
|
2015-09-16 13:34:34 +00:00
|
|
|
}
|
|
|
|
|
2017-01-03 14:55:40 +00:00
|
|
|
type filesystemLabels struct {
|
2018-07-16 13:56:27 +00:00
|
|
|
device, mountPoint, fsType, options string
|
2017-01-03 14:55:40 +00:00
|
|
|
}
|
|
|
|
|
2015-09-16 13:34:34 +00:00
|
|
|
type filesystemStats struct {
|
2017-03-23 00:48:18 +00:00
|
|
|
labels filesystemLabels
|
|
|
|
size, free, avail float64
|
|
|
|
files, filesFree float64
|
|
|
|
ro, deviceError float64
|
2015-09-16 13:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2017-09-28 13:06:26 +00:00
|
|
|
registerCollector("filesystem", defaultEnabled, NewFilesystemCollector)
|
2015-09-16 13:34:34 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 16:44:53 +00:00
|
|
|
// NewFilesystemCollector returns a new Collector exposing filesystems stats.
|
2019-12-31 16:19:37 +00:00
|
|
|
func NewFilesystemCollector(logger log.Logger) (Collector, error) {
|
2015-09-16 13:34:34 +00:00
|
|
|
subsystem := "filesystem"
|
2016-03-15 13:01:08 +00:00
|
|
|
mountPointPattern := regexp.MustCompile(*ignoredMountPoints)
|
2016-06-06 18:10:50 +00:00
|
|
|
filesystemsTypesPattern := regexp.MustCompile(*ignoredFSTypes)
|
2015-09-16 13:34:34 +00:00
|
|
|
|
|
|
|
sizeDesc := prometheus.NewDesc(
|
2018-01-17 16:55:55 +00:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "size_bytes"),
|
2015-09-16 13:34:34 +00:00
|
|
|
"Filesystem size in bytes.",
|
|
|
|
filesystemLabelNames, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
freeDesc := prometheus.NewDesc(
|
2018-01-17 16:55:55 +00:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "free_bytes"),
|
2015-09-16 13:34:34 +00:00
|
|
|
"Filesystem free space in bytes.",
|
|
|
|
filesystemLabelNames, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
availDesc := prometheus.NewDesc(
|
2018-01-17 16:55:55 +00:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "avail_bytes"),
|
2015-09-16 13:34:34 +00:00
|
|
|
"Filesystem space available to non-root users in bytes.",
|
|
|
|
filesystemLabelNames, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
filesDesc := prometheus.NewDesc(
|
2017-09-28 13:06:26 +00:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "files"),
|
2015-09-16 13:34:34 +00:00
|
|
|
"Filesystem total file nodes.",
|
|
|
|
filesystemLabelNames, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
filesFreeDesc := prometheus.NewDesc(
|
2017-09-28 13:06:26 +00:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "files_free"),
|
2015-09-16 13:34:34 +00:00
|
|
|
"Filesystem total free file nodes.",
|
|
|
|
filesystemLabelNames, nil,
|
|
|
|
)
|
|
|
|
|
2015-11-10 08:25:04 +00:00
|
|
|
roDesc := prometheus.NewDesc(
|
2017-09-28 13:06:26 +00:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "readonly"),
|
2015-11-10 08:25:04 +00:00
|
|
|
"Filesystem read-only status.",
|
|
|
|
filesystemLabelNames, nil,
|
|
|
|
)
|
|
|
|
|
2017-03-23 00:48:18 +00:00
|
|
|
deviceErrorDesc := prometheus.NewDesc(
|
2017-09-28 13:06:26 +00:00
|
|
|
prometheus.BuildFQName(namespace, subsystem, "device_error"),
|
2017-05-14 17:46:23 +00:00
|
|
|
"Whether an error occurred while getting statistics for the given device.",
|
2017-03-23 00:48:18 +00:00
|
|
|
filesystemLabelNames, nil,
|
|
|
|
)
|
2016-12-19 10:48:32 +00:00
|
|
|
|
2015-09-16 13:34:34 +00:00
|
|
|
return &filesystemCollector{
|
2016-03-15 13:01:08 +00:00
|
|
|
ignoredMountPointsPattern: mountPointPattern,
|
2016-06-06 18:10:50 +00:00
|
|
|
ignoredFSTypesPattern: filesystemsTypesPattern,
|
2015-09-16 13:34:34 +00:00
|
|
|
sizeDesc: sizeDesc,
|
|
|
|
freeDesc: freeDesc,
|
|
|
|
availDesc: availDesc,
|
|
|
|
filesDesc: filesDesc,
|
|
|
|
filesFreeDesc: filesFreeDesc,
|
2015-11-10 08:25:04 +00:00
|
|
|
roDesc: roDesc,
|
2017-03-23 00:48:18 +00:00
|
|
|
deviceErrorDesc: deviceErrorDesc,
|
2019-12-31 16:19:37 +00:00
|
|
|
logger: logger,
|
2015-09-16 13:34:34 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2017-02-28 18:47:20 +00:00
|
|
|
func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) error {
|
2015-09-16 13:34:34 +00:00
|
|
|
stats, err := c.GetStats()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-01-03 14:55:40 +00:00
|
|
|
// Make sure we expose a metric once, even if there are multiple mounts
|
|
|
|
seen := map[filesystemLabels]bool{}
|
2015-09-16 13:34:34 +00:00
|
|
|
for _, s := range stats {
|
2017-01-03 14:55:40 +00:00
|
|
|
if seen[s.labels] {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
seen[s.labels] = true
|
|
|
|
|
2017-03-23 00:48:18 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.deviceErrorDesc, prometheus.GaugeValue,
|
|
|
|
s.deviceError, s.labels.device, s.labels.mountPoint, s.labels.fsType,
|
|
|
|
)
|
|
|
|
if s.deviceError > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-16 13:34:34 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.sizeDesc, prometheus.GaugeValue,
|
2017-01-03 14:55:40 +00:00
|
|
|
s.size, s.labels.device, s.labels.mountPoint, s.labels.fsType,
|
2015-09-16 13:34:34 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.freeDesc, prometheus.GaugeValue,
|
2017-01-03 14:55:40 +00:00
|
|
|
s.free, s.labels.device, s.labels.mountPoint, s.labels.fsType,
|
2015-09-16 13:34:34 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.availDesc, prometheus.GaugeValue,
|
2017-01-03 14:55:40 +00:00
|
|
|
s.avail, s.labels.device, s.labels.mountPoint, s.labels.fsType,
|
2015-09-16 13:34:34 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.filesDesc, prometheus.GaugeValue,
|
2017-01-03 14:55:40 +00:00
|
|
|
s.files, s.labels.device, s.labels.mountPoint, s.labels.fsType,
|
2015-09-16 13:34:34 +00:00
|
|
|
)
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.filesFreeDesc, prometheus.GaugeValue,
|
2017-01-03 14:55:40 +00:00
|
|
|
s.filesFree, s.labels.device, s.labels.mountPoint, s.labels.fsType,
|
2015-09-16 13:34:34 +00:00
|
|
|
)
|
2015-11-10 08:25:04 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.roDesc, prometheus.GaugeValue,
|
2017-01-03 14:55:40 +00:00
|
|
|
s.ro, s.labels.device, s.labels.mountPoint, s.labels.fsType,
|
2015-11-10 08:25:04 +00:00
|
|
|
)
|
2015-09-16 13:34:34 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|