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.
2021-10-03 13:35:24 +02:00
//go:build !nofilesystem && (linux || freebsd || openbsd || darwin || dragonfly)
2015-09-16 15:34:34 +02:00
// +build !nofilesystem
2021-04-12 18:46:27 +02:00
// +build linux freebsd openbsd darwin dragonfly
2015-09-16 15:34:34 +02:00
package collector
import (
2021-03-23 12:00:06 +01:00
"errors"
2015-09-16 15:34:34 +02:00
"regexp"
2023-03-07 09:25:05 +01:00
"github.com/alecthomas/kingpin/v2"
2020-11-14 11:53:51 +01:00
"github.com/go-kit/log"
"github.com/go-kit/log/level"
2015-09-16 15:34:34 +02:00
"github.com/prometheus/client_golang/prometheus"
)
// Arch-dependent implementation must define:
2021-03-23 12:00:06 +01:00
// * defMountPointsExcluded
// * defFSTypesExcluded
2015-09-16 15:34:34 +02:00
// * filesystemLabelNames
// * filesystemCollector.GetStats
var (
2021-03-23 12:00:06 +01:00
mountPointsExcludeSet bool
mountPointsExclude = kingpin . Flag (
"collector.filesystem.mount-points-exclude" ,
"Regexp of mount points to exclude for filesystem collector." ,
) . Default ( defMountPointsExcluded ) . PreAction ( func ( c * kingpin . ParseContext ) error {
mountPointsExcludeSet = true
return nil
} ) . String ( )
oldMountPointsExcluded = 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." ,
2021-03-23 12:00:06 +01:00
) . Hidden ( ) . String ( )
fsTypesExcludeSet bool
fsTypesExclude = kingpin . Flag (
"collector.filesystem.fs-types-exclude" ,
"Regexp of filesystem types to exclude for filesystem collector." ,
) . Default ( defFSTypesExcluded ) . PreAction ( func ( c * kingpin . ParseContext ) error {
fsTypesExcludeSet = true
return nil
} ) . String ( )
oldFSTypesExcluded = 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." ,
2021-03-23 12:00:06 +01:00
) . Hidden ( ) . String ( )
2016-03-15 14:01:08 +01:00
2024-02-18 12:04:30 +01:00
filesystemLabelNames = [ ] string { "device" , "mountpoint" , "fstype" , "device_error" }
2015-09-16 15:34:34 +02:00
)
type filesystemCollector struct {
2021-03-23 12:00:06 +01:00
excludedMountPointsPattern * regexp . Regexp
excludedFSTypesPattern * regexp . Regexp
2017-03-23 01:48:18 +01:00
sizeDesc , freeDesc , availDesc * prometheus . Desc
filesDesc , filesFreeDesc * prometheus . Desc
roDesc , deviceErrorDesc * prometheus . Desc
2019-12-31 17:19:37 +01:00
logger log . Logger
2015-09-16 15:34:34 +02:00
}
2017-01-03 15:55:40 +01:00
type filesystemLabels struct {
2024-02-18 12:04:30 +01:00
device , mountPoint , fsType , options , deviceError string
2017-01-03 15:55:40 +01:00
}
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.
2019-12-31 17:19:37 +01:00
func NewFilesystemCollector ( logger log . Logger ) ( Collector , error ) {
2021-03-23 12:00:06 +01:00
if * oldMountPointsExcluded != "" {
if ! mountPointsExcludeSet {
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" )
* mountPointsExclude = * oldMountPointsExcluded
} else {
return nil , errors . New ( "--collector.filesystem.ignored-mount-points and --collector.filesystem.mount-points-exclude are mutually exclusive" )
}
}
if * oldFSTypesExcluded != "" {
if ! fsTypesExcludeSet {
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" )
* fsTypesExclude = * oldFSTypesExcluded
} else {
return nil , errors . New ( "--collector.filesystem.ignored-fs-types and --collector.filesystem.fs-types-exclude are mutually exclusive" )
}
}
2015-09-16 15:34:34 +02:00
subsystem := "filesystem"
2021-03-23 12:00:06 +01:00
level . Info ( logger ) . Log ( "msg" , "Parsed flag --collector.filesystem.mount-points-exclude" , "flag" , * mountPointsExclude )
mountPointPattern := regexp . MustCompile ( * mountPointsExclude )
level . Info ( logger ) . Log ( "msg" , "Parsed flag --collector.filesystem.fs-types-exclude" , "flag" , * fsTypesExclude )
filesystemsTypesPattern := regexp . MustCompile ( * fsTypesExclude )
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 {
2021-03-23 12:00:06 +01:00
excludedMountPointsPattern : mountPointPattern ,
excludedFSTypesPattern : filesystemsTypesPattern ,
sizeDesc : sizeDesc ,
freeDesc : freeDesc ,
availDesc : availDesc ,
filesDesc : filesDesc ,
filesFreeDesc : filesFreeDesc ,
roDesc : roDesc ,
deviceErrorDesc : deviceErrorDesc ,
logger : logger ,
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 ,
2024-02-18 12:04:30 +01:00
s . deviceError , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2017-03-23 01:48:18 +01:00
)
2023-09-19 14:55:58 +02:00
ch <- prometheus . MustNewConstMetric (
c . roDesc , prometheus . GaugeValue ,
2024-02-18 12:04:30 +01:00
s . ro , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2023-09-19 14:55:58 +02:00
)
2017-03-23 01:48:18 +01:00
if s . deviceError > 0 {
continue
}
2015-09-16 15:34:34 +02:00
ch <- prometheus . MustNewConstMetric (
c . sizeDesc , prometheus . GaugeValue ,
2024-02-18 12:04:30 +01:00
s . size , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2015-09-16 15:34:34 +02:00
)
ch <- prometheus . MustNewConstMetric (
c . freeDesc , prometheus . GaugeValue ,
2024-02-18 12:04:30 +01:00
s . free , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2015-09-16 15:34:34 +02:00
)
ch <- prometheus . MustNewConstMetric (
c . availDesc , prometheus . GaugeValue ,
2024-02-18 12:04:30 +01:00
s . avail , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2015-09-16 15:34:34 +02:00
)
ch <- prometheus . MustNewConstMetric (
c . filesDesc , prometheus . GaugeValue ,
2024-02-18 12:04:30 +01:00
s . files , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2015-09-16 15:34:34 +02:00
)
ch <- prometheus . MustNewConstMetric (
c . filesFreeDesc , prometheus . GaugeValue ,
2024-02-18 12:04:30 +01:00
s . filesFree , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2015-09-16 15:34:34 +02:00
)
}
return nil
}