Skip to content

Commit 226c80c

Browse files
SuperQdiscordianfish
authored andcommitted
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 <[email protected]>
1 parent cf8c689 commit 226c80c

File tree

8 files changed

+108
-54
lines changed

8 files changed

+108
-54
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ cpu | flags | --collector.cpu.info.flags-include | N/A
9999
diskstats | device | --collector.diskstats.device-include | --collector.diskstats.device-exclude
100100
ethtool | device | --collector.ethtool.device-include | --collector.ethtool.device-exclude
101101
ethtool | metrics | --collector.ethtool.metrics-include | N/A
102-
filesystem | fs-types | N/A | --collector.filesystem.fs-types-exclude
103-
filesystem | mount-points | N/A | --collector.filesystem.mount-points-exclude
102+
filesystem | fs-types | --collector.filesystem.fs-types-include | --collector.filesystem.fs-types-exclude
103+
filesystem | mount-points | --collector.filesystem.mount-points-include | --collector.filesystem.mount-points-exclude
104104
hwmon | chip | --collector.hwmon.chip-include | --collector.hwmon.chip-exclude
105105
hwmon | sensor | --collector.hwmon.sensor-include | --collector.hwmon.sensor-exclude
106106
interrupts | name | --collector.interrupts.name-include | --collector.interrupts.name-exclude

collector/filesystem_aix.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
3232
return nil, err
3333
}
3434
for _, stat := range fsStat {
35-
if c.excludedMountPointsPattern.MatchString(stat.MountPoint) {
35+
if c.mountPointFilter.ignored(stat.MountPoint) {
3636
c.logger.Debug("Ignoring mount point", "mountpoint", stat.MountPoint)
3737
continue
3838
}
3939
fstype := stat.TypeString()
40-
if c.excludedFSTypesPattern.MatchString(fstype) {
40+
if c.fsTypeFilter.ignored(fstype) {
4141
c.logger.Debug("Ignoring fs type", "type", fstype)
4242
continue
4343
}

collector/filesystem_bsd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
4848
stats = []filesystemStats{}
4949
for i := 0; i < int(count); i++ {
5050
mountpoint := C.GoString(&mnt[i].f_mntonname[0])
51-
if c.excludedMountPointsPattern.MatchString(mountpoint) {
51+
if c.mountPointFilter.ignored(mountpoint) {
5252
c.logger.Debug("Ignoring mount point", "mountpoint", mountpoint)
5353
continue
5454
}
5555

5656
device := C.GoString(&mnt[i].f_mntfromname[0])
5757
fstype := C.GoString(&mnt[i].f_fstypename[0])
58-
if c.excludedFSTypesPattern.MatchString(fstype) {
58+
if c.fsTypeFilter.ignored(fstype) {
5959
c.logger.Debug("Ignoring fs type", "type", fstype)
6060
continue
6161
}

collector/filesystem_common.go

Lines changed: 93 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ package collector
1919

2020
import (
2121
"errors"
22+
"fmt"
2223
"log/slog"
23-
"regexp"
2424

2525
"github.com/alecthomas/kingpin/v2"
2626
"github.com/prometheus/client_golang/prometheus"
@@ -36,7 +36,7 @@ var (
3636
mountPointsExcludeSet bool
3737
mountPointsExclude = kingpin.Flag(
3838
"collector.filesystem.mount-points-exclude",
39-
"Regexp of mount points to exclude for filesystem collector.",
39+
"Regexp of mount points to exclude for filesystem collector. (mutually exclusive to mount-points-include)",
4040
).Default(defMountPointsExcluded).PreAction(func(c *kingpin.ParseContext) error {
4141
mountPointsExcludeSet = true
4242
return nil
@@ -45,11 +45,15 @@ var (
4545
"collector.filesystem.ignored-mount-points",
4646
"Regexp of mount points to ignore for filesystem collector.",
4747
).Hidden().String()
48+
mountPointsInclude = kingpin.Flag(
49+
"collector.filesystem.mount-points-include",
50+
"Regexp of mount points to include for filesystem collector. (mutually exclusive to mount-points-exclude)",
51+
).String()
4852

4953
fsTypesExcludeSet bool
5054
fsTypesExclude = kingpin.Flag(
5155
"collector.filesystem.fs-types-exclude",
52-
"Regexp of filesystem types to exclude for filesystem collector.",
56+
"Regexp of filesystem types to exclude for filesystem collector. (mutually exclusive to fs-types-include)",
5357
).Default(defFSTypesExcluded).PreAction(func(c *kingpin.ParseContext) error {
5458
fsTypesExcludeSet = true
5559
return nil
@@ -58,13 +62,17 @@ var (
5862
"collector.filesystem.ignored-fs-types",
5963
"Regexp of filesystem types to ignore for filesystem collector.",
6064
).Hidden().String()
65+
fsTypesInclude = kingpin.Flag(
66+
"collector.filesystem.fs-types-include",
67+
"Regexp of filesystem types to exclude for filesystem collector. (mutually exclusive to fs-types-exclude)",
68+
).String()
6169

6270
filesystemLabelNames = []string{"device", "mountpoint", "fstype", "device_error"}
6371
)
6472

6573
type filesystemCollector struct {
66-
excludedMountPointsPattern *regexp.Regexp
67-
excludedFSTypesPattern *regexp.Regexp
74+
mountPointFilter deviceFilter
75+
fsTypeFilter deviceFilter
6876
sizeDesc, freeDesc, availDesc *prometheus.Desc
6977
filesDesc, filesFreeDesc *prometheus.Desc
7078
roDesc, deviceErrorDesc *prometheus.Desc
@@ -89,29 +97,7 @@ func init() {
8997

9098
// NewFilesystemCollector returns a new Collector exposing filesystems stats.
9199
func NewFilesystemCollector(logger *slog.Logger) (Collector, error) {
92-
if *oldMountPointsExcluded != "" {
93-
if !mountPointsExcludeSet {
94-
logger.Warn("--collector.filesystem.ignored-mount-points is DEPRECATED and will be removed in 2.0.0, use --collector.filesystem.mount-points-exclude")
95-
*mountPointsExclude = *oldMountPointsExcluded
96-
} else {
97-
return nil, errors.New("--collector.filesystem.ignored-mount-points and --collector.filesystem.mount-points-exclude are mutually exclusive")
98-
}
99-
}
100-
101-
if *oldFSTypesExcluded != "" {
102-
if !fsTypesExcludeSet {
103-
logger.Warn("--collector.filesystem.ignored-fs-types is DEPRECATED and will be removed in 2.0.0, use --collector.filesystem.fs-types-exclude")
104-
*fsTypesExclude = *oldFSTypesExcluded
105-
} else {
106-
return nil, errors.New("--collector.filesystem.ignored-fs-types and --collector.filesystem.fs-types-exclude are mutually exclusive")
107-
}
108-
}
109-
110-
subsystem := "filesystem"
111-
logger.Info("Parsed flag --collector.filesystem.mount-points-exclude", "flag", *mountPointsExclude)
112-
mountPointPattern := regexp.MustCompile(*mountPointsExclude)
113-
logger.Info("Parsed flag --collector.filesystem.fs-types-exclude", "flag", *fsTypesExclude)
114-
filesystemsTypesPattern := regexp.MustCompile(*fsTypesExclude)
100+
const subsystem = "filesystem"
115101

116102
sizeDesc := prometheus.NewDesc(
117103
prometheus.BuildFQName(namespace, subsystem, "size_bytes"),
@@ -162,18 +148,28 @@ func NewFilesystemCollector(logger *slog.Logger) (Collector, error) {
162148
nil,
163149
)
164150

151+
mountPointFilter, err := newMountPointsFilter(logger)
152+
if err != nil {
153+
return nil, fmt.Errorf("unable to parse mount points filter flags: %w", err)
154+
}
155+
156+
fsTypeFilter, err := newFSTypeFilter(logger)
157+
if err != nil {
158+
return nil, fmt.Errorf("unable to parse fs types filter flags: %w", err)
159+
}
160+
165161
return &filesystemCollector{
166-
excludedMountPointsPattern: mountPointPattern,
167-
excludedFSTypesPattern: filesystemsTypesPattern,
168-
sizeDesc: sizeDesc,
169-
freeDesc: freeDesc,
170-
availDesc: availDesc,
171-
filesDesc: filesDesc,
172-
filesFreeDesc: filesFreeDesc,
173-
roDesc: roDesc,
174-
deviceErrorDesc: deviceErrorDesc,
175-
mountInfoDesc: mountInfoDesc,
176-
logger: logger,
162+
mountPointFilter: mountPointFilter,
163+
fsTypeFilter: fsTypeFilter,
164+
sizeDesc: sizeDesc,
165+
freeDesc: freeDesc,
166+
availDesc: availDesc,
167+
filesDesc: filesDesc,
168+
filesFreeDesc: filesFreeDesc,
169+
roDesc: roDesc,
170+
deviceErrorDesc: deviceErrorDesc,
171+
mountInfoDesc: mountInfoDesc,
172+
logger: logger,
177173
}, nil
178174
}
179175

@@ -230,3 +226,61 @@ func (c *filesystemCollector) Update(ch chan<- prometheus.Metric) error {
230226
}
231227
return nil
232228
}
229+
230+
func newMountPointsFilter(logger *slog.Logger) (deviceFilter, error) {
231+
if *oldMountPointsExcluded != "" {
232+
if !mountPointsExcludeSet {
233+
logger.Warn("--collector.filesystem.ignored-mount-points is DEPRECATED and will be removed in 2.0.0, use --collector.filesystem.mount-points-exclude")
234+
*mountPointsExclude = *oldMountPointsExcluded
235+
} else {
236+
return deviceFilter{}, errors.New("--collector.filesystem.ignored-mount-points and --collector.filesystem.mount-points-exclude are mutually exclusive")
237+
}
238+
}
239+
240+
if *mountPointsInclude != "" && !mountPointsExcludeSet {
241+
logger.Debug("mount-points-exclude flag not set when mount-points-include flag is set, assuming include is desired")
242+
*mountPointsExclude = ""
243+
}
244+
245+
if *mountPointsExclude != "" && *mountPointsInclude != "" {
246+
return deviceFilter{}, errors.New("--collector.filesystem.mount-points-exclude and --collector.filesystem.mount-points-include are mutually exclusive")
247+
}
248+
249+
if *mountPointsExclude != "" {
250+
logger.Info("Parsed flag --collector.filesystem.mount-points-exclude", "flag", *mountPointsExclude)
251+
}
252+
if *mountPointsInclude != "" {
253+
logger.Info("Parsed flag --collector.filesystem.mount-points-include", "flag", *mountPointsInclude)
254+
}
255+
256+
return newDeviceFilter(*mountPointsExclude, *mountPointsInclude), nil
257+
}
258+
259+
func newFSTypeFilter(logger *slog.Logger) (deviceFilter, error) {
260+
if *oldFSTypesExcluded != "" {
261+
if !fsTypesExcludeSet {
262+
logger.Warn("--collector.filesystem.ignored-fs-types is DEPRECATED and will be removed in 2.0.0, use --collector.filesystem.fs-types-exclude")
263+
*fsTypesExclude = *oldFSTypesExcluded
264+
} else {
265+
return deviceFilter{}, errors.New("--collector.filesystem.ignored-fs-types and --collector.filesystem.fs-types-exclude are mutually exclusive")
266+
}
267+
}
268+
269+
if *fsTypesInclude != "" && !fsTypesExcludeSet {
270+
logger.Debug("fs-types-exclude flag not set when fs-types-include flag is set, assuming include is desired")
271+
*fsTypesExclude = ""
272+
}
273+
274+
if *fsTypesExclude != "" && *fsTypesInclude != "" {
275+
return deviceFilter{}, errors.New("--collector.filesystem.fs-types-exclude and --collector.filesystem.fs-types-include are mutually exclusive")
276+
}
277+
278+
if *fsTypesExclude != "" {
279+
logger.Info("Parsed flag --collector.filesystem.fs-types-exclude", "flag", *fsTypesExclude)
280+
}
281+
if *fsTypesInclude != "" {
282+
logger.Info("Parsed flag --collector.filesystem.fs-types-include", "flag", *fsTypesInclude)
283+
}
284+
285+
return newDeviceFilter(*fsTypesExclude, *fsTypesInclude), nil
286+
}

collector/filesystem_freebsd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
3939
stats := []filesystemStats{}
4040
for _, fs := range buf {
4141
mountpoint := unix.ByteSliceToString(fs.Mntonname[:])
42-
if c.excludedMountPointsPattern.MatchString(mountpoint) {
42+
if c.mountPointFilter.ignored(mountpoint) {
4343
c.logger.Debug("Ignoring mount point", "mountpoint", mountpoint)
4444
continue
4545
}
4646

4747
device := unix.ByteSliceToString(fs.Mntfromname[:])
4848
fstype := unix.ByteSliceToString(fs.Fstypename[:])
49-
if c.excludedFSTypesPattern.MatchString(fstype) {
49+
if c.fsTypeFilter.ignored(fstype) {
5050
c.logger.Debug("Ignoring fs type", "type", fstype)
5151
continue
5252
}

collector/filesystem_linux.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ func (c *filesystemCollector) GetStats() ([]filesystemStats, error) {
7373

7474
go func() {
7575
for _, labels := range mps {
76-
if c.excludedMountPointsPattern.MatchString(labels.mountPoint) {
76+
if c.mountPointFilter.ignored(labels.mountPoint) {
7777
c.logger.Debug("Ignoring mount point", "mountpoint", labels.mountPoint)
7878
continue
7979
}
80-
if c.excludedFSTypesPattern.MatchString(labels.fsType) {
81-
c.logger.Debug("Ignoring fs", "type", labels.fsType)
80+
if c.fsTypeFilter.ignored(labels.fsType) {
81+
c.logger.Debug("Ignoring fs type", "type", labels.fsType)
8282
continue
8383
}
8484

collector/filesystem_netbsd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
9797
stats = []filesystemStats{}
9898
for _, v := range mnt {
9999
mountpoint := unix.ByteSliceToString(v.F_mntonname[:])
100-
if c.excludedMountPointsPattern.MatchString(mountpoint) {
100+
if c.mountPointFilter.ignored(mountpoint) {
101101
c.logger.Debug("msg", "Ignoring mount point", "mountpoint", mountpoint)
102102
continue
103103
}
104104

105105
device := unix.ByteSliceToString(v.F_mntfromname[:])
106106
fstype := unix.ByteSliceToString(v.F_fstypename[:])
107-
if c.excludedFSTypesPattern.MatchString(fstype) {
107+
if c.fsTypeFilter.ignored(fstype) {
108108
c.logger.Debug("msg", "Ignoring fs type", "type", fstype)
109109
continue
110110
}

collector/filesystem_openbsd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@ func (c *filesystemCollector) GetStats() (stats []filesystemStats, err error) {
4141
stats = []filesystemStats{}
4242
for _, v := range mnt {
4343
mountpoint := unix.ByteSliceToString(v.F_mntonname[:])
44-
if c.excludedMountPointsPattern.MatchString(mountpoint) {
44+
if c.mountPointFilter.ignored(mountpoint) {
4545
c.logger.Debug("Ignoring mount point", "mountpoint", mountpoint)
4646
continue
4747
}
4848

4949
device := unix.ByteSliceToString(v.F_mntfromname[:])
5050
fstype := unix.ByteSliceToString(v.F_fstypename[:])
51-
if c.excludedFSTypesPattern.MatchString(fstype) {
51+
if c.fsTypeFilter.ignored(fstype) {
5252
c.logger.Debug("Ignoring fs type", "type", fstype)
5353
continue
5454
}

0 commit comments

Comments
 (0)