Add filesystem include flags

Add support for allow lists of filesystem mount points and filesystem
types. This allows for less messy regexps when you want to target only
specific lists of mount points or filesystem types.

Signed-off-by: Ben Kochie <superq@gmail.com>
This commit is contained in:
Ben Kochie 2024-11-05 10:36:13 +01:00
parent 07ee8efaa4
commit e44ce4c56d
No known key found for this signature in database
GPG key ID: C646B23C9E3245F1
8 changed files with 108 additions and 54 deletions

View file

@ -99,8 +99,8 @@ cpu | flags | --collector.cpu.info.flags-include | N/A
diskstats | device | --collector.diskstats.device-include | --collector.diskstats.device-exclude diskstats | device | --collector.diskstats.device-include | --collector.diskstats.device-exclude
ethtool | device | --collector.ethtool.device-include | --collector.ethtool.device-exclude ethtool | device | --collector.ethtool.device-include | --collector.ethtool.device-exclude
ethtool | metrics | --collector.ethtool.metrics-include | N/A ethtool | metrics | --collector.ethtool.metrics-include | N/A
filesystem | fs-types | N/A | --collector.filesystem.fs-types-exclude filesystem | fs-types | --collector.filesystem.fs-types-include | --collector.filesystem.fs-types-exclude
filesystem | mount-points | N/A | --collector.filesystem.mount-points-exclude filesystem | mount-points | --collector.filesystem.mount-points-include | --collector.filesystem.mount-points-exclude
hwmon | chip | --collector.hwmon.chip-include | --collector.hwmon.chip-exclude hwmon | chip | --collector.hwmon.chip-include | --collector.hwmon.chip-exclude
hwmon | sensor | --collector.hwmon.sensor-include | --collector.hwmon.sensor-exclude hwmon | sensor | --collector.hwmon.sensor-include | --collector.hwmon.sensor-exclude
interrupts | name | --collector.interrupts.name-include | --collector.interrupts.name-exclude interrupts | name | --collector.interrupts.name-include | --collector.interrupts.name-exclude

View file

@ -32,12 +32,12 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
return nil, err return nil, err
} }
for _, stat := range fsStat { for _, stat := range fsStat {
if c.excludedMountPointsPattern.MatchString(stat.MountPoint) { if c.mountPointFilter.ignored(stat.MountPoint) {
c.logger.Debug("Ignoring mount point", "mountpoint", stat.MountPoint) c.logger.Debug("Ignoring mount point", "mountpoint", stat.MountPoint)
continue continue
} }
fstype := stat.TypeString() fstype := stat.TypeString()
if c.excludedFSTypesPattern.MatchString(fstype) { if c.fsTypeFilter.ignored(fstype) {
c.logger.Debug("Ignoring fs type", "type", fstype) c.logger.Debug("Ignoring fs type", "type", fstype)
continue continue
} }

View file

@ -48,14 +48,14 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
stats = []filesystemStats{} stats = []filesystemStats{}
for i := 0; i < int(count); i++ { for i := 0; i < int(count); i++ {
mountpoint := C.GoString(&mnt[i].f_mntonname[0]) mountpoint := C.GoString(&mnt[i].f_mntonname[0])
if c.excludedMountPointsPattern.MatchString(mountpoint) { if c.mountPointFilter.ignored(mountpoint) {
c.logger.Debug("Ignoring mount point", "mountpoint", mountpoint) c.logger.Debug("Ignoring mount point", "mountpoint", mountpoint)
continue continue
} }
device := C.GoString(&mnt[i].f_mntfromname[0]) device := C.GoString(&mnt[i].f_mntfromname[0])
fstype := C.GoString(&mnt[i].f_fstypename[0]) fstype := C.GoString(&mnt[i].f_fstypename[0])
if c.excludedFSTypesPattern.MatchString(fstype) { if c.fsTypeFilter.ignored(fstype) {
c.logger.Debug("Ignoring fs type", "type", fstype) c.logger.Debug("Ignoring fs type", "type", fstype)
continue continue
} }

View file

@ -19,8 +19,8 @@ package collector
import ( import (
"errors" "errors"
"fmt"
"log/slog" "log/slog"
"regexp"
"github.com/alecthomas/kingpin/v2" "github.com/alecthomas/kingpin/v2"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -36,7 +36,7 @@ var (
mountPointsExcludeSet bool mountPointsExcludeSet bool
mountPointsExclude = kingpin.Flag( mountPointsExclude = kingpin.Flag(
"collector.filesystem.mount-points-exclude", "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 { ).Default(defMountPointsExcluded).PreAction(func(c *kingpin.ParseContext) error {
mountPointsExcludeSet = true mountPointsExcludeSet = true
return nil return nil
@ -45,11 +45,15 @@ var (
"collector.filesystem.ignored-mount-points", "collector.filesystem.ignored-mount-points",
"Regexp of mount points to ignore for filesystem collector.", "Regexp of mount points to ignore for filesystem collector.",
).Hidden().String() ).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 fsTypesExcludeSet bool
fsTypesExclude = kingpin.Flag( fsTypesExclude = kingpin.Flag(
"collector.filesystem.fs-types-exclude", "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 { ).Default(defFSTypesExcluded).PreAction(func(c *kingpin.ParseContext) error {
fsTypesExcludeSet = true fsTypesExcludeSet = true
return nil return nil
@ -58,13 +62,17 @@ var (
"collector.filesystem.ignored-fs-types", "collector.filesystem.ignored-fs-types",
"Regexp of filesystem types to ignore for filesystem collector.", "Regexp of filesystem types to ignore for filesystem collector.",
).Hidden().String() ).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"} filesystemLabelNames = []string{"device", "mountpoint", "fstype", "device_error"}
) )
type filesystemCollector struct { type filesystemCollector struct {
excludedMountPointsPattern *regexp.Regexp mountPointFilter deviceFilter
excludedFSTypesPattern *regexp.Regexp fsTypeFilter deviceFilter
sizeDesc, freeDesc, availDesc *prometheus.Desc sizeDesc, freeDesc, availDesc *prometheus.Desc
filesDesc, filesFreeDesc *prometheus.Desc filesDesc, filesFreeDesc *prometheus.Desc
roDesc, deviceErrorDesc *prometheus.Desc roDesc, deviceErrorDesc *prometheus.Desc
@ -89,29 +97,7 @@ func init() {
// NewFilesystemCollector returns a new Collector exposing filesystems stats. // NewFilesystemCollector returns a new Collector exposing filesystems stats.
func NewFilesystemCollector(logger *slog.Logger) (Collector, error) { func NewFilesystemCollector(logger *slog.Logger) (Collector, error) {
if *oldMountPointsExcluded != "" { const subsystem = "filesystem"
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)
sizeDesc := prometheus.NewDesc( sizeDesc := prometheus.NewDesc(
prometheus.BuildFQName(namespace, subsystem, "size_bytes"), prometheus.BuildFQName(namespace, subsystem, "size_bytes"),
@ -162,18 +148,28 @@ func NewFilesystemCollector(logger *slog.Logger) (Collector, error) {
nil, 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{ return &filesystemCollector{
excludedMountPointsPattern: mountPointPattern, mountPointFilter: mountPointFilter,
excludedFSTypesPattern: filesystemsTypesPattern, fsTypeFilter: fsTypeFilter,
sizeDesc: sizeDesc, sizeDesc: sizeDesc,
freeDesc: freeDesc, freeDesc: freeDesc,
availDesc: availDesc, availDesc: availDesc,
filesDesc: filesDesc, filesDesc: filesDesc,
filesFreeDesc: filesFreeDesc, filesFreeDesc: filesFreeDesc,
roDesc: roDesc, roDesc: roDesc,
deviceErrorDesc: deviceErrorDesc, deviceErrorDesc: deviceErrorDesc,
mountInfoDesc: mountInfoDesc, mountInfoDesc: mountInfoDesc,
logger: logger, logger: logger,
}, nil }, nil
} }
@ -230,3 +226,61 @@ func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) error {
} }
return nil 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
}

View file

@ -39,14 +39,14 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
stats := []filesystemStats{} stats := []filesystemStats{}
for _, fs := range buf { for _, fs := range buf {
mountpoint := unix.ByteSliceToString(fs.Mntonname[:]) mountpoint := unix.ByteSliceToString(fs.Mntonname[:])
if c.excludedMountPointsPattern.MatchString(mountpoint) { if c.mountPointFilter.ignored(mountpoint) {
c.logger.Debug("Ignoring mount point", "mountpoint", mountpoint) c.logger.Debug("Ignoring mount point", "mountpoint", mountpoint)
continue continue
} }
device := unix.ByteSliceToString(fs.Mntfromname[:]) device := unix.ByteSliceToString(fs.Mntfromname[:])
fstype := unix.ByteSliceToString(fs.Fstypename[:]) fstype := unix.ByteSliceToString(fs.Fstypename[:])
if c.excludedFSTypesPattern.MatchString(fstype) { if c.fsTypeFilter.ignored(fstype) {
c.logger.Debug("Ignoring fs type", "type", fstype) c.logger.Debug("Ignoring fs type", "type", fstype)
continue continue
} }

View file

@ -73,12 +73,12 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
go func() { go func() {
for _, labels := range mps { for _, labels := range mps {
if c.excludedMountPointsPattern.MatchString(labels.mountPoint) { if c.mountPointFilter.ignored(labels.mountPoint) {
c.logger.Debug("Ignoring mount point", "mountpoint", labels.mountPoint) c.logger.Debug("Ignoring mount point", "mountpoint", labels.mountPoint)
continue continue
} }
if c.excludedFSTypesPattern.MatchString(labels.fsType) { if c.fsTypeFilter.ignored(labels.fsType) {
c.logger.Debug("Ignoring fs", "type", labels.fsType) c.logger.Debug("Ignoring fs type", "type", labels.fsType)
continue continue
} }

View file

@ -97,14 +97,14 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
stats = []filesystemStats{} stats = []filesystemStats{}
for _, v := range mnt { for _, v := range mnt {
mountpoint := unix.ByteSliceToString(v.F_mntonname[:]) mountpoint := unix.ByteSliceToString(v.F_mntonname[:])
if c.excludedMountPointsPattern.MatchString(mountpoint) { if c.mountPointFilter.ignored(mountpoint) {
c.logger.Debug("msg", "Ignoring mount point", "mountpoint", mountpoint) c.logger.Debug("msg", "Ignoring mount point", "mountpoint", mountpoint)
continue continue
} }
device := unix.ByteSliceToString(v.F_mntfromname[:]) device := unix.ByteSliceToString(v.F_mntfromname[:])
fstype := unix.ByteSliceToString(v.F_fstypename[:]) fstype := unix.ByteSliceToString(v.F_fstypename[:])
if c.excludedFSTypesPattern.MatchString(fstype) { if c.fsTypeFilter.ignored(fstype) {
c.logger.Debug("msg", "Ignoring fs type", "type", fstype) c.logger.Debug("msg", "Ignoring fs type", "type", fstype)
continue continue
} }

View file

@ -41,14 +41,14 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
stats = []filesystemStats{} stats = []filesystemStats{}
for _, v := range mnt { for _, v := range mnt {
mountpoint := unix.ByteSliceToString(v.F_mntonname[:]) mountpoint := unix.ByteSliceToString(v.F_mntonname[:])
if c.excludedMountPointsPattern.MatchString(mountpoint) { if c.mountPointFilter.ignored(mountpoint) {
c.logger.Debug("Ignoring mount point", "mountpoint", mountpoint) c.logger.Debug("Ignoring mount point", "mountpoint", mountpoint)
continue continue
} }
device := unix.ByteSliceToString(v.F_mntfromname[:]) device := unix.ByteSliceToString(v.F_mntfromname[:])
fstype := unix.ByteSliceToString(v.F_fstypename[:]) fstype := unix.ByteSliceToString(v.F_fstypename[:])
if c.excludedFSTypesPattern.MatchString(fstype) { if c.fsTypeFilter.ignored(fstype) {
c.logger.Debug("Ignoring fs type", "type", fstype) c.logger.Debug("Ignoring fs type", "type", fstype)
continue continue
} }