@ -19,8 +19,8 @@ package collector
import (
"errors"
"fmt"
"log/slog"
"regexp"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/client_golang/prometheus"
@ -36,7 +36,7 @@ var (
mountPointsExcludeSet bool
mountPointsExclude = kingpin . Flag (
"collector.filesystem.mount-points-exclude" ,
"Regexp of mount points to exclude for filesystem collector. ",
"Regexp of mount points to exclude for filesystem collector. (mutually exclusive to mount-points-include) ",
) . Default ( defMountPointsExcluded ) . PreAction ( func ( c * kingpin . ParseContext ) error {
mountPointsExcludeSet = true
return nil
@ -45,11 +45,15 @@ var (
"collector.filesystem.ignored-mount-points" ,
"Regexp of mount points to ignore for filesystem collector." ,
) . Hidden ( ) . String ( )
mountPointsInclude = kingpin . Flag (
"collector.filesystem.mount-points-include" ,
"Regexp of mount points to include for filesystem collector. (mutually exclusive to mount-points-exclude)" ,
) . String ( )
fsTypesExcludeSet bool
fsTypesExclude = kingpin . Flag (
"collector.filesystem.fs-types-exclude" ,
"Regexp of filesystem types to exclude for filesystem collector. ",
"Regexp of filesystem types to exclude for filesystem collector. (mutually exclusive to fs-types-include) ",
) . Default ( defFSTypesExcluded ) . PreAction ( func ( c * kingpin . ParseContext ) error {
fsTypesExcludeSet = true
return nil
@ -58,13 +62,17 @@ var (
"collector.filesystem.ignored-fs-types" ,
"Regexp of filesystem types to ignore for filesystem collector." ,
) . Hidden ( ) . String ( )
fsTypesInclude = kingpin . Flag (
"collector.filesystem.fs-types-include" ,
"Regexp of filesystem types to exclude for filesystem collector. (mutually exclusive to fs-types-exclude)" ,
) . String ( )
filesystemLabelNames = [ ] string { "device" , "mountpoint" , "fstype" , "device_error" }
)
type filesystemCollector struct {
excludedMountPointsPattern * regexp . Regexp
excludedFSTypesPattern * regexp . Regexp
mountPointFilter deviceFilter
fsTypeFilter deviceFilter
sizeDesc , freeDesc , availDesc * prometheus . Desc
filesDesc , filesFreeDesc * prometheus . Desc
roDesc , deviceErrorDesc * prometheus . Desc
@ -89,29 +97,7 @@ func init() {
// NewFilesystemCollector returns a new Collector exposing filesystems stats.
func NewFilesystemCollector ( logger * slog . Logger ) ( Collector , error ) {
if * oldMountPointsExcluded != "" {
if ! mountPointsExcludeSet {
logger . Warn ( "--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 {
logger . Warn ( "--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" )
}
}
subsystem := "filesystem"
logger . Info ( "Parsed flag --collector.filesystem.mount-points-exclude" , "flag" , * mountPointsExclude )
mountPointPattern := regexp . MustCompile ( * mountPointsExclude )
logger . Info ( "Parsed flag --collector.filesystem.fs-types-exclude" , "flag" , * fsTypesExclude )
filesystemsTypesPattern := regexp . MustCompile ( * fsTypesExclude )
const subsystem = "filesystem"
sizeDesc := prometheus . NewDesc (
prometheus . BuildFQName ( namespace , subsystem , "size_bytes" ) ,
@ -162,9 +148,19 @@ func NewFilesystemCollector(logger *slog.Logger) (Collector, error) {
nil ,
)
mountPointFilter , err := newMountPointsFilter ( logger )
if err != nil {
return nil , fmt . Errorf ( "unable to parse mount points filter flags: %w" , err )
}
fsTypeFilter , err := newFSTypeFilter ( logger )
if err != nil {
return nil , fmt . Errorf ( "unable to parse fs types filter flags: %w" , err )
}
return & filesystemCollector {
excludedMountPointsPattern : mountPointPattern ,
excludedFSTypesPattern : filesystemsTypesPattern ,
mountPointFilter: mountPointFilter ,
fsTypeFilter: fsTypeFilter ,
sizeDesc : sizeDesc ,
freeDesc : freeDesc ,
availDesc : availDesc ,
@ -230,3 +226,61 @@ func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) error {
}
return nil
}
func newMountPointsFilter ( logger * slog . Logger ) ( deviceFilter , error ) {
if * oldMountPointsExcluded != "" {
if ! mountPointsExcludeSet {
logger . Warn ( "--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 deviceFilter { } , errors . New ( "--collector.filesystem.ignored-mount-points and --collector.filesystem.mount-points-exclude are mutually exclusive" )
}
}
if * mountPointsInclude != "" && ! mountPointsExcludeSet {
logger . Debug ( "mount-points-exclude flag not set when mount-points-include flag is set, assuming include is desired" )
* mountPointsExclude = ""
}
if * mountPointsExclude != "" && * mountPointsInclude != "" {
return deviceFilter { } , errors . New ( "--collector.filesystem.mount-points-exclude and --collector.filesystem.mount-points-include are mutually exclusive" )
}
if * mountPointsExclude != "" {
logger . Info ( "Parsed flag --collector.filesystem.mount-points-exclude" , "flag" , * mountPointsExclude )
}
if * mountPointsInclude != "" {
logger . Info ( "Parsed flag --collector.filesystem.mount-points-include" , "flag" , * mountPointsInclude )
}
return newDeviceFilter ( * mountPointsExclude , * mountPointsInclude ) , nil
}
func newFSTypeFilter ( logger * slog . Logger ) ( deviceFilter , error ) {
if * oldFSTypesExcluded != "" {
if ! fsTypesExcludeSet {
logger . Warn ( "--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 deviceFilter { } , errors . New ( "--collector.filesystem.ignored-fs-types and --collector.filesystem.fs-types-exclude are mutually exclusive" )
}
}
if * fsTypesInclude != "" && ! fsTypesExcludeSet {
logger . Debug ( "fs-types-exclude flag not set when fs-types-include flag is set, assuming include is desired" )
* fsTypesExclude = ""
}
if * fsTypesExclude != "" && * fsTypesInclude != "" {
return deviceFilter { } , errors . New ( "--collector.filesystem.fs-types-exclude and --collector.filesystem.fs-types-include are mutually exclusive" )
}
if * fsTypesExclude != "" {
logger . Info ( "Parsed flag --collector.filesystem.fs-types-exclude" , "flag" , * fsTypesExclude )
}
if * fsTypesInclude != "" {
logger . Info ( "Parsed flag --collector.filesystem.fs-types-include" , "flag" , * fsTypesInclude )
}
return newDeviceFilter ( * fsTypesExclude , * fsTypesInclude ) , nil
}