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-03-07 00:25:05 -08:00
"github.com/alecthomas/kingpin/v2"
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 (
2021-03-23 04:00:06 -07: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 06:34:34 -07:00
"collector.filesystem.ignored-mount-points" ,
2017-08-12 06:07:24 -07:00
"Regexp of mount points to ignore for filesystem collector." ,
2021-03-23 04:00:06 -07: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 06:01:08 -07:00
"collector.filesystem.ignored-fs-types" ,
2017-08-12 06:07:24 -07:00
"Regexp of filesystem types to ignore for filesystem collector." ,
2021-03-23 04:00:06 -07:00
) . Hidden ( ) . String ( )
2016-03-15 06:01:08 -07:00
2024-02-18 03:04:30 -08:00
filesystemLabelNames = [ ] string { "device" , "mountpoint" , "fstype" , "device_error" }
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
2024-07-14 07:33:12 -07:00
mountInfoDesc * prometheus . Desc
2019-12-31 08:19:37 -08:00
logger log . Logger
2015-09-16 06:34:34 -07:00
}
2017-01-03 06:55:40 -08:00
type filesystemLabels struct {
2024-07-14 07:33:12 -07:00
device , mountPoint , fsType , options , deviceError , major , minor 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
}
func init ( ) {
2017-09-28 06:06:26 -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.
2019-12-31 08:19:37 -08:00
func NewFilesystemCollector ( logger log . Logger ) ( Collector , error ) {
2021-03-23 04:00:06 -07: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 06:34:34 -07:00
subsystem := "filesystem"
2021-03-23 04:00:06 -07: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 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
2024-07-14 07:33:12 -07:00
mountInfoDesc := prometheus . NewDesc (
prometheus . BuildFQName ( namespace , subsystem , "mount_info" ) ,
"Filesystem mount information." ,
[ ] string { "device" , "major" , "minor" , "mountpoint" } ,
nil ,
)
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 ,
2024-07-14 07:33:12 -07:00
mountInfoDesc : mountInfoDesc ,
2021-03-23 04:00:06 -07:00
logger : logger ,
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 ,
2024-02-18 03:04:30 -08:00
s . deviceError , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2017-03-22 17:48:18 -07:00
)
2023-09-19 05:55:58 -07:00
ch <- prometheus . MustNewConstMetric (
c . roDesc , prometheus . GaugeValue ,
2024-02-18 03:04:30 -08:00
s . ro , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2023-09-19 05:55:58 -07:00
)
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 ,
2024-02-18 03:04:30 -08:00
s . size , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2015-09-16 06:34:34 -07:00
)
ch <- prometheus . MustNewConstMetric (
c . freeDesc , prometheus . GaugeValue ,
2024-02-18 03:04:30 -08:00
s . free , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2015-09-16 06:34:34 -07:00
)
ch <- prometheus . MustNewConstMetric (
c . availDesc , prometheus . GaugeValue ,
2024-02-18 03:04:30 -08:00
s . avail , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2015-09-16 06:34:34 -07:00
)
ch <- prometheus . MustNewConstMetric (
c . filesDesc , prometheus . GaugeValue ,
2024-02-18 03:04:30 -08:00
s . files , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2015-09-16 06:34:34 -07:00
)
ch <- prometheus . MustNewConstMetric (
c . filesFreeDesc , prometheus . GaugeValue ,
2024-02-18 03:04:30 -08:00
s . filesFree , s . labels . device , s . labels . mountPoint , s . labels . fsType , s . labels . deviceError ,
2015-09-16 06:34:34 -07:00
)
2024-07-14 07:33:12 -07:00
ch <- prometheus . MustNewConstMetric (
c . mountInfoDesc , prometheus . GaugeValue ,
1.0 , s . labels . device , s . labels . major , s . labels . minor , s . labels . mountPoint ,
)
2015-09-16 06:34:34 -07:00
}
return nil
}