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