2015-09-26 08:36:40 -07: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.
2021-10-03 04:35:24 -07:00
//go:build !nofilesystem && (linux || freebsd || openbsd || darwin || dragonfly)
2015-09-16 06:34:34 -07:00
// +build !nofilesystem
2021-04-12 09:46:27 -07:00
// +build linux freebsd openbsd darwin dragonfly
2015-09-16 06:34:34 -07:00
package collector
import (
2021-03-23 04:00:06 -07:00
"errors"
2015-09-16 06:34:34 -07:00
"regexp"
2023-09-25 08:41:34 -07:00
"time"
2015-09-16 06:34:34 -07:00
2020-11-14 02:53:51 -08:00
"github.com/go-kit/log"
"github.com/go-kit/log/level"
2015-09-16 06:34:34 -07:00
"github.com/prometheus/client_golang/prometheus"
)
// Arch-dependent implementation must define:
2021-03-23 04:00:06 -07:00
// * defMountPointsExcluded
// * defFSTypesExcluded
2015-09-16 06:34:34 -07:00
// * filesystemLabelNames
// * filesystemCollector.GetStats
var (
2015-11-01 19:25:21 -08:00
filesystemLabelNames = [ ] string { "device" , "mountpoint" , "fstype" }
2015-09-16 06:34:34 -07:00
)
type filesystemCollector struct {
2021-03-23 04:00:06 -07:00
excludedMountPointsPattern * regexp . Regexp
excludedFSTypesPattern * regexp . Regexp
2017-03-22 17:48:18 -07:00
sizeDesc , freeDesc , availDesc * prometheus . Desc
filesDesc , filesFreeDesc * prometheus . Desc
roDesc , deviceErrorDesc * prometheus . Desc
2019-12-31 08:19:37 -08:00
logger log . Logger
2023-09-26 07:41:39 -07:00
config NodeCollectorConfig
2015-09-16 06:34:34 -07:00
}
2017-01-03 06:55:40 -08:00
type filesystemLabels struct {
2018-07-16 06:56:27 -07:00
device , mountPoint , fsType , options string
2017-01-03 06:55:40 -08:00
}
2015-09-16 06:34:34 -07:00
type filesystemStats struct {
2017-03-22 17:48:18 -07:00
labels filesystemLabels
size , free , avail float64
files , filesFree float64
ro , deviceError float64
2015-09-16 06:34:34 -07:00
}
2023-09-25 08:41:34 -07:00
type FilesystemConfig struct {
MountPointsExclude * string
2023-09-27 01:55:06 -07:00
MountPointsExcludeSet bool
2023-09-25 08:41:34 -07:00
OldMountPointsExcluded * string
FSTypesExclude * string
2023-09-27 01:55:06 -07:00
FSTypesExcludeSet bool
2023-09-25 08:41:34 -07:00
OldFSTypesExcluded * string
MountTimeout * time . Duration
StatWorkerCount * int
}
2015-09-16 06:34:34 -07:00
func init ( ) {
2023-09-26 07:41:39 -07:00
registerCollector ( "filesystem" , defaultEnabled , NewFilesystemCollector )
2015-09-16 06:34:34 -07:00
}
2017-02-28 08:44:53 -08:00
// NewFilesystemCollector returns a new Collector exposing filesystems stats.
2023-09-26 07:41:39 -07:00
func NewFilesystemCollector ( config NodeCollectorConfig , logger log . Logger ) ( Collector , error ) {
if * config . Filesystem . OldMountPointsExcluded != "" {
2023-09-27 01:55:06 -07:00
if ! config . Filesystem . MountPointsExcludeSet {
2021-03-23 04:00:06 -07:00
level . Warn ( logger ) . Log ( "msg" , "--collector.filesystem.ignored-mount-points is DEPRECATED and will be removed in 2.0.0, use --collector.filesystem.mount-points-exclude" )
2023-09-26 07:41:39 -07:00
* config . Filesystem . MountPointsExclude = * config . Filesystem . OldMountPointsExcluded
2021-03-23 04:00:06 -07:00
} else {
return nil , errors . New ( "--collector.filesystem.ignored-mount-points and --collector.filesystem.mount-points-exclude are mutually exclusive" )
}
}
2023-09-26 07:41:39 -07:00
if * config . Filesystem . MountPointsExclude != "" {
level . Info ( logger ) . Log ( "msg" , "Parsed flag --collector.filesystem.mount-points-exclude" , "flag" , * config . Filesystem . MountPointsExclude )
2023-09-25 08:41:34 -07:00
} else {
2023-09-26 07:41:39 -07:00
* config . Filesystem . MountPointsExclude = defMountPointsExcluded
2023-09-25 08:41:34 -07:00
}
2021-03-23 04:00:06 -07:00
2023-09-26 07:41:39 -07:00
if * config . Filesystem . OldFSTypesExcluded != "" {
2023-09-27 01:55:06 -07:00
if ! config . Filesystem . FSTypesExcludeSet {
2021-03-23 04:00:06 -07:00
level . Warn ( logger ) . Log ( "msg" , "--collector.filesystem.ignored-fs-types is DEPRECATED and will be removed in 2.0.0, use --collector.filesystem.fs-types-exclude" )
2023-09-26 07:41:39 -07:00
* config . Filesystem . FSTypesExclude = * config . Filesystem . OldFSTypesExcluded
2021-03-23 04:00:06 -07:00
} else {
return nil , errors . New ( "--collector.filesystem.ignored-fs-types and --collector.filesystem.fs-types-exclude are mutually exclusive" )
}
}
2023-09-26 07:41:39 -07:00
if * config . Filesystem . FSTypesExclude != "" {
level . Info ( logger ) . Log ( "msg" , "Parsed flag --collector.filesystem.fs-types-exclude" , "flag" , * config . Filesystem . FSTypesExclude )
2023-09-25 08:41:34 -07:00
} else {
2023-09-26 07:41:39 -07:00
* config . Filesystem . FSTypesExclude = defFSTypesExcluded
2023-09-25 08:41:34 -07:00
}
2021-03-23 04:00:06 -07:00
2015-09-16 06:34:34 -07:00
subsystem := "filesystem"
2023-09-26 07:41:39 -07:00
mountPointPattern := regexp . MustCompile ( * config . Filesystem . MountPointsExclude )
filesystemsTypesPattern := regexp . MustCompile ( * config . Filesystem . FSTypesExclude )
2015-09-16 06:34:34 -07:00
sizeDesc := prometheus . NewDesc (
2018-01-17 08:55:55 -08:00
prometheus . BuildFQName ( namespace , subsystem , "size_bytes" ) ,
2015-09-16 06:34:34 -07:00
"Filesystem size in bytes." ,
filesystemLabelNames , nil ,
)
freeDesc := prometheus . NewDesc (
2018-01-17 08:55:55 -08:00
prometheus . BuildFQName ( namespace , subsystem , "free_bytes" ) ,
2015-09-16 06:34:34 -07:00
"Filesystem free space in bytes." ,
filesystemLabelNames , nil ,
)
availDesc := prometheus . NewDesc (
2018-01-17 08:55:55 -08:00
prometheus . BuildFQName ( namespace , subsystem , "avail_bytes" ) ,
2015-09-16 06:34:34 -07:00
"Filesystem space available to non-root users in bytes." ,
filesystemLabelNames , nil ,
)
filesDesc := prometheus . NewDesc (
2017-09-28 06:06:26 -07:00
prometheus . BuildFQName ( namespace , subsystem , "files" ) ,
2015-09-16 06:34:34 -07:00
"Filesystem total file nodes." ,
filesystemLabelNames , nil ,
)
filesFreeDesc := prometheus . NewDesc (
2017-09-28 06:06:26 -07:00
prometheus . BuildFQName ( namespace , subsystem , "files_free" ) ,
2015-09-16 06:34:34 -07:00
"Filesystem total free file nodes." ,
filesystemLabelNames , nil ,
)
2015-11-10 00:25:04 -08:00
roDesc := prometheus . NewDesc (
2017-09-28 06:06:26 -07:00
prometheus . BuildFQName ( namespace , subsystem , "readonly" ) ,
2015-11-10 00:25:04 -08:00
"Filesystem read-only status." ,
filesystemLabelNames , nil ,
)
2017-03-22 17:48:18 -07:00
deviceErrorDesc := prometheus . NewDesc (
2017-09-28 06:06:26 -07:00
prometheus . BuildFQName ( namespace , subsystem , "device_error" ) ,
2017-05-14 10:46:23 -07:00
"Whether an error occurred while getting statistics for the given device." ,
2017-03-22 17:48:18 -07:00
filesystemLabelNames , nil ,
)
2016-12-19 02:48:32 -08:00
2015-09-16 06:34:34 -07:00
return & filesystemCollector {
2021-03-23 04:00:06 -07:00
excludedMountPointsPattern : mountPointPattern ,
excludedFSTypesPattern : filesystemsTypesPattern ,
sizeDesc : sizeDesc ,
freeDesc : freeDesc ,
availDesc : availDesc ,
filesDesc : filesDesc ,
filesFreeDesc : filesFreeDesc ,
roDesc : roDesc ,
deviceErrorDesc : deviceErrorDesc ,
logger : logger ,
2023-09-25 08:41:34 -07:00
config : config ,
2015-09-16 06:34:34 -07:00
} , nil
}
2017-02-28 10:47:20 -08:00
func ( c * filesystemCollector ) Update ( ch chan <- prometheus . Metric ) error {
2015-09-16 06:34:34 -07:00
stats , err := c . GetStats ( )
if err != nil {
return err
}
2017-01-03 06:55:40 -08:00
// Make sure we expose a metric once, even if there are multiple mounts
seen := map [ filesystemLabels ] bool { }
2015-09-16 06:34:34 -07:00
for _ , s := range stats {
2017-01-03 06:55:40 -08:00
if seen [ s . labels ] {
continue
}
seen [ s . labels ] = true
2017-03-22 17:48:18 -07:00
ch <- prometheus . MustNewConstMetric (
c . deviceErrorDesc , prometheus . GaugeValue ,
s . deviceError , s . labels . device , s . labels . mountPoint , s . labels . fsType ,
)
2023-09-19 05:55:58 -07:00
ch <- prometheus . MustNewConstMetric (
c . roDesc , prometheus . GaugeValue ,
s . ro , s . labels . device , s . labels . mountPoint , s . labels . fsType ,
)
2017-03-22 17:48:18 -07:00
if s . deviceError > 0 {
continue
}
2015-09-16 06:34:34 -07:00
ch <- prometheus . MustNewConstMetric (
c . sizeDesc , prometheus . GaugeValue ,
2017-01-03 06:55:40 -08:00
s . size , s . labels . device , s . labels . mountPoint , s . labels . fsType ,
2015-09-16 06:34:34 -07:00
)
ch <- prometheus . MustNewConstMetric (
c . freeDesc , prometheus . GaugeValue ,
2017-01-03 06:55:40 -08:00
s . free , s . labels . device , s . labels . mountPoint , s . labels . fsType ,
2015-09-16 06:34:34 -07:00
)
ch <- prometheus . MustNewConstMetric (
c . availDesc , prometheus . GaugeValue ,
2017-01-03 06:55:40 -08:00
s . avail , s . labels . device , s . labels . mountPoint , s . labels . fsType ,
2015-09-16 06:34:34 -07:00
)
ch <- prometheus . MustNewConstMetric (
c . filesDesc , prometheus . GaugeValue ,
2017-01-03 06:55:40 -08:00
s . files , s . labels . device , s . labels . mountPoint , s . labels . fsType ,
2015-09-16 06:34:34 -07:00
)
ch <- prometheus . MustNewConstMetric (
c . filesFreeDesc , prometheus . GaugeValue ,
2017-01-03 06:55:40 -08:00
s . filesFree , s . labels . device , s . labels . mountPoint , s . labels . fsType ,
2015-09-16 06:34:34 -07:00
)
}
return nil
}